TheJoeIaut TheJoeIaut - 1 month ago 22
C# Question

Deep Clone Entity Without Serialization

I need to deep clone an Entity (To use an existing Entity as pattern for a new one). I found several approaches, but all include Serialization. Since our Entities derive from ObservableObject of the MVVM Toolkit that is not Serializeable we can't use those approaches.

What would be the best way to do so?

We are usinng .net 4.5 and EF5 and WPF.

Answer
  1. One method is to simply write custom code that will perform the clone for you. Given an object, new an instance, and then for each property write some code that will give you a deep copy of that property, perhaps recursively chaining down your object graph for complex properties.

  2. Another option is create a model that stores your important data, serialize that, and then deserialize and restore data from that temporary serializable model into a new entity object.