How do I best bubble the ListChanged events back to a root collection

How do I best bubble the ListChanged events back to a root collection

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


rbarna1 posted on Monday, November 02, 2009

I have a linked set of BusinessListBase objects, I'll call them root, child, grandchild where the classes are defined as :
Roots : BusinessListBase<Roots, Root>
Root : BusinessBase<Root>
Children : BusinessListBase<Children, Child>
Child : BusinessBase<Child>
etc...

Each child collection has a reference to its parent object that I set in the OnDeserialized() , such as this in Children...
 private void SetParentOnChildren(Root parent)
        {
            foreach (Child item in this)
                item.RootID = parent.ID;
        }

... and the converse in Root...
     protected override void OnDeserialized(System.Runtime.Serialization.StreamingContext context)
        {
            base.OnDeserialized(context);
            _children.SetParent(this);
        }
 

This app is WPF MVVM.  I am using the Roots_ListChanged() event in my viewmodel to notify the view that the save can execute.  The problem is that these Child collections are not bubbling up their ListChanged events.  I understand that CSLA 3.7 has an OnChildChanged event that *may* be useful (or isDirtyChanged)??  Either way I'm just not wrapping my head around the best way to get that change event in the child objects up to the root parent collection.

Thanks,
Bob


robert_m replied on Monday, November 02, 2009

ChildChanged does just what you need (at least in Windows version of CSLA, I haven't tried it with Silverlight version)

rbarna1 replied on Monday, November 02, 2009

Thanks.

How does it do it?  From what I can tell the event is available on the collection to notify that one of its objects has changed...  Whats the best way to push that up to a parent object (not THE CSLA 'parent', but an independent collection that I have created a _parent reference to)?


RockfordLhotka replied on Monday, November 02, 2009

ChildChanged will cascade up through your entire business object graph, as many levels as it takes, until the root object raises the event.

To my knowledge there is no comparable feature in .NET itself - and I'm sure we'd have found it, because ChildChanged was not easy to implement...

rbarna1 replied on Tuesday, November 03, 2009

Yeah, it sounds great.  There has got to be some plumbing involved with implementing it though.

In my ViewModel's Set of the business object collection I am hooking both the ListChanged and ChildChanged events.  I am not overriding either event in the business objects themeselves. 
The ListChanged event still gets called (followed by the ChildChanged)  when I change an object belonging to the root collection, but neither gets called when an object in the List<Child> property is changed.
In my most recent test I first changed a child object (no events raised), then a root object (event raised) and examined the ChildChangedEventArgs...if I drill down to the child collection it is indeed marked as dirty...its not just not bubbling up this event on its own.

What am I missing?


Thanks!

rbarna1 replied on Wednesday, November 04, 2009

bump.

I really need to know if a particular parent/child property must be set to establish the objects as part of the same graph, or if the events need to be explicitly raised from objects to child collections, or some other plumbing.

Thanks.

RockfordLhotka replied on Wednesday, November 04, 2009

CSLA manages child relationships on your behalf. As long as your child objects are in a collection, or are referenced through a managed backing field as shown in Expert 2008 Business Objects they are child objects and the ChildChanged event should flow up through the object graph to the root object.

Copyright (c) Marimer LLC