Cannot use Validation using FileConfigurationSource and string[]Rulesets

Topics: General discussion, Validation Application Block
Sep 24, 2008 at 3:37 PM
Edited Sep 24, 2008 at 3:38 PM
Hi guys

We have decided to keep each application block separate and therefore I was exploring how to do with the Validation Block.

I have this code and works fine and its an baseclass  



virtual ValidationResults Validate(T entity, params string[] ruleset)
    return  Validation.ValidateFromConfiguration<T>(entity, ruleset);


 Now let's suppose I wanted to use together with FileConfiguration Source I got lost how to do it?
My failed attempt? 

public virtual ValidationResults Validate(T entity, string validationConfigPath,params string[] ruleset) 
        IConfigurationSource source=new FileConfigurationSource(validationConfigPath); 
      //Cannot find a combination where I can validate using the Source and passing an array of ruleset!!!!
     // There isnt an overload for string[]ruleset!! 

       string onlyOneRuleset="DefaultRule";
       Validator<T> validator = ValidationFactory.CreateValidator<T>(onlyOneRuleset, source);//Ideally I would like to read from config and passing both an array of rules + source
       return validator.Validate(entity);

Am I missing the obvious?

Thanks a lot!!!




Sep 25, 2008 at 2:12 PM


The overloads in the Validation class that take multiple rulesets simply aggregate multiple calls to the single-ruleset overloads. You can see this in the implementation for Validation.Validate<T>(T target, params string[] rulesets) and replicate it in your class.

Hope this helps,

Sep 25, 2008 at 7:02 PM
Edited Sep 25, 2008 at 7:09 PM
I just had to look at they way you guys did it.
This is "my version" Allows you to pass multiple ruleset and and use a Config Source. I hope it's correct



static ValidationResults Validate<T> (T target, string validationConfigPath, params string[] rulesets)
       IConfigurationSource source = new FileConfigurationSource(validationConfigPath);
       ValidationResults resultsReturned = new ValidationResults();
(string ruleset in rulesets)
            Validator<T> validator = ValidationFactory.CreateValidatorFromConfiguration<T>(ruleset, source);
            ValidationResults results = validator.Validate(target);
            foreach (ValidationResult validationResult in results)
return resultsReturned;

Thanks again for your invaluable help.




Sep 30, 2008 at 2:13 PM


It looks fine. Does it work as you expected it to work?