Another one on Business Rules...

Another one on Business Rules...

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


guyroch posted on Monday, March 03, 2008

What is the best way to have one broken rule for the following...

I have 3 properties and if one has a value then all tree are mandatory.  It's either all or nothing and I need to display only ONE broken rules, not 3.

 

JoeFallon1 replied on Monday, March 03, 2008

For rules like this I write them inside the class itself (as opposed to my Common Rules class).

I assign the rule to a non-existant Property. This means it never gets called from the UI or from PropertyHasChanged("SomeRealProperty").

Once the entire BO is unbound and all properties are set the UI can call IsValid. 

This is where I typically perform ValidationRules.CheckRules("FakePropertyName"). (No need to check all rules here - only the one(s) you assigned to fake properties.)

And now the rule can check the current value of all three properties and the rule only runs one time.

Then I call MyBase.IsValid and the correct results are returned.

This is a common paradigm in a Web app - if you want more interactivity then you have to call the rule more often. Perhaps AddDependentProperty to each of the three real properties and assign the fake property as the dependent. This way it gets called three times and is immediately available after each property is changed in the UI.

Just some ideas.

Joe

 

 

 

RockfordLhotka replied on Monday, March 03, 2008

I don't know how you'd get one rule to span three properties? Which one do you mark as invalid?

Ahh, well, you could do something. You could say

if x is not empty AND y and z are empty
  then x is invalid because it has a value but the others don't
OR if x and y are not empty AND z is empty
  then z is invalid because it doesn't have a value but the others do

Kind of a "which of these things doesn't match the other ones" sort of algorithm.

guyroch replied on Tuesday, March 04, 2008

We followed Joe's advice and it's working fine.  No offence Rocky :)

Much appreciated

Copyright (c) Marimer LLC