C # Basic series: Implementing Your own ORM (constructing my own ORM)

Source: Internet
Author: User
Tags inheritance

To construct my own ORM

I'm sure a lot of my friends have understood what I'm going to discuss here in the previous two chapters, which is to define the O/r mapping rule based on custom attributes, and then dynamically acquire the rule by reflection to dynamically construct the SQL statement.

Because this little Thing (ORM) was born in Shenzhen, so I think, she should have a Shenzhen name, so I called her "miniorm". I wonder what you think?

Miniorm uses the structure of the one_inherit_tree_one_class (an inheritance tree corresponds to a table), although this structure can easily lead to data redundancy, but the structure is simple. Another, this miniorm only consider a table a PK, an FK situation.

The miniorm structure is as follows, in order to make it easier to understand and use, I use 3 classes:

1, Ormwriter: Responsible for the entity objects (such as the person mentioned in the previous section) into the database and modify the corresponding records in the database.

2, Ormremover: Responsible for the deletion of the specified records according to the entity object;

3, Ormreader: Responsible for reading the specified records according to the entity object;

Above is the 3 main classes of Miniorm. Below we will construct her step-by-step in detail according to the previous description. We are here to illustrate the example of the person mentioned above.

Through the first chapter of this series, we know that objects not only have inheritance relationships, but also include relationships in practical applications, such as a person containing two hand (hand) classes, including a head class, and so on, our person should have an ID in the database, For easier use and discussion, this ID is an int as well as an automatic growth type (ID indentity (1,1)) in Miniorm. These are the areas that our miniorm should consider.

We make changes to our person:

[Dataobjectattribute (' person ')]
public class person
{
private int _id;
private string _name;
Private int _age;
private String _sex;
Private head _head;
Private Hand _lefthand;
Private Hand _righthand;
Public int ID
{
get {_id;}
Set {_id = value;}
}
Public head head
{
get {_head;}
Set {_head = value;}
}
Public Hand lefthand
{
get {return _lefthand;}
Set {_lefthand = value;}
}
Public Hand righthand
{
get {return _righthand;}
Set {_righthand = value;}
}
[Datafieldattribute ("name", "NvarChar")]
public string name
{
Get {return this._name ;
Set {this._name = value;}
}
[Datafieldattribute (' Age ', ' int ']]
public int age
{
get {return this._age;}
Set {this._age = value;}
}
  [Datafieldattribute ("Sex", "NvarChar")]
public string Sex
{
get {return this._sex;}
Set {this._sex = value;}
}
}

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.