validating multiple interfaces

Topics: Validation Application Block
May 18, 2007 at 4:18 PM

Suppose I have an instance of concrete class Employee (myPerson) which implements IPhoneNumber and IPerson, and I have rules defined for both of these interfaces. The following code works as expected:

Assert.IsFalse(Validation.ValidateFromConfiguration<IPerson>(myPerson, "Save").IsValid());
Assert.IsFalse(Validation.ValidateFromConfiguration<IPhoneNumber>(myPerson, "Save").IsValid());

However, the next snippet fails
Assert.IsFalse(Validation.ValidateFromConfiguration<Employee>(myPerson, "Save").IsValid());

I would expect this validation to be implicit, but it does not seem to be so. Is there a way to achieve what I am attempting? Or do I need to explicitly test each component of my class?

Thanks for any input,
May 19, 2007 at 10:54 PM
Edited May 20, 2007 at 5:26 AM
I am not sure why you would expect that validation to be implicit.

The Validation Application Block maps validators to types. In your configuration you have mapped validators to types of IPerson and IPhoneNumber but not the type Employee. When you try to validate the type Employee, the Validation Application Block does not find any validators attached to the type in the configuration file so it will just retun IsValid = true.

If you want to validate by type Employee, you will have to assign validators to the type Employee.




David Hayden
Microsoft MVP C#
May 21, 2007 at 4:21 PM
Thanks for the clarification, David.