LoadProperty OnChildChanged not working

LoadProperty OnChildChanged not working

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

vbbeta posted on Thursday, July 21, 2011

Hey guys i have 2 problems both is the same but they're still 2 problems



1). When a change the value of a property (which is  a collections of objects) it does not trigger any rules to run, this is the first problem why the rule doesn't run when the value changes - unless I'm thinking because that property is a readOnly property - ??


2). so in order to fix the first problem what i have done is, in the OnChildChanged method I did a local variable to calculate the Total of items in the collection object here is the code


double _total = (from items _Item........................................).sum

LoadProperty(totalProperty, _Total)

but ....... the property does not change the value unless i'm doing like this


Toatl = _toal;


and then the rules start to trigger 1 after the other

can someone explain how i need to get this done




RockfordLhotka replied on Thursday, July 21, 2011

The LoadProperty method doesn't run any rules. It is intended to be a light weight way to set the "field" for the property, without running any rules. Assuming a private backing field for a property, LoadProperty is basically the same as this:

_myBackingField = newValue

The SetProperty method runs rules. It is intended to set the property, after checking authorization rules, and then it runs business/validation rules, and then it marks the object as dirty, and raises the PropertyChanged event.

vbbeta replied on Thursday, July 21, 2011

Thanks for the reply, i guess i did not describe my self good my problem is when i'm changing the propertyValue through the LoadProperty method from the base class for some reason it don't change the value to the UI field - i'm using WinForms -; in short my question is why is the UI not responding to the LoadProperty method and it doesn't meter  if im calling through the a business rule like traget.LoadProperty or in the OnChildChange method,


the only way it works if setting the Property it self Like SaleTotal = _MyBackingField, is there any reason why this should be. thanks in advance for you'r reply 

RockfordLhotka replied on Thursday, July 21, 2011

The UI updates due to data binding, and data binding listens for the PropertyChanged event to know that the value has changed. Because LoadProperty doesn't raise PropertyChanged, it won't update the UI.

vbbeta replied on Thursday, July 21, 2011

But 1 minute does not the "Csla.Rules.RuleContext" AddOutValue property need to change the UI? 

and why is it so if the collection is being changed, it does not trigger any business rules, and i need to call it manually  like BusinessRules.CheckRules(SaleTotalProperty)

my code is here 

 Protected Overrides Sub Execute(ByVal context As Csla.Rules.RuleContext)

                Dim _Action As SalesItemsAction = context.Target

                Dim summary As Double = (From item As SaleItem In _Action.ReadProperty(ItemsProperty) Select item.PricePaid).Sum

                context.AddOutValue(PrimaryProperty, summary)   -----------------   why does this not refresh the UI

  End Sub

RockfordLhotka replied on Thursday, July 21, 2011

When rules are automatically run, they are run by a method in BusinessBase that does automatically raise PropertyChanged for all affected properties.

You can trigger this behavior by calling the protected method named PropertyHasChanged in an editable business class. This will trigger all appropriate processing for a changed property - just like SetProperty does for you. In fact, SetProperty calls PropertyHasChanged to run all appropriate processing.

Also, you might notice that BusinessRules.CheckRules() returns a List<string> - which is the list of affected properties. You can use this to raise PropertyChanged for all those properties. You can (should?) do this if you choose to avoid the automatic processing of SetProperty and instead do your own thing by calling LoadProperty where you still want rule processing to occur.

skagen00 replied on Thursday, July 21, 2011

We've done a couple things in this area -

In our base class, we have introduced "SetPropertyWithElevatedPermissions".  This basically will set the property without giving attention to CanWriteProperty but it causes rules to run and bindings to take place.  We have "property level permissions" which might prevent the user from manipulating the object but we don't necessarily want the object prohibited from changing its state and causing rules/etc to fire.

We also have used something to deal with "related records" of a main root reflecting changes made while the main root is open.  In this case, we're updating read only information on the main root that is open and so all we want to do is LoadProperty followed by an OnPropertyChanged for that property.

A collegue actually suggested a LoadProperty with some sort of bitted enumeration of "check rules?", "refresh bindings?" etc...

Sometimes SetProperty and LoadProperty isn't entirely what you want to have happen.  You have the ability to introduce variations on those in your classes.


Copyright (c) Marimer LLC