Webmaster Forums Banner Professional Hosting from Just Host
Welcome Guest Search | Active Topics | Members | Log In | Register

Adding Validation Rules of Business Object in CSLA.NET Framework Options
ca4nul
Posted: Friday, May 30, 2008 7:59:53 AM

Rank: Advanced Member
Groups: Member

Joined: 11/9/2007
Posts: 223
Points: 575
Location: UK
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 Method

Let’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.

Sponsor
Posted: Friday, May 30, 2008 7:59:53 AM
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by HaqTech.Com
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 1.697 seconds.