Clone & IsNew

Clone & IsNew

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


DivisibleByZero posted on Tuesday, May 12, 2009

I have a child object that I want to make a copy of so I am calling the Clone method. How do I mark this newly cloned object as a new object and make sure that all of its children and its childrens children, etc. are marked as New, so that they get inserted into the database when I do a Save()?

ajj3085 replied on Tuesday, May 12, 2009

Clone by itself won't help you do this. You'll need to write code that does the clone and then sets the newly created object's state correctly. So you'll probably need an internal method on the class which is being cloned.

JoeFallon1 replied on Wednesday, May 13, 2009

I agree with Andy.

What I usually do is create a New instance of the same BO Tyoe and then use the DataMapper class to move only the data that I want from the original BO to the "clone". You can include a list of excluded Properties to avoid copying things like the PK. That way the new BO has the correct state and will Insert for you instead of Update.

It is only a few more lines of code to do the exact same thing to a child collection and all its objects.

Joe

Copyright (c) Marimer LLC