Skip to content

Comparing, Copying and Cloning Entities

Simon Mourier edited this page Feb 19, 2020 · 1 revision

CodeModeler generated entity classes come by default with a set of convenient methods for common object manipulations such as comparing, copying or cloning.

Comparing

All entity classes implement the System.IComparable interface, which implies they all have a CompareTo method. Entity classes contain two CompareTo methods:

  • an explicit implementation,

  • and an implicit implementation

The explicit implementation simply calls the implicit one which can be overridden. Comparison is then done by calling the CompareTo of the entity key. Here is the generated code:

int System.IComparable.CompareTo(object value)
{
    Sample.Customer customer = null;
    customer = value as Sample.Customer;
    if ((customer == null))
    {
        throw new System.ArgumentException("value");
    }
    int localCompareTo = this.CompareTo(customer);
    return localCompareTo;
}
 
public virtual int CompareTo(Sample.Customer customer)
{
    if ((customer == null))
    {
        throw new System.ArgumentNullException("customer");
    }
    int localCompareTo = this.Id.CompareTo(customer.Id);
    return localCompareTo;
}

The property used to in the CompareTo method can be changed in the model by using the comparer property attribute. By default, all properties except the key property are set to false.

Copying

The entity class contains three copying methods:

1. CopyTo: to copy the current instance to another one of the same type. Here is the generated code:

public virtual void CopyTo(Sample.Customer customer, bool deep)
{
    if ((customer == null))
    {
        throw new System.ArgumentNullException("customer");
    }
    customer.Id = this.Id;
    customer.Name = this.Name;
    customer.Address = this.Address;
    this.OnEntityAction(new CodeModeler.Runtime.EntityActionEventArgs(this, CodeModeler.Runtime.EntityAction.CopyTo, false, customer));
}

2. CopyTo: to copy the current instance into a dictionary. Here is the generated code:

public virtual void CopyTo(System.Collections.IDictionary dict, bool deep)
{
    if ((dict == null))
    {
        throw new System.ArgumentNullException("dict");
    }
    dict["Id"] = this.Id;
    dict["Name"] = this.Name;
    dict["Address"] = this.Address;
    this.OnEntityAction(new CodeModeler.Runtime.EntityActionEventArgs(this, CodeModeler.Runtime.EntityAction.CopyTo, false, dict));
}

3. CopyFrom: to copy from a dictionary to the property values of the current instance. Here is the generated code:

public virtual void CopyFrom(System.Collections.IDictionary dict, bool deep)
{
    if ((dict == null))
    {
        throw new System.ArgumentNullException("dict");
    }
    if ((dict.Contains("Id") == true))
    {
        this.Id = ((int)(ConvertUtilities.ChangeType(dict["Id"], typeof(int), -1)));
    }
    if ((dict.Contains("Name") == true))
    {
        this.Name = ((string)(ConvertUtilities.ChangeType(dict["Name"], typeof(string), default(string))));
    }
    if ((dict.Contains("Address") == true))
    {
        this.Address = ((string)(ConvertUtilities.ChangeType(dict["Address"], typeof(string), default(string))));
    }
    this.OnEntityAction(new CodeModeler.Runtime.EntityActionEventArgs(this, CodeModeler.Runtime.EntityAction.CopyFrom, false, dict));
}

Cloning

Cloning is slightly different from copying since it creates a new instance of the entity class in the process. Technically it's done by creating a new instance and then calling the CopyTo method described earlier. Moreover, generating by default a Clone method for all generated entity classes also comes from the fact that all entity classes implement the System.ICloneable interface. The System.ICloneable is implemented explicitly.

public Sample.Customer Clone(bool deep)
{
    Sample.Customer customer = new Sample.Customer();
    this.CopyTo(customer, deep);
    return customer;
}
 
public Sample.Customer Clone()
{
    Sample.Customer localClone = this.Clone(true);
    return localClone;
}
 
object System.ICloneable.Clone()
{
    object localClone = this.Clone();
    return localClone;
}
Clone this wiki locally