Micro-Project Practice (3): entity code generation

Source: Internet
Author: User

In the last two articles, we already have an XML file and generated a database based on this XML file. This time, let's take a look at how to get the initial entity class from this XML file. Or the XML:

 
1:<?XML Version= "1.0" Encoding= "UTF-8"?>
 
2:<Entities Xmlns= "Http://it.ouc.edu.cn/EntityDescription/V2">
 
3:<Entity Title= "Log" Name= "Blog" Module= "Blogs">
4:<Item Title= "Title" Name= "Title" Type= "Text" Require= "True"/>
 
5:<Item Title= "Content" Name= "Content" Type= "Longtext" Require= "False"/>
6:<Item Title= "Category" Name= "Blogclass" Type= "Entity" Entityname= "Blogclass" Require= "False"/>
 
7:<Item Title= "Creation Time" Name= "Createdatetime" Type= "Datetime" Require= "True"/>
8:<Item Title= "Update Time" Name= "Updatedatetime" Type= "Datetime" Require= "True"/>
 
9:</Entity>
 
10:<Entity Title= "Log category" Name= "Blogclass" Module= "Blogs">
11:<Item Title= "Name" Name= "Name" Type= "Text" Require= "True"/>
 
12:<Item Title= "Description" Name= "Description" Type= "Text" Require= "False"/>
 
13:</Entity>
14:</Entities>
 
 
 
 

Modify the unit test used to generate the database script. Add the followingCode:

 
1:/// <Summary>
 
2:/// Construct entity code
 
3:/// </Summary>
 
4:[Testmethod, description ("Construct entity code")]
 
5:Public VoidUtil_createentitycodes ()
 
6:{
7:VaR entities = getentities ();
 
8:VaR basespace ="Dongblog. Business";
 
9:VaR usingnamespace =New String[] {"Dongblog. Common"};
 
10:VaR Path = Gobal. solutionpath +@ "/Dongblog. Business";
 
11: 
 
12:NewLinqentitycodegenerater (). Generate (path, basespace, entities, usingnamespace,False);
 
13:}

Looking at the code above, we can find that we have some preparation work to do before generating the entity code:

    1. Add the dongblog. Common class library. This class library mainly contains verification and query encapsulation and Common Code such as MD5 encryption and conversion of Pinyin initials. For details, refer to the code. We will introduce these classes later.
    2. Add the dongblog. Business Class Library. This class library is the place where the business entity code is stored, that is, the domain model. It includes the base class (entity) of all object classes and the interface for defining data access (ientitydataaccess and idatabase). The definition of data access is greatly simplified by LINQ. In the future, we will focus on analyzing the functions of these classes.

Then run the test to generate the blog and blogclass entities (which must be manually included in the Project ). After the code is generated, the system structure and related code can be seen from the picture on the right.

Now, the structure of the system has become a little complicated. Let's take a look at it again:

    1. Dongblog. Common: Basic settings, including common functions, are equivalent to extension of. NET Framework.
    2. Dongblog. Business: A domain model that defines various entities and their data access.
    3. Dongblog. Test: Test, responsible for testing and automated scripts.
    4. YD: tool module and various code generation tools that can be reused across projects.

The dependencies between the above four modules are as follows:

From the dependency, we can see that dongblog. common provides basic functions that are depended on by all modules; dongblog. business comes from requirements and only relies on common (although business is dongblog. test Generation, but does not rely on test, this point is relatively winding); YD is independent from the project, of course, does not rely on any project module; dongblog. test depends on all modules. We will continue to improve the dependency graph as the project progresses.

If we drop the semi-auxiliary module of common, we can find that business is the core of the system. Because the business logic comes from the needs, it determines other modules of the system, the rest of the system is built around the business logic. Currently, this graph module is relatively small and does not fully reflect this point. After we add data access and the UI Layer, this dependency will become more obvious.

In the next few articles, we will gradually introduce some classes and entity core classes in common, as well as their design ideas.

Download this code

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.