martes, 27 de diciembre de 2016

Cloning Objects in .NET Framework






In this article, we will be showing any ways to clone objects in .NET Framework. For each mode to clone will analyses the pros and cons.

This explain is not valid to Immutable class (stringsdelegatesstructures, etc), because these classes have another behaviors and do not feature in this article.


For this we will use two implementation techniques, first of all the ICloneable Interface, Secondly Extension Methods depending the type of cloning.







Class for examples

This is the class we will use to the examples:


public class Customer : ICloneable
{
    public    int                ID        { get; set; }
    public    string             Name      { get; set; }
    public    decimal            Sales     { get; set; }
    public    DateTime           EntryDate { get; set; }
    public    Address            Adress    { get; set; }
    public    Collection<string> Mails     { get; set; }
 
    protected string             Data1     { get; set; }
    private   string             Data2     { get; set; }
 
 
    public Customer()
    {
        Data1 = "data1";
        Data2 = "Data2";
    }
 
 
 
    public virtual object Clone() { }
 
}





The need to clone

It is a very essential programmatic part, if you are not a basic developer, you can skip this bloc.

In the high level languages (C#, java, C++, etc), when we assign an object to another object, we are assigning two objects to the same reference:


Customer customer1 = new Customer { ID = 1, Name = "Test", City = "City", Sales = 1000m };
 
Customer customer2 = customer1;
























Customer1 and Customer2  are linked and any modification in an object will be reflected in the other object.

To Clone is necessary for unlinked the object and its virtual copy and they are independent objects.






















ICloneable

It is an official .NET Framework Interface to Clone objects. It is very simple and has only one method, Clone.

This interface leaves you free to use the Clone method as we like. We can apply depth level we choose.


public interface ICloneable
{
    object Clone();
}


The most problem of this interface is the return value of Clone method, object type. Whenever you use the Clone method, you will have to do a casting to principal type:


Customer customer2 = (Customer)customer1.Clone();




Extension Method

Another way to clone objects is by Extension Methods. These methods provide an opportunity to return generics types, with this we save the boxing/unboxing problems. We write the Extensions Methods only once, and our extension method extending object, we may use it for all .NET types. 


public static class MyExtensions
{
    public static T CloneObject<T>(this object source)
    {
        T result = Activator.CreateInstance<T>();
 
        //// **** made things
        return result;
    }
}

Call:


Customer Customer2 = customer1.CloneObject();


We can use the extension method in conjunction with ICloneable:


public class Customer : ICloneable
{
   // Properties ...

    public virtual object Clone()
    {
        return this.CloneObject();
    }
}



Object.MemberWiseClone

MemberWiseClone is a protected method of object. This method creates a shallow copy of current object to the new object.

MemberWiseClone copies at a different way, the references properties (classes) or values properties (structs):

·                   Structs .- Copy bit a bit the value of property.
·                  Class    . - Copy the reference of property, consequently they are the same object.

The case class is a problem, because both objects are the same. This is a lack of method.

The use of MemberWiseClone is usually done at the same that ICloneable Interface, because the MemberWiseClone is a protected method and is mandatory call it internally.


MemberWiseClone example:


public class Customer : ICloneable
{
    public    int                ID        { get; set; }
    public    string             Name      { get; set; }
    public    decimal            Sales     { get; set; }
    public    DateTime           EntryDate { get; set; }
    public    Address            Adress    { get; set; }
    public    Collection<string> Mails     { get; set; }
 
    protected string             Data1     { get; set; }
    private   string             Data2     { get; set; }
 
 
    public Customer()
    {
        Data1 = "data1";
        Data2 = "Data2";
    }

    public virtual object Clone()
    {
        return this.MemberwiseClone();
    }
}


Pros:
  • Easy to developer.
  • Very little code
  • Easy to Understand
  • It copy any fields/properties types (simples and complex)
  • It doesn’t need to mark the class with any special attribute
Cons:
  • It can be called inside the class only, because it is a protected method.
  • It must be implemented in all classes to clone.
  • The reference properties of object to clone don´t be copied, they are linked.
  • The clone method return object, consequently we will have do casting each time we use it.

If we try a completely depth copy, we have to do manual assignments of all references properties:


public virtual object Clone()
{
    var result = this.MemberwiseClone();
 
    // Manual assignments 
 
    result.Adress = new Address
    {
        City    = this.Adress.City,
        Street  = this.Adress.Street,
        ZipCode = this.Adress.ZipCode
    };
 
    result.Mails = new Collection<string>();
 
    this.Mails.ToList().ForEach(a => result.Mails.Add(a));
 
    return result;
}




Stream - Formatters

This cloning type use serialization to process the objects copies. It makes a depth copies but forces to mark the class objects with any serialization attribute.

In this site, there is a very good example from our companion  Surajit Datta Article, we will take this code for our example.

For we don’t write this code in all clone classes, we will create an Extension Method:


public static T CloneObjectSerializable<T>(this T obj) where T : class
{
    MemoryStream    ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, obj);
    ms.Position = 0;
    object result = bf.Deserialize(ms);
    ms.Close();
    return (T) result;
}

Call:

Customer customer2 = customer1.CloneObjectSerializable();


If run this code, it throw SerializationException:

























To prevent these errors, we mark the class with Serialization Attribute:


[Serializable]
public class Customer



Pros:
  • Easy to developer
  • Easy to write in an Extension Method, therefore we implement once.
  • It copy any fields/properties types (simples and complex)
  • It implements deep copy.
  • It doesn’t necessary call inside the class, because is an object extension method.
  • Return a Generic Type, therefore we aren’t necessary apply boxing / unboxing.

Cons:
  • It needs to mark with special attribute.
  • For your implementation it needs more code and logic.

This method can be use with ICloneable perfectly maintaining any of its virtues.


public virtual object Clone()
{
    return this.CloneObjectSerializable();
}




Conclusions

There isn’t a magic way to clone objects in .NET Framework, but these two models make the work easier. In the development world, it's necessary to be clear about cloning objects, this misunderstanding is often the consequence of errors and unexpected behaviors in our programs.