As the ORM name indicates, the key to ORM is to solve the mapping between "object-relationship", such as how to convert a DataRow to a entity object, and how to map an action to a entity object to a IDbCommand Wait a minute. We take datarabbit as an example, and in Datarabbit, we use the Iormapping interface to abstract these mappings:
public interface iormapping<tentity>
{
Tentity Getentityfrom (DataRow row, bool Withblob);
<summary>
Fillparametervalue uses the contents of entity to populate the parameter values of each idbdataparameter in the command.
</summary>
void Fillparametervalue (IDbCommand command, tentity entity);
}
There are at least four options on how to implement the Iormapping interface. We take the example of implementing the Getentityfrom method as an example to describe these four scenarios, which is to convert a DataRow to a entity Object.
1. Code generator
Now there are many code generators that can directly generate the DAL layer that implements ORM, and the rationale behind this is to generate the corresponding class that implements the Iormapping interface based on the outline of the datasheet (such as what columns, the type of each column, etc.). The code generator completes this work before it is compiled.
2. Reflection
Converts a DataRow to a entity object, and if the granularity is finer, what we actually want to do is to assign the value of a column of DataRow to the property of the entity Object, and we can use reflection to give entity at run time object to assign a value, such as:
Entitytype.invokemember (ColumnName, BindingFlags.Public | Bindingflags.ignorecase |
BindingFlags.Instance | BindingFlags.SetProperty, NULL, entity, Row[columnname]);
The meaning of this line of code is to assign the value of the "columnName" column in row to the entity object's "ColumnName" property.