Suspicious Business Rule behavior

Suspicious Business Rule behavior

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


Ranjini posted on Tuesday, July 19, 2011

Technologies – CSLA 4/Silverlight 4

Here is my scenario.

Object A (BusinessBase)

|

----------- PropertyB (typeof(Boolean))

----------- PropertyC (typeof(ChildListC) BusinessListBase)

 

When property B is set to true, I require C to contain at least 1 child.

 

To implement this,

1)   I implemented a rule on the Property B     

 

BusinessRules.AddRule(new CheckCSettings(_PropertyB));

 

2)       Implemented OnChildChanged on ObjectA to capture change in ChildList C

 

protected override void OnChildChanged (Csla.Core.ChildChangedEventArgs e)

        {

            base.OnChildChanged(e);

            if (e.ChildObject.GetType() == typeof(ChildListC) && e.CollectionChangedArgs != null)

                BusinessRules.CheckRules(_PropertyB);

        }

 

3)       Implemented the CheckCSettings Rule as follows

 

        public class CheckCSettings: Csla.Rules.BusinessRule

        {

            public CheckCSettings (IPropertyInfo primaryProperty)

                : base(primaryProperty)

            {

 

            }

            protected override void Execute (RuleContext context)

            {

                var ctx = context.Target as ObjectA;

                if (ctx!= null)

                {

                    if (ctx.PropertyB.GetValueOrDefault(false)

                        && (ctx.PropertyC != null &&

                        ctxDomain.PropertyC.Count < 1))

                    {

                        context.AddErrorResult("Atleast 1 child C is required.”);

                    }

                }

            }

        }

 

The rule seems to get triggered correctly when I change the Boolean flag or when I add/remove child objects from PropertyC. However, when the rule is executed in the latter situation, something seems to not reach completion.

 

Eg, when PropertyB is set initially, the rule runs and correctly invalidates the property indicating that a child is required. However, when I add a child, the rule runs again, and even though the child count is 1 and the context.AddErrorResult line is never hit, the UI still shows the error adorner around Property B indicating it is invalid. The Save button that I have that is bound to Viewmodel CanSave is also disabled.

 

Here is how I have bound my UI –

<Button Content="Save" Name="SaveButton" DataContext="{Binding ObjectAVM}"

 IsEnabled="{Binding Path=CanSave}"/>

 

<CheckBox x:Name="chbPropertyB" IsChecked="{Binding ObjectAVM.Model.PropertyB, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, FallbackValue='False'}" />

 

What I find inconsistent/strange is that when I try to capture the object status at this point, this is what I see –

 

https://docs.google.com/leaf?id=0B-Qwbn69ZX90ZGQ4OTZjYjEtYmFhOS00NWE3LThlZTgtNGY4OWZmZmU3ZTM2&hl=en_US

 

IsValid is true, IsDirty is true, IsBusy is false, but CanSave is false. How is this possible?

 

Please help. Any thoughts /comments are welcome.


Thanks

Ranjini Suryanarayanan

 

JonnyBee replied on Tuesday, July 19, 2011

It is not suspicious at all!!

BusinessRules.CheckRules(property) does NOT raise OnPropertyChanged to notify UI of changes to property (and rules). It only does the rule checking.

You should rather call <BO>.PropertyHasChanged(property) as this will

or implement OnPropertyChanged in your own eventhandler.

Something like this:

protected override void OnChildChanged (Csla.Core.ChildChangedEventArgs e)

        {

            base.OnChildChanged(e);

            if (e.ChildObject.GetType() == typeof(ChildListC) && e.CollectionChangedArgs != null)
                        {
                PropertyHasChanged(_propertyB);
            }

        }

Ranjini replied on Tuesday, July 19, 2011

Aaaah! Thanks so much! That does work perfectly!

I still find the object status inconsistency a little puzzling. Is there an explanation for that?

Thanks!

Ranjini

JonnyBee replied on Tuesday, July 19, 2011

We had a few bugs in PropertyStatus that I worked on in May so you should grab the latest PropertStatus.cs from trunk and update your CSLA 4  version if you experience other inconsistencies.

Basically - it may fail to attach to the correct "CurrentItem" when databaound to a list or miss on updating the PropertyStatus if a control is not visible on the first run, f.ex in a hidden Tab control page (ie: control is not yet part of the VisualTree and property status is unable to set the icon/message, believes everything is OK and did not try to refresh when control became visible).

And - all updates via Databinding requires that some event is triggered to notfiy UI controls.

 

 

 

Ranjini replied on Tuesday, July 19, 2011

Thanks so much for your quick responses! We rely on this forum a lot and you guys make it so easy !

Ranjini

Copyright (c) Marimer LLC