C # basic series: implement your own ORM (reflection and attribute application in ORM)

Source: Internet
Author: User

Application of reflection and attribute in ORM

 

I. Reflection
What is reflection?
To put it simply, reflection is a way to dynamically obtain object information at runtime, such as knowing the attributes, methods, and delegation of an object at runtime.
What is the purpose of reflection?
Reflection not only allows you to obtain object information during running, but also provides the ability to dynamically call object methods and dynamically set and obtain attributes during running.
What is the use of reflection in Orm?
The ORM implementation I discussed here describes the ing rules through custom attribute. However, we do not know which table the specific object needs to correspond to, and these objects are independent of our ORM framework. Therefore, we can only use custom attribute to define the ing rules, then, the ing rules are dynamically obtained through reflection.
Reflection implementation:
Next we will discuss it in a simple way to get the object's property value. Suppose we have class person, which has three attributes: name, age, and sex. We use the Reflection Method to dynamically obtain the values of these three attributes of the person object.

  1. Public class person
  2. {
  3. Private string _ name;
  4. Private int _ age;
  5. Private string _ sex;
  6. Public string name
  7. {
  8. Get {return this. _ name ;}
  9. Set {This. _ name = value ;}
  10. }
  11. Public int age
  12. {
  13. Get {return this. _ age ;}
  14. Set {This. _ age = value ;}
  15. }
  16. Public String sex
  17. {
  18. Get {return this. _ sex ;}
  19. Set {This. _ sex = value ;}
  20. }
  21. }

The test code is as follows:

 

  1. Static class Program
  2. {
  3. [Stathread]
  4. Static void main ()
  5. {
  6. Person = new person ();
  7. Person. Name = "Snoopy ";
  8. Person. Age = 5;
  9. Person. Sex = "male ";
  10. Propertyinfo [] Infos = person. GetType (). getproperties ();
  11. Console. writeline ("Print attributes ");
  12. Foreach (propertyinfo info in Infos)
  13. {
  14. // Obtain and print attributes
  15. Console. writeline (info. Name + ":" + info. getvalue (person, null ));
  16. }
  17. Console. writeline ("set person. Name = hellokitty ");
  18. // Set attributes and name attributes
  19. Foreach (propertyinfo info in Infos)
  20. {
  21. If (info. Name = "name ")
  22. {
  23. Info. setvalue (person, "hellokitty", null );
  24. }
  25. }
  26. Console. writeline ("Print attributes ");
  27. Foreach (propertyinfo info in Infos)
  28. {
  29. // Obtain and print attributes
  30. Console. writeline (info. Name + ":" + info. getvalue (person, null ));
  31. }
  32. Console. Read ();
  33. }
  34. }

 

The above demonstrates how to dynamically obtain and set object attributes through reflection. But what does this have to do with orm and attribute? This is what we will do next.

 

Ii. Use of attribute:
Although attribute Chinese translation is also known as "attribute", it is totally different from the attribute of an object. She describes the object or object attributes, methods, delegates, and so on at runtime. It is used to describe your code or affect the behavior of your program at runtime.
In fact, we often see attribute in C # programming, but we didn't pay attention to it. For example, the "[stathread]" before the main function is actually an attribute. The entire process is [stathreadattribute]. In addition, specify the serializable [serializable] of the class. Are you familiar with this? However, it is estimated that it is useless at ordinary times, so I did not pay attention to it.

Since attribute is a class, its definition method and class are no different. The only difference is that the custom attribute class must inherit from system. attribute.
Next we will briefly define an attribute that describes the field information of the database. In this class, we will omit it and only provide "field name" and "field type ":

  1. Public class datafieldattribute: attribute
  2. {
  3. Private string _ fieldname;
  4. Private string _ fieldtype;
  5. Public datafieldattribute (string fieldname, string fieldtype)
  6. {
  7. This. _ fieldname = fieldname;
  8. This. _ fieldtype = fieldtype;
  9. }
  10. Public String fieldname
  11. {
  12. Get {return this. _ fieldname ;}
  13. Set {This. _ fieldname = value ;}
  14. }
  15. Public String fieldtype
  16. {
  17. Get {return this. _ fieldtype ;}
  18. Set {This. _ fieldtype = value ;}
  19. }
  20. }

Well, we have our own attribute to describe the database field, so now we apply it to the actual class. We continue to use the person class as follows:

 

 

 

  1. Public class person
  2. {
  3. Private string _ name;
  4. Private int _ age;
  5. Private string _ sex;
  6. [Datafieldattribute ("name", "nvarchar")]
  7. Public string name
  8. {
  9. Get {return this. _ name ;}
  10. Set {This. _ name = value ;}
  11. }
  12. [Datafieldattribute ("Age", "int")]
  13. Public int age
  14. {
  15. Get {return this. _ age ;}
  16. Set {This. _ age = value ;}
  17. }
  18. [Datafieldattribute ("sex", "nvarchar")]
  19. Public String sex
  20. {
  21. Get {return this. _ sex ;}
  22. Set {This. _ sex = value ;}
  23. }
  24. }

Through custom attribute, we define the one-to-one correspondence between class attributes and database fields. We add attribute descriptions to the name, age, and sex attributes of the person class, the field names and types corresponding to them are specified, where person. name corresponds to the field name. The field type is nvarchar ....

 

Iii. Joint Use of reflection and attribute.
From the above description, we understand reflection, attribute, and ORM ing rules. However, it may be confusing for new contacts. How can we dynamically obtain these ing rules? Let's hear from you.
This requires reflection:
In the following example, we add the datafieldattribute description to the name, age, and sex of person, which is actually adding the ing rules of O (object)/R (relational database, next we will use the Reflection Method to dynamically obtain this ing rule:

  1. Static class Program
  2. {
  3. [Stathread]
  4. Static void main ()
  5. {
  6. Person = new person ();
  7. Person. Name = "Snoopy ";
  8. Person. Age = 5;
  9. Person. Sex = "male ";
  10. Propertyinfo [] Infos = person. GetType (). getproperties ();
  11. Object [] objdatafieldattribute = NULL;
  12. Foreach (propertyinfo info in Infos)
  13. {
  14. Objdatafieldattrites = info. getcustomattributes (typeof (datafieldattrites), false );
  15. If (objdatafieldattribute! = NULL)
  16. {
  17. Console. writeline (info. Name + "-> database field:" + (datafieldattribute) objdatafieldattribute [0]). fieldname );
  18. }
  19. }
  20. }
  21. }

 

 

Haha, do you really want to do it? Of course, if you start this step, I will be very happy, indicating that my description is still clear (Note: This is not valid for the big players who already know it ). It also shows that you are very competent. The next step is to dynamically obtain the ing rules from the object based on this method, and dynamically construct statements such as insert, update, and delete.

 

Iv. Summary in this Chapter
In this chapter, I have introduced in detail the concepts and applications of reflection and custom attribute, as well as how to dynamically obtain the ing rules of O/R Mapping at runtime. Of course, the code here is just an example. To implement an Orm, we still need to consider a lot, such:
1. Which database table does person correspond?
2. What is the expression of PK and FK in person (if any?
......
These questions will be explained in my next article.

 

Related connections:

C # basic series: implement your own ORM (basic concepts of ORM)

C # basic series: implement your own ORM (build my own ORM)

C # basic series: implement your own ORM (miniorm test code)

 

Source code download

Share:
  • Previous Article: C # basic series: implementing your own ORM (basic concepts of ORM)
  • Next article: C # basic series: implement your own ORM (build my own ORM)

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.