Not Calling Rules On Save

Not Calling Rules On Save

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


BuckeyeJeff posted on Tuesday, December 02, 2008

I am using CSLA, latest version.  I have created some basic business rules and I am trying to update the object with changes in UI and then set values to object and then call save.  I would think on the save, rules would be checked and an exception would be thrown. I am having to call                 ValidationRules.CheckRules(); on _Update to get rules to run.

Current way I am having to do it:

obj = BLL.GLEventInfo.GetGLEvent(Me.GLEventList.SelectedEvent.GLEventID)
                Csla.Data.DataMapper.Map(Me.GLEventList.SelectedEvent, obj, New String() {"IsEstimatedYesNo"})
                Me.PopulateBOFromControls(obj)
                obj.Save()
                If obj.BrokenRulesCollection.Count > 0 Then

                End If

rsbaker0 replied on Tuesday, December 02, 2008

It's largely up to you how you want to implement this.

A common pattern is to call ValidationRules.CheckRules() when the object is *created*, and so the process of setting properties can fix any broken rules if the proper values are supplied. This should avoid the need to call CheckRules() again when the object is saved.

It's also possible to associate rules with a "pseudo" property (e.g. CSLA doesn't care if the property name associated with a rule is actually a real property or not) to defer processing of those rules until save time. Then you can call ValidationRules.CheckRules("pseudopropertyname") in your Save method to fire only these special server side rules.

MarkD replied on Friday, February 27, 2009

I do call CheckRules on DP_Create, but is it acceptable to call it on DP_Fetch for when an existing system has new rules added after records have already been saved.

It does seem unnecessary to call CheckRules on fetch 99% of the time, but what are your thoughts on how to implement new rules on a system?

Thanks,
MarkD

ajj3085 replied on Friday, February 27, 2009

Yes, this is a valid thing to do whenever you can't be sure data in the database meets the current rules.  I wouldn't worry about the call; if it's possible rules won't be met on an object because they are new, you really don't have any other option.

Copyright (c) Marimer LLC