Business rule not working - Jonny Bee's sample

Business rule not working - Jonny Bee's sample

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


griff posted on Tuesday, October 09, 2012

I've just started getting into business rules. I have a rule 'ToUpper' that is executed but does not update the UI.

The  UI does get updated if I update another field though (that also has a business rule).  Is there something I have to do the get the UI to update immediately after the field/property is updated.

I am using Jonny Bee's business rules sample (Csla 4.2.12)  and added this rule

BusinessRules.AddRule(new ToUpper(NameProperty));

public class ToUpper : PropertyRule
    {
      
        public ToUpper(IPropertyInfo primaryProperty)
            : base(primaryProperty)
        {
           
            InputProperties = new List<IPropertyInfo> { primaryProperty };
            AffectedProperties.Add(primaryProperty);
 
            CanRunOnServer = false;
        }

        protected override void Execute(RuleContext context)
        {
            var value = (string)context.InputPropertyValues[PrimaryProperty];
            context.AddOutValue(PrimaryProperty, value.ToUpper());
        }
    }

The rule fires but does not update the Name field until I have updated another field in teh sample.
Can someone please explain/help.
Thanks

JonnyBee replied on Tuesday, October 09, 2012

Which UI platform are you using? WindowsForms og Xaml or another?

1. You should check the setting of CslaPropertyChangedMode in your app.config/web.config.

For CSLA 3.x the default setting is Windows
For CSLA 4.x the default setting is Xaml

This setting determines how the OnPropertyChanged is raised for "AffectedProperties" back to the UI.

2. If it is WindowsForms and the PropertyChangedMode is correct you should add the Csla.Windows.BindingSourceRefresh component and set it to do refresh on that BindingSource as WindowsForms will reread all properties EXCEPT the one that was modified (ie: got OnPropertyChanged). The BindingSourceRefresh will make sure that the value is reread also for the property in OnPropertyChanged.

From what you explain - THE RULE IS WORKING AND DOES UPDATE THE PROPERTY IN THE BO AS IT SHOULD but the UI does not update properly until you change another property (ie: get OnPropertyChanged on another property).  This is a known problem in WindowsForm and is is the reason that CSLA provides the BindingSourceRefresh control.

griff replied on Tuesday, October 09, 2012

Thanks for quick reply.

I am using SL but don't have CslaPropertyChangedMode  set anywhere - what is the full statement and what section does it go under?

Having said that, I am testing the issue in your BusinessRulesDemo - your CalcSum business rules works fine (i.e. the sum is calculated and updated after

num1 or num2 updates) - this does not have an APP.config and CslaPropertyChangedMode is not set - so why does ToUpper not work and CalcSum does?

JonnyBee replied on Tuesday, October 09, 2012

The default PropertyChangedMode of Xaml should be fine in SL.

This is the interesting code in Csla.Core.BusinessBase:

    protected virtual void PropertyHasChanged(Csla.Core.IPropertyInfo property)
    {
      MarkDirty(true);
      var propertyNames = BusinessRules.CheckRules(property);
      if (ApplicationContext.PropertyChangedMode == ApplicationContext.PropertyChangedModes.Windows)
        OnPropertyChanged(property);
      else
        foreach (var name in propertyNames)
          OnPropertyChanged(name);
    }

that call the rule engine and reises OnPropertyChanged to notify UI.

ApplicationContext.PropertyChangedMode may be defined in app.config/web.config or set in code.

I would set a breakpoint on the if statement to verify correct PropertyChangedMode and then check which properties gets a OnPropertyChanged.

When "Name" gets OnPropertyChanged the UI should reread the field value in SL.

 

 

Copyright (c) Marimer LLC