开发者

Enterprise library Validation block and rulesets

I am using Rulesets on a type that looks like this:

public class Salary
{

   public decimal HourlyRate { get; set; }

   [ValidHours]  //Custom validator
   public int NumHours { get; set; }

   [VerifyValidState(Ruleset="State")]  //Custom validator with ruleset
   public string State { 开发者_如何学Goget; set; }
}

Due to business requirements, I'd need to first validate the ruleset "State" and then validate the entire business entity

public void Save()
{
   ValidationResults results = Validation.Validate(salary, "State");

   //Check for validity

   //Now run the validation for ALL rules including State ruleset
   ValidationResults results2 = Validation.Validate(salary); //Does not run the ruleset marked with "State"

}

How do I accomplish what I am trying to do?


You will need to add VerifyValidState to both RuleSets:

public class Salary
{

   public decimal HourlyRate { get; set; }

   [ValidHours]  //Custom validator
   public int NumHours { get; set; }

   [VerifyValidState]  
   [VerifyValidState(Ruleset="State")]  //Custom validator with ruleset
   public string State { get; set; }
}

Then you can invoke each RuleSet separately (which you were already doing). The code will look like:

public void Save()
{
    ValidationResults results = Validation.Validate(salary, "State");

    //Check for validity
    if (results.IsValid)
    {    
        //Now run the validation for ALL rules including State ruleset
        results.AddAllResults(Validation.Validate(salary)); 
    }    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜