When to call ValidationRules.CheckRules()?

When to call ValidationRules.CheckRules()?

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


mase posted on Tuesday, September 11, 2007

Should I be calling ValidationRules.CheckRules() explicitly?  I'm having an issue where I sometimes get a broken rules collection when it is definitely not broken. 

I am not even really able to reproduce the error, but sometimes when I create a new object and then try to save it to the database, I get a broken rule execption.  But when capturing this bug, I print a bit of debug output and the property that is shown to be broken is not b/c I can see the value is correct, but the rule is shown broken anyway.

From what I understand, the rules are checked when attempting to save or insert, but if I need to check before trying to save, then do I just override IsValid and call CheckRules when IsValid is checked?  Optionally, I could do a CheckRule("Property") in the property setter, right?

Thanks

Marjon1 replied on Tuesday, September 11, 2007

The rules are checked whenever the PropertyChanged() method is called, therefore you calling it manually shouldn't change the behaviour that you are seeing; unless it is a multiple property rule. What sort of rule is being broken? Is it a common CSLA rule or a custom rule? Does it affect one or more than property?

I could see the following causing the behaviour that you are seeing:

I have a rule which states that if PropertyA >= 5 then PropertyB must have a value.

If PropertyB is not added as a dependant property via:
    Me.ValidationRules.AddDependantProperty("PropertyA", "PropertyB")

If you changed PropertyA to be Open, the rule would be marked as broken; if you then changed the value of PropertyB the rule would not fire because it PropertyB doesn't have any rules against it (unless the above code is run). Adding dependant properties in my understanding a better way to do it, rather than overriding the Save method and calling Validation.CheckRules() calling Mybase.Save()

Hopefully this gives you a bit of guideance, however, feel free to post some more details about your rules and what values you are using and I'll try to give you a better answer.

JoeFallon1 replied on Wednesday, September 12, 2007

Most people call ValidationRules.CheckRules as the last step in DataPortal_Create (and Fetch).

This means that once the BO is initially created or fetched it is in the correct state. (Either Valid or not.)

All of your property sets should contain code like this:

Set(ByVal Value As String)
 
If mAcctcode <> Value Then
   
mAcctcode = Value
    PropertyHasChanged(
"Acctcode")
 
End If
End Set

As each property is changed your rules are broken or removed. The BO should always be in the correct state.

In my Web app, I find it useful sometimes to override IsValid and add a call to
CheckRules("SomeSpecialRule"). Where SomeSpecialRule uses more than one field in the BO.

e.g. You have 5 Boolean properties and you want to know if 1 of them is True. Rather than run the rule 5 times as each Property Set occurs, I run it once when I check IsValid. The check for IsValid is usually in the Save button code so I can give the user a message before going through the DataPortal.

Joe

 

 

Joe

Copyright (c) Marimer LLC