Cannot use Validation using FileConfigurationSource and string[]Rulesets

Topics: General discussion, Validation Application Block
Sep 24, 2008 at 4:37 PM
Edited Sep 24, 2008 at 4: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  

public

 

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 3:12 PM

Hi,

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,
Fernando

Sep 25, 2008 at 8:02 PM
Edited Sep 25, 2008 at 8:09 PM
Silly,
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

public 

 

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

Thanks again for your invaluable help.

 

 

 

Sep 30, 2008 at 3:13 PM

Hi,

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

Fernando