Ways to validate interfaces

Topics: Validation Application Block
Jul 23, 2007 at 9:47 AM
Hi!

I have a similar problem as Joe in this thread:
http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=10450

I would like some input on how to move forward.

Suppose you have this:
IPhoneNumber
IPerson
IEmployee : IPerson, IPhoneNumber

All interfaces are targets of validation, i.e. IPhoneNumber has validation on its property PhoneNumber and IPerson has validation on its property Name and so on.

Let's say you have an employee:
IEmployee employee;

In order to fully validate an IEmployee you would have to make three calls:
Validation.Validate(employee);
Validation.Validate((IPhoneNumber)employee);
Validation.Validate((IPerson)employee);

Question 1: Is there a better way than calling Validate multiple times to validate an IEmployee?


One possible solution I've been thinking about is to wrap the Validation.Validate<T>(T target) call with my own validation method, MyValidate<T>(T target).
This would first call Validation.Validate(target).
Then check if T has a specific attribute, say ValidateBaseTypes in lack of a better name. If it does then recursively call MyValidate for every base type.

Pseudo code:
MyValidate<T>(T target)
{