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.
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:
FileConfigurationSource
based on the role. Something like: return (new FileConfigurationSource('validation_' + role + '.config')).GetSection(sectionName);
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:
I hope this helps.