Validation Rule for two Property Strange

Validation Rule for two Property Strange

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


st3fanus posted on Friday, July 26, 2013

Hi all..

I'm learning about Business Rules  Engine on CSLA 4, so I post my another question on http://forums.lhotka.net/forums/p/12091/56013.aspx#56013, thanks jonny :)

I have an example for my real rule which is I don't understand it's behavior.

  public class CardNumberCustomerRule : BusinessRule
    {
        public CardNumberCustomerRule(Csla.Core.IPropertyInfo primaryProperty,
            Csla.Core.IPropertyInfo custProperty)
            : base(primaryProperty)
        {
            InputProperties = new List<Csla.Core.IPropertyInfo> { PrimaryProperty, custProperty };
        }

        protected override void Execute(RuleContext context)
        {
            ........
        }
    } // end of class

 

My Question is : this rule is called twice, which are :

1. when my custProperty on my BO is set , use SetProperty ( this is call FIRST, I don't know why the rule called on this step ? ) Am I wrong if My Understanding at this step is the rule should not be run ?

2. when my PrimaryProperty on my BO is set , use SetProperty ( I Accept, and this is what I want )

 

thanks a lot

 

stefanus

JonnyBee replied on Friday, July 26, 2013

The InputProperties is considered as Dependency.

Take f.ex a rule that validates StartDate and EndDate - and StartDate must be before EndDate. The rule must run when ANY of the input properties is changed to make sure that the validation is correct.

So the basic principle is 

 

st3fanus replied on Friday, July 26, 2013

Hi jonny..

I'm still confusing about this subject, let me give you my understanding currently

1. If every input properties changed then the rule must be run, honestly I don't know about this ( I'll read csla ebook more ), BUT from this conclusion, I have another question about what is the significant availability of primary property ? (currently before your explained, I suppose that rule is run when primary property changed)

 

2. In my example above , what I want to go is :

I want to make a confirmation if my primary property value is exist on database based on custproperty to lookup from database, but I want that this rule is called when my primary property changed ONLY. So on my prev post I ask about IF rule is shared for every instance of Business Object Type =>and I got your explained ( very well :)

Because of this, I doubt that I can received custproperty value on MyRule object private property. I read on ebook that property of business rule can't be changed after first rule alive.

 

Could you give your suggestions ?

 

thanks a lot

 

stefanus

 

 

JonnyBee replied on Friday, July 26, 2013

1. I believe this behavior was added in the 4.2 version - as nearly all the times you had InputProperties then you had to add similar Dependency rules to make sure the validation was correct as per the field values.

2. You may change your rule to inherit from CommonBusinessRule and set CanRunAsAffectedProperty = false. This will exclude the rule from running when the PrimaryProperty is an AffectedProperty of another rule or one of the aggregated InputPropeties for this fields rules has changed.

st3fanus replied on Friday, July 26, 2013

Hi jonny

thanks for your responds, But I still do not understand with your no 2 suggestion,

CommonBusinessRule ? is my own definition rule or from csla base class ?

could you show me an example please ? thanks a lot before

 

 

stefanus

JonnyBee replied on Friday, July 26, 2013

Make your rule inherit from Csla.Rules.CommonRules.CommonBusinessRule

st3fanus replied on Friday, July 26, 2013

Hi jonny..

yes It's worked, I get what I want to do.

But I have to learn more about Business Rule System on CSLA. Currently what I know about CSLA I got it from CSLA ebook series.

what you explained or your suggestion I did not find it from ebook.

 

I think I have to make a conclusion for my self. [ I think I have more questions for this area, but I will arrange that systematically so I can take a conclusion ]

thanks a lot jonny

 

stefanus

JonnyBee replied on Friday, July 26, 2013

You should also read my blog posts about changes to the rule engine in 4.2 and 4.5

See this post for links: http://forums.lhotka.net/forums/p/12087/56017.aspx#56017

st3fanus replied on Friday, July 26, 2013

Nice jonny..

thanks a lot, I'll read your blog soon

 

stefanus

st3fanus replied on Thursday, August 01, 2013

Hi jonny..

I've read your blog about rule engine 4.2.

1. the default value of CanRunAsAffectedProperty is TRUE,

SO => every property in AffectedProperties CHANGED the rule is execute ? Am I right ?

2. Use your blog as an example

If a Rule is called / triggered with change of each property in AffectedProperties, in your example StartDate and EndDate,

why we need add a rule twice for StartDate and EndDate ?

thanks a lot

 

stefanus

JonnyBee replied on Friday, August 02, 2013

1. RuleEngine will rerun rules for affected properties from the "changed" property. Only 1 level down. 

2.You only need 2 rules so that error icon is shown on both StartDate and EndDate and rechecked whenever one of them is changed.

Copyright (c) Marimer LLC