Move Collection child to another collection

Move Collection child to another collection

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


Gravity00 posted on Wednesday, August 11, 2010

Whats the best way to move a child of a collection to another collection?

 

 var  newX = x.Clone();

xCollection.Add(newX);

 

OR

var newX = xCollection.AddNew();

but what to do with x?

 

RockfordLhotka replied on Wednesday, August 11, 2010

This is in the FAQ: http://www.lhotka.net/cslanet/faq/CslaObjectFaq.ashx

 

Gravity00 replied on Thursday, August 12, 2010

Thanks.

I'm actually doing a copy. Is it true that Add(X x) and AddNew() differ in that 'AddNew' marks the collection as IsDirty and 'Add' doesn't?

 

var newX = x.Clone();

owner.xCollection.Add(newX);

owner is NOT marked IsDirty (xCollection is a CSLA property of owner).

AND

owner.xCollection.AddNew();

owner IS marked as IsDirty

 

RockfordLhotka replied on Thursday, August 12, 2010

Add and AddNew are very different.

AddNew causes AddNewCore to execute, which creates a new object and adds it to the collection. This doesn't add your existing object - it creates a new one. Since the new object is actually new, IsDirty and IsNew are both true - so the collection is dirty.

Add accepts a "new" object as a parameter and adds that object to the collection. If the new object has IsDirty=false, then the collection won't be dirty.

The IsDirty property of a collection is entirely dependent on whether any of its child objects are dirty.

Gravity00 replied on Thursday, August 12, 2010

"The IsDirty property of a collection is entirely dependent on whether any of its child objects are dirty."

Thanks, that's the bit I missed!

However, I noticed that when I removed a child form the collection the collection became IsDirty

How come?

RockfordLhotka replied on Thursday, August 12, 2010

This is because the removed child is marked for deletion (so IsDeleted and IsDirty are true) and it is moved to DeletedList. When that collection is saved, that child is deleted from the database.

Copyright (c) Marimer LLC