Four ways to achieve ORM

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.