The wojilu system has a complete ORM system. For the ORM system, there should be many masters in the garden. I didn't have any RESEARCH ON THE ORM system in the past. By studying the wojilu system, by the way, I learned about wojilu's ORM code, which is an implementation process of ORM.
First, let's take a look at the location of the wojilu system's ORM code: wojiluORM
Before learning wojilu code, let's first imagine what its code looks like. Before studying other people's code, you must make a prediction first. Just like taking the test, you must first repeat the question and then read the answer to learn things. If you look at the answer, if you lose the opportunity to think independently, you may be misled by a wrong answer.
In the orm system, objects are put into the database of RationShip through Mapping. Place objects in a relational database through ing. He needs something
1. A set of ing rules for basic object types to database types, such as ing long integer int16 to long integer of the database, ing DateTime to DateTimeStamp, and ing String to varChar.
2. A reflection mechanism allows you to traverse all the public attributes of an object through reflection.
3. A set of automatic database access systems, some basic database access methods, so that developers do not feel the access to the database. To achieve this function, there must be an automatic SQL file generation system.
Here is my answer. Let's take a look at wojilu's answer. Of course, wojilu's answer is not a standard answer. An ORM system has no standard answer and does not need a standard answer. The answer that fits your project is a good answer.
In many ORM systems, there is an ObjectBase class, which is the source of all things. ORM is centered around the source of all things. Only one object can be inherited from ObjectBase, to enjoy the convenience of ORM. In other words, all the things inherited from ObjectBase are included in the ORM hosting scope. Wojilu also has 10 thousand thing source class. The source code of this class is in wojilu \ _ wojiluObjectBase. cs
Part of the Code is as follows:
1 namespace wojilu {
2
3 /// <summary>
4 // The base class that all the Domain Models in the ORM must inherit
5 /// </summary>
6 /// <typeparam name = "T"> </typeparam>
7 [Serializable]
8 public class ObjectBase <T>: IEntity, IComparable where T: ObjectBase <T> {
9
10 private int _ id;
11
12 /// <summary>
13 // Object id
14 /// </summary>
15 public int Id {
16 get {return _ id ;}
17 set {this. setId (value); _ id = value ;}
18}
19
20 protected virtual void setId (int id ){
21}
22
23 /// <summary>
24 // query all data
25 /// </summary>
26 /// <returns> </returns>
27 public static List <T> findAll () {return db. findAll <T> ();}
28
29 /// <summary>
30 /// query objects by id
31 /// </summary>
32 // <param name = "id"> </param>
33 // <returns> </returns>
34 public static T findById (int id) {return db. findById <T> (id );}
35
36 /// <summary>
37 // count all data volumes
38 /// </summary>
39 /// <returns> </returns>
40 public static int count () {return db. count <T> ();}
The interface of multiple methods used by the ORM is defined here, which is also the basis of the entire ORM. We can analyze this class to analyze the entire wojiluORM system.
This class must implement the IEntity and IComparable interfaces.
First interface IEntity, entity interface: Look at its code
Every Orm object has an ID attribute. If you are interested in the wojilu system, this ID is the key word of the cache system. The content of the same ID is obtained from the cache. ID is used as a basis for determining whether the cache hits.
There is also a Get and SET method to Get or SET the value of an attribute. (Not through reflection, fast speed)
The meaning of this sentence is not very clear yet. It may be understood through further study.
1 /// <summary>
2 // all the objects that can be persisted by ORM automatically implement this interface
3 /// </summary>
4 public interface IEntity {
5
6 /// <summary>
7 // each Persistent object has an Id attribute
8 /// </summary>
9 int Id {get; set ;}
10
11 /// <summary>
12 // get the attribute value (not through reflection, fast)
13 /// </summary>
14 /// <param name = "propertyName"> attribute name </param>
15 /// <returns> </returns>
16 Object get (String propertyName );
17
18 /// <summary>
19 // set the attribute value (not through reflection, fast)
20 /// </summary>
21 /// <param name = "propertyName"> attribute name </param>
22 /// <param name = "propertyValue"> attribute value </param>
23 void set (String propertyName, Object propertyValue );
24
25 /// <summary>
26 // including the object metadata and additional information required for object query, which is not commonly used
27 /// </summary>
28 // ObjectInfo state {get; set ;}
29}
30
31
32
33}
The specific implementation method is explained in the source code carefully. Part of the Code is truncated:
1 /// <summary>
2 // obtain the attribute value based on the attribute name
3 /// </summary>
4 // <param name = "propertyName"> attribute name </