Is it possible to navigate between rule sets?

Topics: Validation Application Block
Oct 7, 2008 at 6:34 AM
In my case I need to navigate between different rule sets of a certain type, getting the rules defined on rule A (for example) and use it in rule B (also an example)
Below is a common scenario where I have to do this:
1.Suppose I have a class called User which represents a table in a relational database
public class User
{
    public int Identity { get; set; }

    public string Username { get; set; }

    public int Age { get; set; }
}

2.I have defined a rule set on type User called "WhenInsert", the rules are
  (1)Username should not be null or empty and shoule be shorter than 20 characters
  (2)Age should be positive and be less than 120

3.Not I need another rule set when the entity is updating called "WhenUpdate", however the rules of property "Username" and "Age" when update is exactly the same as those when insert, so I dont want to set rules again, I will be very glad if I can navigate to the ruleset "WhenInsert" here and validate the user

4.Besides rules set in ruleset "WhenInsert", the "WhenUpdate" ruleset should contain an extra rule
  The Identity should be positive (this is majorly because the identity is generated by database so it is not required when insert, but when update I have to check the identity value in order to find the row in table)
so what I want is:
  (1)Validate the entity using ruleset "WhenInsert"
  (2)Validate Identity value using my defined rule

so is it possible to do so, getting out the "WhenInsert" ruleset and use it in "WhenUpdate" ruleset?
Oct 8, 2008 at 3:53 AM
You can use the Validation.Validate method.  It has 2 overloads, one of which allows you to pass the rulesets you want to use. 

User user;
.....
ValidationResults results = Validation.Validate<User>(user, "WhenInsert", "WhenUpdate");