Where do YOU put your custom validation?

Where do YOU put your custom validation?

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


reagan123 posted on Wednesday, July 30, 2008

Just curious...do you put your custom rules in your BusinessObject class or do you have a single module shared between all of your BO's?

I like the idea of sharing the rules, but i'm not sure what the pro's and con's are for each method.

P.S = If anyone wants to put some of there rules as examples on this thread it would be great :)

awbacker replied on Wednesday, July 30, 2008

IIRC, we mostly put the rules in the biz objects themselves.  There are some standard "types" of rules which we have functions for, such as IsNotBlank, IsBetween, IsGreaterThan, etc., but the *rules* themselves (which field to validate, between what?, etc) are specific to each object.

Rules that involve multiple properties are coded as custom functions inside the object, and added manually at construction time to both properties that need them.

For other rules we use a DevEx "ValidationProvider", and write custom rule classes (they only have one function), and then check that as well as our error provider before saving a form. 

So... personally I would like to store multiple "rule-sets" and simply apply them to an instance, but I don't see a way to do that right now.  Ideally that would include rules like "read only", so one object could be supplied with multiple personalities depending on the situation.

//Andrew


JonStonecash replied on Wednesday, July 30, 2008

I am a consultant, so of course the answer is that it depends.

If the rule is specific to a given BO, I stick it in that BO.  If the rule applies to several business objects, I might created a class, say CommonCustomRules, that holds these rules.  This would be similar to what Rocky has done with the CommonRules class in CSLA. 

This last statement needs to be qualified a bit.  If I am absolutely sure that the rule applies to multiple business objects and that the rule has some weight (it it takes more than two or three lines of code) and that the rule is unlikely to diverge over time, I would put the rule in the common code.  If the rule is common but trivial, I would keep it in the business object; the value to be gotten by sharing the code is outweighed by the hassle of going to a different class to find out the details of the rule.  [Obviously my personal hangup that years of therapy have failed to correct.]  If the code is somewhat more complicated but I can see some trends that would cause divergence, I would reuse via the cut and paste method; that way I would have to think about each business object when I made a change to the rule. 

The more narrowly focused the rule, the more likely that it can be shared.  You can have multiple rules applied to a given property and you can set up the priorities so that the certain rules only execute if other rules have succeeded.  With a little thought, you can have more rules that can be safely shared.

Just some of my thoughts.

Jon Stonecash

 

JoeFallon1 replied on Wednesday, July 30, 2008

I have a set of Common Rules which apply to many BOs.

Otherwise I embed the rule in the BO that uses it. I like this style as the strong typing allows me access to the Fields or Properties of the BO.

Protected Overrides Sub AddBusinessRules()
 
'ActTotal
  ValidationRules.AddRule(Of MyBO, MyRuleArgs)(AddressOf ActTotalDifferentFromComputed, New MyRuleArgs("ActTotal"))
End Sub

Private Shared Function ActTotalDifferentFromComputed(Of T As MyBO, R As MyRuleArgs)(ByVal target As T, ByVal e As R) As Boolean

If target.ActTotal <> CDec(target.ComputedTotal) Then
 
e.Description = "Total amount does not match the computed total."
 
Return False
Else
 
Return True
End If

End Function

Most rules that are embedded are simple like this one.

But there are a few which are *very* large due to the amount of logic required.

Joe

reagan123 replied on Wednesday, July 30, 2008

Awesome feedback!  Thanks everyone... makes sense.
I really appreciate the help


akhirudin replied on Wednesday, August 06, 2008

hallo reagan

just a short addition from me

I've been using CSLAGEN for sometimes, it generated our CSLA object in partial class so we can put any customizing in the non partial class. (it has so many awesome functionaly)

as for the cutom validation rules, I usually create my rules thru this generator and i will set the rule to get the status from a custom function. hoped it usefull

regards

Fahmi

Copyright (c) Marimer LLC