PIAB with VAB for Interfaces

Topics: Policy Injection Application Block, Validation Application Block
Oct 29, 2009 at 3:46 PM

I know this has been asked before, but I can't find any resolution:  Using Enterprise Library Configuration in VS08 to use Policy Injection to inject Validation Rules into Types.

Using a VERY simple example: Interface IPerson, User : IPerson, Tester : IPerson

I'd like a policy to inject all types (User, Tester) that imlpement an interface (IPerson) with a simple validation (range validation on a member for example)

 

The injection "works" - since I can validate as follows:

results = Validation.Validate<IPerson>(myPerson);

 

However, this doesn't:

Tester myTester = new Tester();
Validator validator = ValidationFactory.CreateValidator(Tester.GetType());
results = validator.Validate(myTester);

 

Am I thinking about this all wrong?  Should I always be validating with the interface?  Basically, I'm looking for the best paractice for imlpementing consistent and repeatable validation across a number of types which implement an interface.  Policy Injection appears to offer this since I can change the rules without requiring a code build.

 

Any help gratefully received.

 

Oct 30, 2009 at 2:49 AM

If you want to validate based on the validation rules defined on the interface, you should use the interface type when creating the validator or when calling Validate<InterfacetType>.  Here is a related thread - http://entlib.codeplex.com/Thread/View.aspx?ThreadId=10450.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com