Question regarding IsValid & CheckRules

Question regarding IsValid & CheckRules

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


mightymiracleman posted on Wednesday, September 17, 2008

I'm a little fuzzy on the relationship between the IsValid property and the CheckRules method.

Could someone let me know if I'm correct? From what I can tell, the default behavior of IsValid is to check the count on the broken rules collection; if it's > 0 then it's false. BUT, it looks like the CheckRules is not called anywhere in the IsValid property.

Is this right? If I want to force a call to CheckRules when I refer to IsValid, do I need to override the IsValid property?

I'm just wondering how the B.O. knows it's valid/not valid on a Save if no properties were changed. I know the save checks the IsValid property, but it looks like the CheckRules isn't called there, from what I can tell, it looks like the CheckRules method is only called when a property changes.

Thanks in advance,

Tom

kenb replied on Wednesday, September 17, 2008

Rules are checked when a property is set.  If I set property X then the rules which apply to property X are checked.

A newly created business object, by default, does not check its rules.  If you want this checking to occur then you need to add the code yourself (possibly in the MyBO.NewMyBO static method/shared function).

rwilkerson replied on Wednesday, September 17, 2008

I have a similar question. In my case the object has a requirement that the name be unique in the table. ie. UserName.

Here's the situation:

1. User1 begins the process of adding a record and enters a valid (unique) username.

2. User2 (on another PC) also begins the process of adding a record and enters the same username.

Validation passes for both users while editing.

3. User2 saves the record. At this point User1's object WILL be invalid, but does not know it because the validation rules haven't been rechecked.

4. User1 attempts to save record. The Save will fail because validation rules. User1 gets the original object back (because we cloned before the save) but it's still valid because the validation rules still haven't been checked.

Right now we're thinking we'll expose a method on the business object to allow running the Validation.CheckRules() method on demand.

Is this the "right" way to do it?

rsbaker0 replied on Wednesday, September 17, 2008

rwilkerson:
...4. User1 attempts to save record. The Save will fail because validation rules. User1 gets the original object back (because we cloned before the save) but it's still valid because the validation rules still haven't been checked...

Actually, unless you called CheckRules yourself during the Save(), wouldn't this fail because of a unique constraint or primary key violation in the database?

Anyway, I think the idea of a public method to force an object to call CheckRules() is certainly reasonable.

Also, per the original post, calling CheckRules() once when an object is first created and then letting the automatic rule execution during property changes will handle most cases (except those like this particular one where the breakage of the object actually occurs because of something external to it)

JoeFallon1 replied on Wednesday, September 17, 2008

1. I always call ValidationRules.CheckRules at the end of DataPortal_Create and DataPortal_Fetch. This way my BO is in a known state and then as changes get posted by the user it can end of valid (or not.) Additionally, sometimes I have extra rules that I want to re-check before I determine if the BO is valid so I override IsValid and call a Sub like CheckExtraRules which has  a bunch of ValidationRules.CheckRules("someProperty") in it. I have a web app and once all the fields are unbound into the BO I sometimes want to check the state of all of them. e.g. If I have 5 checkboxes and the rule is that one of them must be checked, I do not put the rule into each of the 5 properties, I assign it to a "dummy" property and then check it once after all the values are set.

2. "Right now we're thinking we'll expose a method on the business object to allow running the Validation.CheckRules() method on demand.  Is this the "right" way to do it? "

I would probably override IsValid and add the call to Validation.CheckRules()  as the first line of code. Then the UI can always "just ask" if the BO.IsValid. That can be expensive though. See #1 for the special calls I make. I don't usually call a blanket Validation.CheckRules().

Joe

 

Copyright (c) Marimer LLC