CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • What is the validation pattern that ensures a unique combination of 2 (or more) property values?

    rated by 0 users
    Answered (Verified) This post has 2 verified answers | 3 Replies | 1 Follower

    Top 50 Contributor
    135 Posts
    stefan posted on Wed, Jan 26 2011 7:34 AM

    Given:

    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.

    Question:

    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?

    Stefan

    Answered (Verified) Verified Answer

    Top 10 Contributor
    2,279 Posts
    Verified by stefan

    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.

    Jonny Bekkum, Norway CslaContrib Coordinator

    Top 10 Contributor
    2,279 Posts
    Verified by stefan

    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():

    • if field1 or field2 was changed set field3 to composite key 
    • if field2 was changed call CheckRules("field1")

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

    Jonny Bekkum, Norway CslaContrib Coordinator

    All Replies

    Top 10 Contributor
    2,279 Posts
    Verified by stefan

    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.

    Jonny Bekkum, Norway CslaContrib Coordinator

    Top 50 Contributor
    135 Posts
    stefan replied on Wed, Jan 26 2011 8:10 AM

    I need to implement it with 3.8.x.

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

    Stefan

    Top 10 Contributor
    2,279 Posts
    Verified by stefan

    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():

    • if field1 or field2 was changed set field3 to composite key 
    • if field2 was changed call CheckRules("field1")

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

    Jonny Bekkum, Norway CslaContrib Coordinator

    Page 1 of 1 (4 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems