Validation Rules with conditions

Validation Rules with conditions

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


NelsonF posted on Wednesday, August 02, 2006

First thanks to all who have helped me in my previous posts. This place is a God send...
 
My current issue is how to have a rule that depends on other conditions. For example:
 
Scenario 1:
Let's say you're managing some real estate information. So you have a value that indicates the PropertyTypeID where (1=Income, 2=Owner Occupied, 3=Commercial, etc)  now if PropertyTypeID=1 I need to insure that a rule is enforced where a child list IncomeUnitList contains at least 1 entry. If PropertyTypeID=2 then IncomeUnitList must be empty. If ProprtyTypeID=3 then another sibling property NumOfUnits must be populated.
 
Scenario 2:
I have an person object called PersonType that is boolean that indicates is a person is of a certain type (broker, attorney, borrower, etc). I also have another field that indicates the PersonID which will need a different regex validation depending on type (borrower must have a valid social security number, broker must have a broker id, attorney must have a bar #,  etc..)
 
In both scenarios I can quickly code a routine to do all these checks but I'd like to encapsulate them within my CSLA objects directly. Are there some recommendations for accomplishing this?
 
 

hurcane replied on Wednesday, August 02, 2006

I'm not directly answering your question, but have you considered having different objects instead of using the data-centric type code within a single object? You could have separate property objects (IncomeProperty, OOProperty, CommercialProperty). The OOProperty object wouldn't even contain a child list. For your second scenario, you might consider having a Borrower object, an Broker object, and an Attorney object. Each object has their own unique rules.

Having separate objects eliminates your dependency on the "type" variable. For your person scenario, you now have very simple business rules based on simple properties. For example, the Attorney can use the StringRequired business rule on the bar # property.

You would still have dependencies on the child collection. I believe there have been other discussions about creating business rules based on the state of a child collection. I won't rehash that here.

If you still want to go with a single, heavy object, and include a type variable, I would still create separate business rules. Each rule's body would take this form:

If Type <> 1 Then Return True
Evaluate rule that applies to Type 1...

xal replied on Wednesday, August 02, 2006

You can make one rule that has a case statement that validates the stuff you need according to the property type.
Or you could also make three different rules, and each of them would actually run the validation if the PropertyTypeID is the type that rule uses, and otherwise return true...

Andrés

Copyright (c) Marimer LLC