BrokenRulesCollection
Old forum URL: forums.lhotka.net/forums/t/2391.aspx
isharko posted on Tuesday, February 20, 2007
Hi,
In my BO object I have a business rule that don't depend on any business property in my object.
If this rule is broken I still need to make my object Invalid.
What is the proper way, if any, to add the broken rule that would set IsValid = false and that is not dependant on any property in the object.
Or may be I am missing something here.
Thank you,
Igor.
hurcane replied on Tuesday, February 20, 2007
Something seems wrong with this design. Nevertheless...
Business rules are associated with properties to automatically invoke the business rule logic when the property changes. Presumably, the change in the property could affect the business rule. This is why our Property setters need a PropertyHasChanged call.
When you add a business rule, I don't think that CSLA.NET validates the property name you assigned. You could make up a property name, such as "ExternalValidator".
The one problem that will need to be addressed is how does your business object know when the rule should be reevaluated? If the rule is not dependent on any internal properties, it must be dependent on properties from other objects. How do you know when the the state outside of your object has changed, so you can reevaluate the rule?
If you have such events/triggers available, you can call Validation.CheckRules("ExternalValidator") when the external state changes and your business rule might be affected.
But I still think this design has a bad "smell.". Without knowing more details, I can only guess why you are taking this approach.
It seems like the business rule belongs in a different object. Your main object should probably check the state of the external object in an override of the IsValid method.
Perhaps you're having an issue with exposing the business rule to the UI when the business rule is contained in an internal object? This can be solved with an override of the BrokenRulesCollection property, where you build and return a collection with rules from your primary business object as well as external objects that need to be exposed. Did I make a good guess?
xal replied on Wednesday, February 21, 2007
You can either add the rule and then call AddDependentProperty for each of the properties, or you can override OnPropertyChanged and call ValidationRules.CheckRules("MyRule").
Andrés
isharko replied on Wednesday, February 21, 2007
Hurcane and xal:
Thank you very much for your responses.
I am developing rating engine for generating insurance quotes.
BOs set up is as follows:
WorkSheet (contains CalcSheet object below as business property)
CalcSheet
FactorGroup list ---> FactorGroup
Factor List ---> Factor
Factor Option list --> Factor Option
As you can see there are several levels of parent-child relationships.
In CalcSheet object I have a function "CalculatePremium" that uses its property BasePremium and
multiply all the factors to calculate Premium. Each time factorvalue is changed the premium is recalculated.
Each factor_value have to be within its range ex. 0.75-1.25
WorkSheet has a function("Apply700rule") that have to modify the factor value of one factor (withing its range) in one percent increments until The Premium reaches $700.If the factor max range is reached before we get to 700 I have to create a broken rule and make worksheet invalid.
What is the proper broken rules handling in this scenario.
I appreciate your help.
Igor.
xal replied on Thursday, February 22, 2007
Assuming I understood correctly what you're trying to do, you could have a "_currentFactor" variable and have the rule check up on that at the end of the process.
Like, (very pseudo codey) ;)
Private Sub Apply700rule()
_currentFactor += .1
....<Recalculate what you need here>
ValidationRules.CheckRules("ValidateFactor")
End Sub
Inside the rule you'd do:
If target._currentFactor > 1.25 Then
e.Description = "Factor too high"
Return False
Else
Return True
End If
Andrésisharko replied on Thursday, February 22, 2007
Makes sense.
Thank you for your help, Andrés.
isharko replied on Thursday, February 22, 2007
Copyright (c) Marimer LLC