views:

428

answers:

4

How to implement cloning of objects (entities) in NHibernate? In the classes of entities has properties such

public virtual IList<Club> Clubs { get; set; }

All classes are inherited from BaseObject. I tried to implement using xml serialization, but the interfaces are not serialized.

Thank you for your answers!

A: 

Cloning of entities is not in the NHIbernate scope. You should implement this. Here is a good example how.

Al Bundy
+2  A: 

Use DTOs.

Jim G.
A: 

I don't know of your domain or requirements, nor whether am I misunderstanding your need, but implementing the ICloneable interface and writing the code to clone your object should work.

Remember you'll have to type cast when cloning.

ClonedObject clonedObjectinstance = (ClonedObject)initialEntityInstance.Clone();
Will Marcouiller
Please, tell me where I got it wrong so that I can myself learn! =) Is there something I misunderstood in the question? Thanks!
Will Marcouiller
+1  A: 

AutoMapper http://automapper.codeplex.com/ resolved my question. An example of cloning a business object:

Mapper.CreateMap<Transaction, Transaction>();
var newtransact = new Transaction();
Mapper.Map(transact, newtransact);
Anry
You will want to exclude the Id properties using `Mapper.CreateMap<Transaction, Transaction>().ForMember(d => d.Id, o => o.Ignore());` and manually copy IList properties using something like: `newtransact.Clubs = this.Clubs.Select(item => item.Clone()).ToList();` - see: http://stackoverflow.com/questions/3396808/
Piers Myers