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;}
}
}