Duplicating/copying entities

329 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

James Gregory

nieprzeczytany,
28 mar 2008, 15:53:3828.03.2008
do nhu...@googlegroups.com
I came across a problem while working on a project a while ago, at the time I wasn't able to find a solution and the project has since progressed. However, it still bugs me, so I'd like to know if any of you guys know of a solution I may have missed.

In it's simplest form, what I was trying to do was take an entity and duplicate it. What I mean by duplicate is to create an exact copy of the object, but have it assigned a new ID. So you end up with two identical records in the database, just with different IDs.

I couldn't find a succinct way to handle this in NHibernate. I actually ended up creating a new instance of the entity and manually doing: copy.MyField = original.MyField, which isn't very maintainable.

I tried using SaveOrUpdateCopy on the session, but that didn't help. I also tried taking the original entity, setting it's ID to null (or 0), then saving it in the hope it would do an INSERT rather than an UPDATE. No avail.

Does anyone have any suggestions of how I should've handled this?

Ayende Rahien

nieprzeczytany,
28 mar 2008, 15:57:1728.03.2008
do nhu...@googlegroups.com
Create a new instance of the entity, copy all the properties to it, call Save()

James Gregory

nieprzeczytany,
28 mar 2008, 16:08:0128.03.2008
do nhu...@googlegroups.com
That's what I ended up doing. There are quite a few properties and some collections that need duplicating. For a few properties I don't mind doing it manually, but with 20+ it gets a bit tedious.

Thinking about it, I could've probably whipped something up using reflection. I'm unsure as to if there would be any issues with the collections though.

Ray Houston

nieprzeczytany,
28 mar 2008, 16:01:2928.03.2008
do nhu...@googlegroups.com
I'm not sure what that would do with all of it's collections though
and other entity references.

On Fri, Mar 28, 2008 at 2:58 PM, Ray Houston <ray.h...@gmail.com> wrote:
> You could do a ISession.Evict(entity) and then change the ID, then
> save it. It should insert a new record.

Ray Houston

nieprzeczytany,
28 mar 2008, 15:58:1028.03.2008
do nhu...@googlegroups.com
You could do a ISession.Evict(entity) and then change the ID, then
save it. It should insert a new record.

On Fri, Mar 28, 2008 at 2:53 PM, James Gregory <jagreg...@gmail.com> wrote:

Gustavo Ringel

nieprzeczytany,
28 mar 2008, 16:19:4428.03.2008
do nhu...@googlegroups.com
i think that doing a deep copy of an entity which has 20+ properties and collections and complex objects between them is performance suicide...with some endless loop options...
I generally have to copy some o the data while initializing part of it...so i end up copying manually or by reflection every value type...
 
Gustavo.

Andrew Burgher

nieprzeczytany,
28 mar 2008, 17:20:3928.03.2008
do nhu...@googlegroups.com

There is always the BinaryFormatter cloning technique... but you’d still have to worry about managing your Ids

 

HTH,

Andrew

 

 

public T CloneObject<T>(T source) {

    if (source == null) {

        return default(T);

    }

 

    T value;

 

    using (MemoryStream stream = new MemoryStream()) {

        IFormatter formatter = new BinaryFormatter();

        formatter.Serialize(stream, source);

        stream.Seek(0, SeekOrigin.Begin);

        value = (T) formatter.Deserialize(stream);

    }

 

    return value;

<br

Tim Scott

nieprzeczytany,
30 mar 2008, 18:06:0930.03.2008
do nhusers
If you use the binary formatter technique, you not only have to worry
about managing IDs, you must also be concerned with many-to-one and
one-to-one associations. Let's say you are cloning an Order. The
Order has a collection IList<LineItem> and also a property Customer.
You probably don't want Order.Customer in your cloned order to have a
duplicate of the customer from the source order having the same Id.
It's not to hard to spin down the object graph making all Ids
transient. It's a little more tricky to preserve 1:1 and n:1
references.
> On Fri, Mar 28, 2008 at 10:01 PM, Ray Houston <ray.hous...@gmail.com> wrote:
>
> I'm not sure what that would do with all of it's collections though
> and other entity references.
>
>
>
> On Fri, Mar 28, 2008 at 2:58 PM, Ray Houston <ray.hous...@gmail.com> wrote:
> > You could do a ISession.Evict(entity) and then change the ID, then
> > save it. It should insert a new record.
>
> > On Fri, Mar 28, 2008 at 2:53 PM, James Gregory <jagregory....@gmail.com>

Fabio Maulo

nieprzeczytany,
30 mar 2008, 18:21:4930.03.2008
do nhusers
James Gregory ha escrito:
> I tried using SaveOrUpdateCopy on the session, but that didn't help. I also
> tried taking the original entity, setting it's ID to null (or 0), then
> saving it in the hope it would do an INSERT rather than an UPDATE. No avail.

The SaveOrUpdateCopy of NH2.0.0 return a new instance of the entity
leaving the original unchanged.
Probably the new method session.Merge is what you are looking for
(take a look to ISession.Merge summary).

Bye.
Fabio Maulo.
Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Nowe wiadomości: 0