0

I am looking for examples of configuration based validation using Validation Application Block. I've found this

I want to ask if someone has an alternative solution to using EL VAB 5.0 to achieve configuration based validation. I've started with DataAnnotations but soon found out that some properties will need different types of validation depending on who is using the application.

Also if anyone has more examples for configuration with VAB and any advice as to what I might run into, please share.

Steven
  • 166,672
  • 24
  • 332
  • 435
mare
  • 13,033
  • 24
  • 102
  • 191
  • I wonder if some sort of IoC container that attaches attributes dynamically at runtime would work? – Nathan Taylor Aug 30 '10 at 16:20
  • It would work for me but haven't seen a sample of it anywhere yet. – mare Aug 30 '10 at 16:26
  • As I might have suspected, such a thing isn't even possible. Attributes are type metadata and as such the only way to append them at runtime would be by generating a brand new type. That being said, you could resort to having different model types which all inherit a single base type, but define unique validation rules. i.e. Product > AdminProduct > ClientProduct. Attribute values are not inherited unless specifically marked as such. – Nathan Taylor Aug 30 '10 at 16:33
  • 1
    @Nathan Taylor, @Mare, it is possible to attach DataAnnotation attributes at runtime via a ModelValidatorProvider if your looking for a "in the box" solution to dynamic validation. I use this technique to even pull validation information from a database for a very dynamically validated system. – John Farrell Aug 30 '10 at 16:45
  • please post details as an answer – mare Aug 30 '10 at 16:58
  • @mare Ask the correct question. Can't throw a dataanootations based solution in a question about application settings and VAB. – John Farrell Aug 30 '10 at 19:40

1 Answers1

0

There are several paths you can walk to achieve this. First of all you can (ab)use rulesets for this. You can create a 'base' ruleset with rules that hold for everybody and you can make a ruleset per role in the system and perhaps even a ruleset per user, but of course that would be cumbersome.

Another option would be to create an IConfigurationSource implementation that is able to return a ValidationSettings instance, based on the logged in user. Now there are several ways that you can build a ValidationSettings object. Here are a few examples:

  1. You can load multiple configuration files from disk using the FileConfigurationSource based on the role. Something like: return (new FileConfigurationSource('validation_' + role + '.config')).GetSection(sectionName);
  2. You can build up the ValidationSettings instances dynamically (and cache them). You can store this definition in the database and load them (this would be a lot of work) or define them in code (perhaps separated by assembly). Here is an example of a code based configuration.

Also to prevent having to duplicate parts of your configuration, you can do the following:

  1. Merge multiple configurations together. You can for instance merge the base line validation with the role specific validation. This saves you from having to manually validate according to the base line and do a second validation for the role specific validation. While this is not supported out of the box, I wrote about how to do this on my blog here.
  2. You can merge rules based on type inheritance. While VAB only supports validator inheritance for attribute based validation out of the box, I've wrote about this on my blog, here.

I hope this helps.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • This is what I was looking for about VAB. I just have to decide now whether to actually use VAB or go with something else because that introduces another piece of code into the app, which complicates development to some degree. – mare Aug 31 '10 at 08:18
  • @Mare: As you might notice from my answers here on SO and my blog posts I really like VAB. However, it's flexibility comes at a price. It isn't an easy framework. I myself am still finding new (better) ways of doing things with VAB (one of the reasons I like it). If you choose to work with VAB and having any problems, I'll be happy to help. But at least, I wish you the best of luck in picking the right framework for you and your team. – Steven Aug 31 '10 at 08:33
  • I wrote an answer a few months back in which I show how to do fluent configuration with VAB: http://stackoverflow.com/a/8906203/264697. – Steven Jun 06 '12 at 21:22