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:...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)
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