What is the validation pattern that ensures a unique combination of 2 (or more) property values?

What is the validation pattern that ensures a unique combination of 2 (or more) property values?

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

stefan posted on Wednesday, January 26, 2011


Two properties; both are required, and the combination of values must be unique.

I want...

- to check for uniqueness whenever one of the two prop values changes

- that the check for uniqueness only runs once when calling CheckRules()

- that both, or at least one, of the props is able to notify a UI if the check for uniqueness fails

Heading for a solution:

Both properties get assigned a 'required'-rule, at priority 0.

I create a third property prop3 as a dummy, and tell validation that prop3 is a dependent property on prop1 and prop2. Then I assign the check-for-uniqueness-rule to prop3, with priority 1.


1. Does the check-for-uniqueness-rule run only once when CheckRules() is called?

2. Can I somehow ensure that, with the above setup, the check-for-uniqueness-rule runs last?

3. How can i achieve that the broken check-for-uniqueness rule is displayed on prop1 and/or prop2?

4. Is there a way in Csla 3.8.x?

JonnyBee replied on Wednesday, January 26, 2011

Is the questions for Csla 4 or Csla 3.8 or both?

The new BusinessRules in Csla 4 provides more options that the ValidationRules in Csla 3.x.

I will assume Csla 4 for my reply:

1  I would create 3 properties with field3 being the "unique"  field.
2. I would create a "SetUniqueField" rule to set the unique value in field3 and attach the rule to both field1 and field2
3. I would attach the "uniqueness" rule to field3
   This rule may set the error message on just one or both properties (field1 and field2) with
   context.AddErrorResult(<IPropertyInfo>, errorMessage)  and could do the
   check for both required and unique. You control the flow inside the rule.

4. I didn't take csla 3.8.x into consideration here. This is much easier to solve in Csla 4.

stefan replied on Wednesday, January 26, 2011

I need to implement it with 3.8.x.

But knowing how to do it in 4.x would surely help too.

JonnyBee replied on Wednesday, January 26, 2011

A little more tricky to resolve all requirements ...

1. Create 3 separate managed fields, field1 to field3
2. Attach format rules (ex required rule) to field1 and field2.
3. Create uniqueness rule and attach to field1.
4. Override "OnPropertyChanged" and add code after base.OnPropertyChanged():

You would only get the uniqueness error message on field1 and the uniqueness rule will only execute once when you call CheckRules().

Copyright (c) Marimer LLC