validation rules are implemented as rule methods that conform to the Csla.Validation.RuleHandler delegate. The Validation Rules region implements the AddBusinessRules() method to associate validation rules to properties of the business object.
AddBusinessRules MethodLet’s look first at the AddBusinessRules() implementation:
Code:protected override void AddBusinessRules()
{
ValidationRules.AddRule(
Csla.Validation.CommonRules.StringRequired, "Name");
ValidationRules.AddRule(
Csla.Validation.CommonRules.StringMaxLength,
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 50));
ValidationRules.AddRule(
StartDateGTEndDate, "Started");
ValidationRules.AddRule(
StartDateGTEndDate, "Ended");
}
This method is automatically invoked by the CSLA .NET framework any time validation rules
need to be associated with the object’s properties. The method should only contain a series of
ValidationRules.AddRule() method calls as shown here.
Each call to AddRule() associates a validation rule with a property. In the simple case, this means
associating a rule method like StringRequired to a property like Name:
Code:ValidationRules.AddRule(
Csla.Validation.CommonRules.StringRequired, "Name");
With this done, any time PropertyHasChanged() is called by the Name property, or ValidationRules.CheckRules() is called anywhere in the object, the rule will be applied to the Name property by executing the StringRequired method.
Other rules are a bit more complex, requiring extra parameter values to operate. This is the case
with the StringMaxLength rule, for instance:
Code:ValidationRules.AddRule(
Csla.Validation.CommonRules.StringMaxLength,
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 50));
Notice that in this case, a MaxLengthRuleArgs object is created, supplying both the name of the
property against which the rule is to be run and the maximum length for a valid string.