-
Notifications
You must be signed in to change notification settings - Fork 0
Comparing, Copying and Cloning Entities
CodeModeler generated entity classes come by default with a set of convenient methods for common object manipulations such as comparing, copying or cloning.
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.
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 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;
}
- Introduction
- Architect Guide
- Concepts
- Using Visual Studio
- Overview
- Creating a CodeModeler Project
- Visual Environment
- Project Hierarchy
- Design Surface
- Customizing Design Surfaces
- Ribbon Bar
- Property Grid
- Member Format Expressions
- Model Grid
- Method Editor
- View Editor
- Instance Editor and Grid
- Resources Editor
- Inferred Model Viewer
- Building
- Project Physical Layout
- Source Control Support
- Generating
- Aspect Oriented Design (AOD)
- Developer Guide
- The Business Object Model (BOM)
- CodeModeler Query Language (CMQL)
- Starting Guide - Tutorial
- Upgrade From CFE