I wonder if someone can tell me where I am going wrong please... ?
I have a simple scenario, i.e. a parent business object based on BusinessBase which in turn contains a child collection based on BusinessListBase which in turn contains a collection of business objects again based on BusinessBase. Think of it as a sales order header object containing a bunch of child sales order lines.
I am using Windows Forms for the UI, and obviously when the user edits a line quantity or unit price, or adds and deletes child lines, I want the new sales order value to be reflected in the parent objects properties. Naturally I want this to be done within the business logic and not have to do any of this in the UI.
Therefore I need to pick up when there is a change in the child collection, and re-calculate the order total in the parent object. This should be really easy.
I assume I should be able to hook into the ChildChanged event of the child collection and that should be it.
I am creating the child collection via the lazy load approach as detailed in Rocky's book. I then hook into the ChildChanged event and write an event handler to recalculate the order total.
For some reason the event is not firing when the child collection is changed.
Any ideas as to where I am going wrong ?
What version of CSLA .NET? There were some bugs in earlier versions, but if you are running 3.8.4 or 4.1 this should work.
I am currently running 3.8.1
I'll download the current version and see if that fixes it...
I have downloaded and installed the 3.8.4 version of CSLA.
4.1.0 looks to be a more significant release and this is perhaps a step too far for now.
I'm still not having any luck I'm afraid.
If I'm reading this right, I should be able to hook into the lists ChildChanged event which is triggered by the framework automatically when any of the child object properties are changed. Naturally the child properties will be changed via databinding from the UI.
Am I reading this correctly ?
Regardless of whether you're propery on the parent does the calculate automatically, or it's done in the change event and stored in a field, you will need to ensure you raise a PropertyChanged event for the property. Although you changed the value, databinding won't pick up on this automatically, it will only requery the property value on a PropertyChanged event.
If you have multiple properties, you can raise an event for each, or raise a single event with the property name set to String.Empty. That will trigger ALL of the parent properties to be re-read (which may or may not be what you want).
You might have to help me along a bit more here...
All my BO properties are set up using managed backing fields ( as described on p247 of Rocky's C# 2008 business objects book ).
I presume this will call the PropertyChanged event automatically via the SetProperty() method won't it ?
Where I am struggling is that I can't seem to capture the ChildChanged event when one of the child objects has a property changed.
If I can capture this, I can then perform the recalculation via a simple helper method.
Thanks for your response...
If you're finding the ChildChanged isn't firing at all, it could be that you're event hanlder got lost. When you're object is serialized (which it always will be as it goes back and forth through the data portal), non-serializable fields and events will be lost, so you need to hook into the OnDeserialized to reattach your event handler.
Also, something else to consider... why not just override the OnChildChanged method?
Perfect Andy, overriding the OnChildChanged method seems to work perfectly...
I had not spotted this method previously...
Many thanks for your help!
Glad the issue was resolved. Given that overriding that method seems to work without any problem, I suspect you were losing the event handler when the object was serialized. Even in local data portal mode, Csla serializes your object to create a clone which it works with.
Copyright (c) Marimer LLC