Business Logic implementation question

Business Logic implementation question

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


Cosmin posted on Tuesday, April 22, 2008

Hi,

I've got a root object with 2 child collections. Whenever I update the first collection I need to adjust the second collection as they are related somehow. The first collection configures the layout of the other in the sense that if in the first collection I have 5 items for example in the second one I must have 5 also. Some other logic is there.

What's the best way to do it? Is there any event I can hook in the first list? Where should I write the code to synch the lists, in the first list or in the root object?

Thanks,
Cosmin.

RikGarner replied on Tuesday, April 22, 2008

Do you need to synchronize the two collections immediately upon an update or can you defer it to when the whole graph gets committed? If you can wait then the best thing is probably to override the  Save method on the root object and do the work there.

If you need immediate sync then it's trickier. I've used a couple of techniques - you can hook into the ListChanged event of your child collection but you don't get much information from it. In the end I hid the Add event of my child class (using the new keyword in c#), delegated the call to the base class (base.Add(item)) and then could do the sync work after that.

Cine replied on Wednesday, April 23, 2008

I implemented it using events, where the two lists listen to the events on a third list and then sync using the events coming from the third list.
Did not completely finish yet though, so my approach may or may not actually work :)

Fintanv replied on Thursday, April 24, 2008

When I have run in to this requirement in the past, I have used a mediator object whose only job is to handle the syncing of the two collections.  This mediator would live in the parent root object.  It registered for the appropriate events from each child collection, and handled the update logic.  What I liked is that the mediator class has a single responsibility, and the implementation is nicely encapsulated.  In my particular case I was using the ActiveObjects code and CSLA 1.4, but that is merely an implementation detail. 

Best Regards,

Fintan

Copyright (c) Marimer LLC