per-type validation and authorization rules in version 2.1

per-type validation and authorization rules in version 2.1

Old forum URL: forums.lhotka.net/forums/t/1400.aspx


dileepagarwal posted on Thursday, October 05, 2006

Hi Rocky,

       In one of your post related to CSLA performance for large collection you mentioned that:

"One thing I'll point out, is that in CSLA .NET 2.0 each object (row) loads its own validation rules, and that will add up to a lot of overhead with 1000+ rows. Version 2.1 will help address this with per-type validation and authorization rules. For your testing, I would suggest starting with no validation or authorization rules in the objects for now."

Is new version address per-type validation and authorization rules ? Right now I am using version 2.0 and want to make use of version 2.1.

Thanks,

Dileep Agarwal

RockfordLhotka replied on Thursday, October 05, 2006

Yes, the biggest change (among many) in version 2.1 is support for per-type validation and authorization rules. I'm working on an ebook to fully explain the changes, which should be out in November - in the meantime you'll have to make do with the change log from the download page (www.lhotka.net/cslanet/download.aspx).

Henrik replied on Thursday, October 05, 2006

Dileep

 

Making the change from 2.0 to 2.1 isn’t that hard as long as you appreciate the options Rocky has provided in the new version.

 

If you have validation rules in your classes in 2.0, these can easily rewritten using the generic AddRule method. The example below is an extract from one of my classes that represents a child in an institution. The validation rule shown, validates that the child’s name has been set. Note that the validationrule is static and is therefore a per-type rule.

 

Protected Overrides Sub AddBusinessRules()

   MyBase.AddBusinessRules()

   With ValidationRules

       .AddRule(Of Child)(AddressOf ValidateName, "Name")

       .AddDependantProperty("FirstName", "Name")

       .AddDependantProperty("MiddleName", "Name")

       .AddDependantProperty("LastName", "Name")

   End With

End Sub

 

''' <summary>

''' Validates that either firstname, middlename or lastname has been set.

''' </summary>

Private Shared Function ValidateName(ByVal target As Child, ByVal e As RuleArgs) As Boolean

   If target._requireName Then

      Dim name As String

       Name = String.Concat(target.FirstName & target.MiddleName & target.LastName).Trim

       If name.Length <= 0 Then

          e.Description = "Child name is required!"

          Return False

       End If

   End If

   Return True

End Function

 

Since the rule is added as a generic rule of Child and the target in the validation method is declared as type Child, you’ll be able to get to the fields of the instance being validated through this target.

 

Also note the AddDependantProperty lines I've added. These instruct the rules manager to validate the rules for property Name, whenever PropertyChanged has been called in either the FirstName, MiddleName and LastName property.

 

Hope this can get you started.

 

Cheers

/Henrik

Copyright (c) Marimer LLC