Clone an entity - revisited

Posts   
 
    
tomc
User
Posts: 31
Joined: 28-Jun-2006
# Posted on: 24-Jul-2006 20:34:30   

I am trying to write an entity clone method that operates on IEntity2 objects. I read your suggestion using the serialize/deserialize technique (http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=1834). As mentioned, this seems to work fine except under the circumstance I am using, i.e. recursive SaveEntityCollection; this never saves the new entity. It seems that the culprit is the ObjectID. When the entity is "cloned", it also clones the ObjectID and SaveEntityCollection then rejects the new entity because it thinks it has already been processed. The obvious solution is to assign a new ObjectID to the new entity. This seems to work fine.

After considering this approach, it seems that it is rather inelegant. My initial approach, before looking at the forums, was to use CreateEntityFactory to instantiate an entity of the desired type and Fields.Clone to assign the values, but alas CreateEntityFactory is protected. Would this approach be valid if CreateEntityFactory were public? If so, could you make CreateEntityFactory public? Otherwise, can you suggest a more elegant means of cloning entities?

Note that I am using V2.

Thanks, Tom

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 25-Jul-2006 11:16:57   

The ObjectID has to be the same after deserialization for obvious reasons when deserialization is used in a remoting scenario, and because the serialization code doesn't know in which context it is used, the same ObjectID will be used.

I didn't make CreateEntityFactory public as I couldn't think of a purpose which would require to have a public method. As making it public will break existing code which overrides the protected method, I can't change this as well.

You could add an interface to the entities (in the designer), and through an include template generate the implementation of the interface to the entity classes, which simply exposes the factory through a public method or property and calls the CreateEntityFactory method.

You could also generate partial classes for this.

Frans Bouma | Lead developer LLBLGen Pro