OnChildChanged when deleting from a child collection

OnChildChanged when deleting from a child collection

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


shawndewet posted on Saturday, March 28, 2009

I have a typical InvoiceHeader / InvoiceDetail scenario.

My InvoiceHeader class has a property called MyInvoiceDetails representing its associated InvoiceDetail records.  I am overriding the OnChildChanged method in InvoiceHeader to handle additions to, changes in, and deletions from MyInvoiceDetails.

It works perfectly for additions and changes.  However, when deleting an InvoiceDetail from MyInvoiceDetails, e.ChildObject represents the InvoiceDetailCollection: I would expect it to represent the deleted InvoiceDetail instead.

Is this by design? If not, I guess I've found a bug.  If indeed it is by design, please advise as to what other approach I should use to trigger business logic associated with the deletion of an item from a child collection in my scenario.

msk replied on Sunday, March 29, 2009

There is an event raised by BusinessListBase called RemovingItem.  You should be able to hook into that. 

Martin.

shawndewet replied on Monday, March 30, 2009

mmm that's a pity the design has to be different for the scenario of removing an item (as opposed to being able to use the overridden OnChildChanged).  Is there any advice as to where the right place is to put the AddHandler for the RemovingItem event?  In the constructor of the parent; In the FromDataReader?

shawndewet replied on Monday, March 30, 2009

Reading through Rocky's book on this topic, it is clear that OnChildChanged is there to be overridden in a Parent class to indicate to the parent what child has changed, and what detail about that child has changed, so that business logic can be executed accordingly.

This "indication" of the change is indeed done perfectly in the ChildChangedEventArgs parameter when adding to and changing items in a child collection.  I therefore can't help but feel that it is not by design that when deleting a record from the child collection, all that ChildChangedEventArgs reports is the changed child collection, without any indication of which item was deleted from the collection.

ajj3085 replied on Tuesday, March 31, 2009

Actually I think that is on purpose.  You can hook up an event listener for RemovingItem, which will fire before the item is removed.  Maybe raising the event via ChildChanged would be considered redundant.

Copyright (c) Marimer LLC