VAB - Load Ruleset(s) from database

Topics: Validation Application Block
Jan 7, 2009 at 10:08 PM
Consider these requirements:

Employee class with a DepartmentCode property.  Our application is used by many different clients and each one has a different HR system and a different convention for DepartmentCode. 

We would like the ability to be able to load a base application ruleset (say the string length limit on DepartmentCode is 25 chars because that is the physical limit in the DB).  Technically, these could be hardcoded in configuration.

However, it would be great to be able to store validation metadata for our application object fields in the database and allow them to be easily configured and allow the client to use an admin interface to update them.

Client A might add(or turn on) a NotNullable rule to the DepartmentCode but nothing else.
Client B might add a NotNullable rule and a RegEx rule to ensure the code entered looks like "HRD-XXXX" where the X's are integers.

These types of validations would be great to incorporate using the VAB and object level validation, but I believe it would involve loading rulesets (applicable for that client instance) at application start and assigning them to the objects.  If the validation settings are changed, then the cache would be cleared and the rulesets reloaded.

What would be the best way to implement this using the VAB?

Jan 8, 2009 at 11:02 AM
Edited Jan 8, 2009 at 1:25 PM

Please see if this thread helps.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Jan 8, 2009 at 2:47 PM
Yeah, I read those threads already... but they are a bit old... I was hoping something had changed for the better.

So automatically storing configuration in the database is not directly supported by the VAB... ok.  But what if I created my own schema to store facts such as:

Employee Entity
- DepartmentCode column
         - Not Nullable
         - RegEx string
         - MaxLength

Is there a way for me to programmatically hookup the objects with their appropriate validators?  Something like:

Type empType = typeof(Employee);
IValidator<string> deptCodeFormatValidator = new RegExValidator("xxxx");

Validation.RegisterRuleOnType(empType, "DepartmentCode", deptCodeFormatValidator);

This would allow me to read my rules from anywhere... get a DataTable of rules and loop them calling RegisterRuleOnType for each rule.  Then the VAB would be configured the same as if it had read the info from an xml file.

From the code examples/quickstarts I've seen, they only mention creating a validator like I did above, and then explicitly using it to validate an object, which might be ok for a single object and single validation test, but if you have multiple validators for a single property, that might be less than ideal.


Jan 9, 2009 at 4:11 AM
There's no support for dynamically hooking up validators to objects even in the latest version.  I think using the SqlConfigurationSource is still the way to go or probably creating your own IConfigurationSource that will also load configuration from your specific database.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.