11

How to implement objects (entities) cloning in NHibernate? Each entity class has such properties:

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

Also, the entity class inherits BaseObject. I tried to implement the solution using XML serialization, but it's not possible to serialize interfaces.

Thank you for your answers!

akrisanov
  • 3,212
  • 6
  • 33
  • 56

3 Answers3

15

AutoMapper http://automapper.codeplex.com/ solves my problem. For example, it's possible to clone a business object in the next way:

Mapper.CreateMap<Transaction, Transaction>();
var newtransact = new Transaction();
Mapper.Map(transact, newtransact);
akrisanov
  • 3,212
  • 6
  • 33
  • 56
  • 3
    You will want to exclude the Id properties using `Mapper.CreateMap().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 Aug 03 '10 at 15:56
  • Thanks for this. It saved me some headaches. – Nick Jun 18 '13 at 19:11
2

Use DTOs.

Community
  • 1
  • 1
Jim G.
  • 15,141
  • 22
  • 103
  • 166
0

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
  • 23,773
  • 22
  • 96
  • 162