ORM code analysis of wojilu system-[source code structure analysis, using features and reflection to perceive attributes-feature Introduction]

Source: Internet
Author: User
Tags net serialization

We know that the primary function of ORM is automation. How to automatically generate corresponding data tables with more class attributes is a focus of ORM research. Wojilu's implementation method is to add the attr feature on the attribute, perceive the attribute features through the Reflection during the runtime, and determine the data ing policy.

The word [annotation] in this article is out of the source code and features refer to the same meaning. I personally prefer [features ].


Open the wojilu source code. wojilu/orm/attribute contains many special classes. wojilu knows the attributes in the class through them and wants to map them to fields in the database.


 

CacheCountAttribute: count cache annotation, which will be explained in detail in wojilu Cache
 

ColumnAttribute: Data column annotation, used to identify the name and length of the column corresponding to the attribute in the database
 

 

1 /// <summary>
2 // data column annotation, used to identify the name and length of the column corresponding to the attribute in the database
3 /// </summary>
4 [Serializable, AttributeUsage (AttributeTargets. Property)]
5 public class ColumnAttribute: Attribute {
6 private String _ columnName;
7 private String _ label;
8 private int _ length;
9
10 public ColumnAttribute (){
11 _ length = 250;
12}
13
14 public String Name {
15 get {
16 return _ columnName;
17}
18 set {
19 _ columnName = value;
20}
21}
22
23 public String Label {
24 get {
25 return _ label;
26}
27 set {
28 _ label = value;
29}
30}
31
32 public int Length {
33 get {
34 return _ length;
35}
36 set {
37 _ length = value;
38}
39}
40
41 public Boolean LengthSetted (){
42 return _ length> 0 & _ length! = 250;
43}
 

 

This field indicates the information of a column in the database. The difference between LABEL and NAME is that LABEL is the NAME used when submitting a form, NAME is the NAME used in the database. Here, LengthSetted is actually a problem. If Length is greater than 0, there is no problem with this condition. If length is not equal to 250, there is a BUG. The correct method here is to Set a Flg for determination, Set the Flg to True when setting the Length attribute, and then LengthSetted returns the Flg. 250 is not a MagicNumber. It is very likely that someone else sets the length to 250.

 

DatabaseAttribute: Database annotation only sets the database name.
 

DecimalAttribute: used for custom precision data. It can also store currency values with custom precision.
This is to solve the floating point number feature. Through this feature, we can customize the precision of numbers in the database.

 

 

 

 

1 /// <summary>
2 // It is used for custom precision data and can also store currency values with custom precision.
3 /// </summary>
4 [Serializable, AttributeUsage (AttributeTargets. Property)]
5 public class DecimalAttribute: Attribute {
6
7 /// <summary>
8 // the precision of the numeric value, that is, the total number of digits around the decimal point, but not the decimal point.
9 /// </summary>
10 public int Precision {get; set ;}
11
12 /// <summary>
13 // number of digits to the right of the decimal point
14 /// </summary>
15 public int Scale {get; set ;}
 

 

 

However, no security check is performed here. For example, the number of decimal places cannot exceed the integer digits. If it is perfect, there should be a check. I am not very clear about the Exception Handling Methods During feature writing. Is there a red error underline when I press Enter during code writing?

 

DefaultAttribute:
Default Value annotation. When the attribute is not assigned a value, the system uses this default value to store it to the database.

HtmlTextAttribute:
Html text annotation, indicating that this attribute allows html-style text

LabelAttribute:
Label annotation for Automatic Generation of form code

Note the relationship between this feature and ColumnAttribute. There is also a Label guy in ColumnAttribute.

LongTextAttribute:

Long text annotation that identifies the data column corresponding to this attribute and allows long text strings to be accepted

MoneyAttribute: currency annotation, which can save data of the currency type.
This annotation can only be used in the decimal data type of dotnet. When the database is stored, the precision is: the total number of digits 19, four digits after the decimal point
This feature is a special case of DecimalAttribute. You can also write it as DecimalAttribute.

However, MoneyAttribute may not be suitable for currencies without decimal points, such as Japanese currency.

 

NotSaveAttribute:
When an ORM saves data, it ignores the attributes marked with the NotSave annotation.

 

NotSerializeAttribute
During json serialization, the attributes annotated with NotSerialize will be ignored. NET serialization also has the same [NotSerialize] feature. This feature is not required for serialization.
 

 

AttributeUsage:
 

Table Name annotation, used to identify the table name of the object in the database
TinyIntAttribute:
 

Small integer annotation that identifies the data column corresponding to this attribute as a small integer
Reflection is used in many aspects of wojilu. However, because wojilu has a complete cache system, the performance loss is negligible.

 

 

 

 

Do while (time ++ ){
Love ++
}


 

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.