MarkDirty called before CheckRules

MarkDirty called before CheckRules

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


ben posted on Thursday, February 28, 2008

I would like to do a small suggestion to improve validations performance.

 

Problem description

 

The IsDirty can’t be used in validation rules because its value is not updated until the validation rules are checked. So if the first property that is edited on a business object is involved in a validation rule then that validation can’t check the IsDirty property because of it will get a wrong value (IsDirty = False).

 

Validations that need executing commands against the database are very expensive. When an object is clean we can avoid checks against the database assuming that all properties are valid in the database (the database constraints get it). Performance can be improved so much.

 

Solution

 

If method BusinessBase.PropertyHasChanged calls MarksDirty before calling CheckRules then the property IsDirty will have the right value when validations are performed.

 

Do the following change to BusinessBase:

 

   <Serializable()> _

   Public MustInherit Class BusinessBase

      Inherits UndoableBase

...

#Region " IsNew, IsDeleted, IsDirty "

...

      Protected Overridable Sub PropertyHasChanged(ByVal propertyName As String)

 

         MarkDirty(True)

         ValidationRules.CheckRules(propertyName)

         OnPropertyChanged(propertyName)

 

      End Sub

...

#End Region

...

   End Class

 

RockfordLhotka replied on Thursday, February 28, 2008

Not a bad idea, but I'm interested to hear if anyone can think of scenarios where this would break their code?

ajj3085 replied on Thursday, February 28, 2008

NT

JoeFallon1 replied on Thursday, February 28, 2008

RockfordLhotka:
Not a bad idea, but I'm interested to hear if anyone can think of scenarios where this would break their code?

I have a lot of BOs and rules and none of them check IsDirty as part of their Validation rules. So this small change would not break my code.

Just to verify, this change really only affects the first Property thatis modified on a BO because after that the BO is dirty. I can't see where this would hurt anything.

Joe

 

 

ben replied on Friday, February 29, 2008

When a business object is edited, after the first property is changed, the object is dirty by definition. If we find that IsDirty property is useful on any validation rule then the IsDirty property should have the right value at the time of performing the validation. I see it as a consistency issue.

 

Setting the IsDirty property to True before calling CheckRules only may affect validations doe to the business object does not raise any notification (event) when MarkDirty is called from PropertyHasChanged.

 

I can’t imagine a situation where it may be problematic this small change, and I am also curious about how could be a use case which would be conflictive with.

 

Thanks every body for your attention.

 

Benjamin

 

RockfordLhotka replied on Sunday, March 02, 2008

OK, I’ve slipped this changed into 3.5.

 

Rocky

stanc replied on Monday, March 03, 2008

I am just looking to get a better understanding of CSLA with this question. How can we get into a validation routine without the object being dirty?

RockfordLhotka replied on Monday, March 03, 2008

You can call ValidationRules.CheckRules() pretty much any time you want – even before the object has been “changed”. For example, if you don’t trust the data from the database (or web service, or wherever you are getting your data) then you might call this method at the end of your DataPortal_Fetch(). The object isn’t dirty at that point, but it might be invalid.

 

Rocky

 

From: stanc [mailto:cslanet@lhotka.net]
Sent: Monday, March 03, 2008 7:51 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] RE: MarkDirty called before CheckRules

 

I am just looking to get a better understanding of CSLA with this question. How can we get into a validation routine without the object being dirty?



JonnyBee replied on Friday, February 29, 2008

Will not brake any of our code as none of our rules check for IsDirty.

/Jonny 


Copyright (c) Marimer LLC