Micro-Project Practice (6): code analysis at the business layer -- entity Generation Policy

Source: Internet
Author: User

In the previous article, we analyzed the base class entity of the object class. In this article, we will analyze the object class based on this class.

Each object class consists of two files. We use blogclass as an example. This class contains two files: blogclass. CS and blogclass. designer. CS, which is similar to the code generated by vs itself. more conveniently, vs automatically folds the two files ,.

In these two files, blogclass. Designer. CS contains allCode: Members, attributes, and so on, while blogclass. CS only contains the definition of a class for our code.

The blogclass. Designer. CS code is as follows.

 
1:UsingSystem;
 
2:UsingSystem. Collections. Generic;
 
3:UsingSystem. Data. LINQ;
 
4:UsingSystem. LINQ;
 
5:UsingSystem. text;
 
6: 
7:UsingDongblog. Common;
 
8: 
 
9:NamespaceDongblog. Business. Blogs
 
10:{
 
11:/// <Summary>
 
12:/// Log category
 
13:/// </Summary>
 
14:Public Partial ClassBlogclass
 
15:{
 
16:# RegionID and timestamp
17: 
 
18:Private Int_ Id = new_entity_id;
 
19:Private Byte[] _ Timestamp =New Byte[] {};
 
20: 
 
21:/// <Summary>
 
22:/// Obtain the ID
 
23:/// </Summary>
 
24:Public Override IntID
25:{
 
26:Get {Return_ Id ;}
 
27:}
 
28:/// <Summary>
 
29:/// Obtain the timestamp
 
30:/// </Summary>
 
31:Public Override Byte[] Timestamp
 
32:{
 
33:Get {Return_ Timestamp ;}
 
34:}
35: 
 
36:# Endregion
 
37: 
 
38:# RegionMember
 
39: 
 
40:Private String_ Name;
 
41:Private String_ Description;
 
42: 
 
43:# Endregion
 
44: 
45:# RegionAttribute
 
46: 
 
47:/// <Summary>
 
48:/// Get or set the name
 
49:/// </Summary>
 
50:Public StringName
 
51:{
 
52:Get {Return_ Name ;}
 
53:Set {_ name =Value;}
 
54:}
55:/// <Summary>
 
56:/// Obtain or set the description
 
57:/// </Summary>
 
58:Public StringDescription
 
59:{
 
60:Get {Return_ Description ;}
 
61:Set {_ description =Value;}
 
62:}
 
63: 
64:# Endregion
 
65:}
 
66:}

From the code, we can see that it is completely a translation of the object XML. In particular, the abstract attributes of ID and timestamp defined by entity are implemented.

The blogclass. CS code is as follows:

1:UsingSystem;
 
2:UsingSystem. Collections. Generic;
 
3:UsingSystem. Data. LINQ;
 
4:UsingSystem. LINQ;
 
5:UsingSystem. text;
 
6: 
 
7:UsingDongblog. Common;
 
8: 
 
9:NamespaceDongblog. Business. Blogs
 
10:{
11:/// <Summary>
 
12:/// Log category
 
13:/// </Summary>
 
14:Public Partial ClassBlogclass: entity <blogclass>
 
15:{
 
16:}
 
17: 
 
18:/// <Summary>
 
19:/// Business appearance of log Classification
20:/// </Summary>
 
21:Public Static ClassBlogclassextension
 
22:{
 
23:}
 
24:}
 
 

Er ...... This is simpler. It is completely empty. Because its usefulness is to let us enter our own code. Now let's assume that we have the function "when setting the log category name, if the category description is blank, set the category description to its name, so we can do this: From blogclass. desinger. in the CS file, CTRL + X, CTRL + V are defined for the name attribute, and then change it. The modified code is as follows:

 
1:UsingSystem;
 
2:UsingSystem. Collections. Generic;
 
3:UsingSystem. Data. LINQ;
4:UsingSystem. LINQ;
 
5:UsingSystem. text;
 
6: 
 
7:UsingDongblog. Common;
 
8: 
 
9:NamespaceDongblog. Business. Blogs
 
10:{
 
11:/// <Summary>
 
12:/// Log category
 
13:/// </Summary>
14:Public Partial ClassBlogclass: entity <blogclass>
 
15:{
 
16:/// <Summary>
 
17:/// Get or set the name
 
18:/// </Summary>
 
19:Public StringName
 
20:{
 
21:Get {Return_ Name ;}
 
22:Set
23:{
 
24:_ Name =Value;
 
25: 
 
26:If(String. Isnullorempty (_ description ))
 
27:_ Description =Value;
 
28:}
 
29:}
 
30:}
 
31: 
 
32:/// <Summary>
33:/// Business appearance of log Classification
 
34:/// </Summary>
 
35:Public Static ClassBlogclassextension
 
36:{
 
37:}
 
38:}

This code is easy to understand. It is worth mentioning that when the XML describing the object is modified and the entity code is regenerated, the code generator will judge blogclass. whether CS contains the name attribute. If CS contains this attribute, it is stored in blogclass. designer. in CS, it will not be regenerated. This is the benefit of Self-writing code generation without using a common code generator-I am the master of my website. :)

Another entity-related class is the extension class. As the name suggests, this class contains extension methods for existing types. Using C #3.0 extension methods, you can write very elegant code. For example, if we need to query all the blogs of a certain category and provide the ID of the category, we can write the code in the blogextension class (the code is located in the \ dongblog. business \ blogs \ blog. CS ):

1:/// <Summary>
 
2:/// Log Service appearance
 
3:/// </Summary>
 
4:Public Static ClassBlogextension
 
5:{
 
6:/// <Summary>
 
7:/// Obtain logs by log category
 
8:/// </Summary>
 
9:/// <Param name = "query"> log query </param>
10:/// <Param name = "blogclassid"> log category id </param>
 
11:/// <Returns> logs under this category </returns>
 
12:Public StaticList <blog> getblogsbyclassid (ThisIqueryable <blog> query,IntBlogclassid)
 
13:{
 
14:If(Query =Null)
 
15:Throw NewArgumentnullexception ("Query");
 
16: 
17:ReturnQuery
 
18:. Where (B => B. blogclassid = blogclassid)
 
19:. Orderbydescending (B => B. updatedatetime)
 
20:. Tolist ();
 
21:}
 
22:}

The preceding query method is implemented using LINQ. We also sorted by Update time. With this method, we can use this method to obtain a blog of a specific category: database. getdataaccess <blog> (). getblogbyclassid (1); this method will be further simplified to: database. blogs. getblogbyclassid (1 ).

Finally, the above Code can be tested. Because all our methods use interfaces as parameters, we can use scaffolding (Mock class) to pseudo-implement the corresponding interfaces, in this way, the code in different input and environment can be automatically tested, and the specific method will not be expanded.

Next articleArticleWe will analyze the design and implementation of data access.

Code download

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.