New Posts New Posts RSS Feed: Creating a Clone of an Object
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Creating a Clone of an Object

 Post Reply Post Reply
Author
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Topic: Creating a Clone of an Object
    Posted: 21-Aug-2009 at 12:36pm
I recently wrote more about cloning as of the 5.2.2 release of DevForce in another forum post: http://www.ideablade.com/forum/forum_posts.asp?TID=1437&PID=5218#5218
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 23-Mar-2009 at 12:10pm
Somewhere between versions we went from a public virtual Clone() method to an explicit interface implementation.  You must now cast your entity to an ICloneable in order to call Clone, and it's not overrideable.
Back to Top
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Posted: 23-Mar-2009 at 8:46am
Alright I'm a bit confused on this, I'm attempting to follow this post.  I'm using version 4.3.0 and the release notes of version 4.3.0 indicate:
 

Entity.Clone now sets EntityState as detached and allows dynamic recreation of EntityKey. [#839]

 
Fantastic.   But, I can't seem to find Clone() anywhere in the Entity class (or any class that I've derived from it).  I've looked in the Object Browser and went to the definition on Entity, but neither lists a Clone() method at all, let alone one that is overridable.  All I'm seeing is a CloneCore method that is not overridable.  Even more confusing is that Entity derives from the ICloneable interface, which indicates that the Clone() method should indeed still exist.
 
I feel like I'm missing a key step, but not sure what it is.  My references include IdeaBlade.EntityModel.v4, IdeaBlade.Util.v4, and IdeaBlade.Verification.v4.  A shot in the dark, but is there another library I need to be referencing to be able to 'see' the Clone() method?
 
Thanks,
Ken
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 25-Sep-2008 at 9:04am
The problem has been fixed in current bits and will be available in the October release.  We don't have a firm date for this release yet.
Back to Top
pokaragat View Drop Down
Newbie
Newbie


Joined: 15-Sep-2008
Location: US
Posts: 13
Post Options Post Options   Quote pokaragat Quote  Post ReplyReply Direct Link To This Post Posted: 23-Sep-2008 at 8:54am
Do you when this feature be in the build and release date?
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 22-Sep-2008 at 6:37pm
Unfortunately, in DevForce version 4.2.0.1 the Clone capability appears to be broken.  So, your best option right now might be to create a new entity and copy all properties one-by-one from the original entity. 
 
Clone should be fixed in the next release.
Back to Top
pokaragat View Drop Down
Newbie
Newbie


Joined: 15-Sep-2008
Location: US
Posts: 13
Post Options Post Options   Quote pokaragat Quote  Post ReplyReply Direct Link To This Post Posted: 22-Sep-2008 at 10:38am

I tried to override the Clone method:

this.RemoveFromManager();
DATA_ENTRY dataEntry =(DATA_ENTRY) base.Clone();
dataEntry.DATA_ENTRY_ID = 102;
dataEntry.DATA_ENTRY_FROM_DATE = DateTime.Now;
manager.AddEntity(dataEntry);
 
 
It allowed me to modify the fields for the primary keys, however, the cloned object still bears the EntityKey of the original object. So when added to the manager an exception is thrown saying the object already existed.
 
Please help.
Back to Top
pokaragat View Drop Down
Newbie
Newbie


Joined: 15-Sep-2008
Location: US
Posts: 13
Post Options Post Options   Quote pokaragat Quote  Post ReplyReply Direct Link To This Post Posted: 20-Sep-2008 at 5:47am

Our way of doing auditing is on the same table. The modified row will actually be marked as inactive and a new row is created as a result of the update. So, the easiest way is to clone the object and mark the original version as inactive. The cloned object will be the latest version. Version is part of the primary key, so it is part of the identity of the new version.

Hope this helps.

 

Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 19-Sep-2008 at 4:18pm
It looks like Clone() is no longer leaving the cloned entity in a Detached EntityState, but instead it's getting the EntityState of the original object (I'll need to check why this was done, since it seems like a bug).  So, because of this things are a little confused when you go to modify the EntityKey of the cloned object.  It should work if you call RemoveFromManager() on the original object before performing the clone.  This will put both the original and cloned entities in a Detached state.
 
I guess I should ask what your use case here is, just in case there are other ways of accomplishing what you need.
Back to Top
pokaragat View Drop Down
Newbie
Newbie


Joined: 15-Sep-2008
Location: US
Posts: 13
Post Options Post Options   Quote pokaragat Quote  Post ReplyReply Direct Link To This Post Posted: 19-Sep-2008 at 2:39pm

I have the same need of modifying the data after it was cloned, modify the primary keys. So I did an override as below:
 
public override Entity Clone()
{
        DATA_ENTRY dataEntry =(DATA_ENTRY) base.Clone();
        dataEntry.DATA_ENTRY_ID = 100;
        dataEntry.DATA_ENTRY_FROM_DATE = DateTime.UtcNow;
        return dataEntry;
}
 
 
----
These two lines modify the primary keys of the entity:
 
        dataEntry.DATA_ENTRY_ID = 100;
        dataEntry.DATA_ENTRY_FROM_DATE = DateTime.UtcNow;
 
The call threw an exception:
"The given key was not present in the dictionary."
 
Is there a way to go around this one?
 
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 08-Jul-2008 at 12:09pm
There's an Entity.Clone method which should do what you need.  It will copy member values but leave the new entity in a detached state.  This method is also overridable if you need to perform custom logic.
Back to Top
danielp37 View Drop Down
Newbie
Newbie


Joined: 18-Mar-2008
Location: United States
Posts: 29
Post Options Post Options   Quote danielp37 Quote  Post ReplyReply Direct Link To This Post Posted: 07-Jul-2008 at 11:41am
In DEF, what would be the best way to create a Clone of an Object that would have all the same properties of the original object, but would be not be added to the EntityManager so it could be modified slightly, given a different primary key and then added to the EntityManager as a separate object.  I notice that there is a MemberwiseClone method, but I'm not sure that is going to do what I want.

Thanks,

Dan
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down