(Enterprise Validation Block, Interfaces) => BIG Trouble

I had a first look at Microsoft’s Enterprise Validation Block 4.1 today and trashed it before I even got really started. The reason: The thing doesn’t really support OO-architectures that rely on interfaces / polymorphism.

The code above won’t output a single validation error, because it only checks the Modelclass for validation attributes. And there are none – the rules have been declared in the IModelinterface. My bad.The code below, on the other hand, works. Notice that the model variable is declared using the interface type:

Now, it gets really ugly when you’re having attributes at different places. After this initial test, I moved the third validation rule into the implementing Model class and removed the Third property from the interface:

As a result, validating for the IModel interface returns two validation errors, and validating for the Model class reports a single error for the third rule. Now think about handling scenarios where your class implements several interfaces that may provide validation rules. Ouch.

Support for inheritance was the absolute first thing I tested. Frankly, it is beyond me how this library could make it into production over a year ago with this kind of behavior.

This not only promotes bad design, it’s also dangerous as hell because refactoring silently breaks your validation.

Actually, it is possible to validate inherited class and still use validators on the base class/interface. I had the same problem before and blogged about it here: http://codemind.blogspot.com/2008/08/are-you-valid.html
You should not use Validation facade, but instead create an instance of Validator class by using ValidationFactory. So, instead of:
var result = Validation.Validate(model);
you should use:
var validator = ValidationFactory.CreateValidator(typeof(Model));
var result = validator.Validate(model);

Not really sure why the Validation facade doesn’t have the support for inheritance. It’s definitely confusing.