C # Discovery Tour 11th talk about using reflection and attributes to construct your own ORM Framework (top)

Source: Internet
Author: User
Tags definition function definition reflection access database

ORM Background

In the database field, the main database is relational database, and its relational data structure model, which is quite mature in both mathematics and practice, is widely used. In relational data structure theory, all data are organized into separate two-dimensional tables, and a data table has several rows of columns. Therefore, the relational database is suitable for storing a large number of simple structure data, which is not suitable for storing the complex data structure.

In the programming world, object-oriented programming ideas and their derivative ideas occupy the mainstream. The object-oriented programming idea has the encapsulation, the inheritance, the heavy load and so on to realize the more complex data structure conveniently, this adapts the modern information system to contain the massive complex structure characteristic. Therefore, object-oriented programming ideas are widely used.

There is a big difference between the relational data model and object-oriented programming idea, the exchange between the data is not smooth, just like Nanjing, Jiangbei and the main urban area is the Yangtze River to block traffic. So developers desperately need to crack this data traffic blocking.

Programmers used to be programmed to read a field value from a database and assign it to a field or property of a programming object, like a log bridge on the Yangtze River, which is inefficient and difficult to maintain. Later, there is a framework called ORM, which can be based on some configuration information in the database of fields and programming objects in the mapping between the fields or properties, so that it is convenient to read from the database field values and assigned to the object properties. This is a semi-automatic mechanism, can greatly improve the efficiency of development, simplifying maintenance, it is like erecting a highway bridge on the Yangtze River.

In essence, the gap between relational database and object-oriented programming ideas is very large, just like the Yangtze River is the sky, even if the construction of five or six bridges is not enough. The thorough solution is abandons the relational database and uses the object-oriented database, but this process is like Nanjing Jiangbei overall relocation to Jiangnan, the project is vast, the time is long. Before we wait for the domination of object-oriented databases, we still have to endure the pain of data traffic between relational databases and object-oriented programming ideas, and use ORM frameworks to reduce this pain very limited. From this point of view, we hate relational databases, just like those who engage in launch vehicles hate the gravity of the Earth.

Reflection and characteristics

Reflection is. NET Framework provides an advanced programming interface. VB has learned that there is a CallByName function in VB, it can call the object's specified name of the member method, such as having a Form object, we can call "frm." Close () "Closes the form, or calls" CallByName (frm) to achieve the same functionality. And reflection is a callbyname version of. NET, and more powerful. With reflection, we can list the names of all the fields, properties, methods, and events for any object type, including public or private. We can also get a more in-depth look at the data type of the field, the number of parameters for the member method, the type and its return value, the delegate type used by the event, and so on. It can be said that reflection technology is. NET Framework provides a read-only program gene analysis technique.

. NET Framework provides a natural support for the technology that reflects this program's genetic analysis. In the. NET Framework, any object type is derived from the type object, and the object type has a GetType function. This function returns an object of type System.Type, which is the entry point of the reflection operation, so that any. NET object type can be analyzed with reflection technology.

Features too. NET Framework provides an advanced programming tool. It is additional information attached to a programming unit such as a type, field, property, function, and so on, equivalent to the descriptive text for tables and fields in an Access database. It does not affect the normal execution of the attached programming unit. But it is objectively existent, can get its information by reflection, in general we can call the System.Attribute type GetCustomAttribute function to get the specified type of attribute object appended by the programming unit.

From a programmatic standpoint, attributes are also an object type, and they are all derived from System.Attribute. NET class library has already defined a large number of attribute types, we can also define our own attributes.

The use of attributes is also very simple, that is, the use of square brackets in front of the programming unit contains the definition of attributes, such as for the WinForm program its entry function has the following similar definitions. Here, the "[Ssystem.stathread]" at the front of the function definition means that the function appends an attribute with a type named "System.STAThreadAttribute", where there is a C # syntax, where the attribute can be appended with the type name " Attribute "suffix removed, of course, can also write full name. A programming unit can attach multiple attributes.

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[System.STAThread]
static void Main()
{
     Application.Run(new frmTestORM());
}

Using attributes, we can attach database field mapping information on object properties, and using reflection, we can dynamically read and set the object's property values. This feature and reflection can be the technical basis for our implementation of ORM.

More information about reflection and features can be referenced in the documentation in MSND.

Related Article

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.