Swapping A Child Object Between Two Collections

Swapping A Child Object Between Two Collections

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


NickTaylor posted on Friday, December 10, 2010

Hi,

I wonder if anyone can tell me if this is possible please ?

I have a parent business object ( based on BusinessBase ) which in turn contains two child collections ( each based on BusinessListBase ).

In this particular scenario I would like to be able to swap objects from one collection to another without marking the object as dirty which in turn provokes a delete when the parent business object saves. This is mainly for cosmetic reasons as the object needs to appear in the right collection following a save of the parent.

My code is something like:-

 

 

 

Collection1.RaiseListChangedEvents =

 

false;

Collection2.RaiseListChangedEvents = f

 

alse;

Collection1.Remove(myObject);

Collection2.Add(myObject);

Collection1.RaiseListChangedEvents =

 

true;

Collection2.RaiseListChangedEvents =

 

true;

Running this code marks the child object as dirty and then tries to remove it.

Is there any way I can make the swap without this happening ?

Many thanks,

Nick

RockfordLhotka replied on Friday, December 10, 2010

There's some information in the FAQ on moving objects from one editable list to another

http://www.lhotka.net/cslanet/faq/CslaObjectFaq.ashx

 

NickTaylor replied on Saturday, December 11, 2010

Hi Rocky,

As always, many thanks for your response.

I must confess I'm struggling to see the solution in the article suggested, although I do understand what you are saying. I guess I am fighting the framework here and that's something I wish to avoid!

I have also realised that the Add part of my scenario also marks the object as "IsNew" which is also technically incorrect in this scenario.

I suspect what I am trying to do isn't quite right. Trying to move objects on the fly before a save takes place is probably going to end up with a messy workaround right ?

Perhaps the easier ( and technically correct ? ) solution would be to go ahead and perform the save and then repopulate the two child collections at the end of the DataPortal_Update code ? 

Many thanks,

Nick

RockfordLhotka replied on Saturday, December 11, 2010

This can be complex, and I think the key is to understand what "move an object" is modeling.

If you were doing this with forms in two stacks of paper, how would it be done? Or if these were widgets in a warehouse that you were moving from one bin to another, how would it be done?

Objects should model the real world - they are simulating some real-world business operation or task after all.

That doesn't always help, but sometimes it does - in terms of coming up with a meaningful solution for implementing the objects.

NickTaylor replied on Tuesday, December 14, 2010

Many thanks Rocky. I think for now I will take a different approach to this.

The need to switch objects was only really for cosmetic reasons, and for now, I will look to re-load the collections after the update has completed.

As always, I appreciate your input.

With many thanks,

Nick

Copyright (c) Marimer LLC