Micro-Project Practice (2): Test-driven code generation

Source: Internet
Author: User

PreviousArticleWe have analyzed the two entity classes of the system and given the corresponding entity XML. Let's take a look at how to use this XMLCodeGenerate.

 
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>
 
 
 
 

Here we mainly generate two most important types of code: one is the database creation script and the other is the entity CS class.
We use tests to drive code generation and add a new unit test: util. CS to the project dongblog. Test. The corresponding code is as follows:

1:Using...
 
12: 
 
13:NamespaceDongblog. Test
 
14:{
 
15:/// <Summary>
 
16:/// Database-related tools and methods
 
17:/// </Summary>
 
18:[Testclass]
 
19:Public ClassDatabaseutil
 
20:{
21:/// <Summary>
 
22:/// Construct the database creation script
 
23:/// </Summary>
 
24:[Testmethod, description ("Construct a database creation script")]
 
25:Public VoidUtil_createdatabasescript ()
 
26:{
 
27:VaR entities = getentities ();
 
28:StringSqltext = mssqlserverscriptbuilder. getsqlscript (entities );
 
29: 
30:Debug. writeline ("");
 
31:Debug. writeline ("The following is the database creating Script :");
 
32:Debug. writeline ("====================================================? ========== ========================================================== =====================================");
 
33:Debug. writeline ("");
 
34:Debug. writeline ("");
 
35:Debug. writeline (sqltext );
 
36:Debug. writeline ("");
37:Debug. writeline ("");
 
38:Debug. writeline ("==============================================## ========================================================== =================================");
 
39:}
 
40: 
 
41:PrivateEntity [] getentities ()
 
42:{
 
43:Xmldocument document =NewXmldocument ();
 
44:Document. Load (Gobal. entityxmlfilename );
45:ReturnEntityxmlparser. parsexml (document );
 
46:}
 
47:}
 
48:}
 
 
 
 
 
 

The unit test defined in this class is used to generate database creation scripts instead of testing any functional code. Vs. Net does not seem to have an automated script such as make. It is a little tedious to use third-party components and tools, and the "misuse" of such unit tests can play a good effect. :)

Unit Testing can also provide many functions as an automated script. In addition to the above database scripts, we can also generate entity-class code, generate external xml linq ing files for LINQ, fill in test data, import data from earlier versions of the system, and count the number of lines of code.

Return to our system, run the util_createdatabasescript test in debug mode, and write the database script into the output window (press Ctrl + W and O to open it ).

1:If Exists(Select*FromDBO. sysobjectsWhereId = object_id (n'[DBO]. [fk_blog_blogclass]')AndObjectproperty (ID, n'Isfornnkey') = 1)
 
2:Alter Table[DBO]. [blog]Drop ConstraintFk_blog_blogclass
 
3:Go
 
4: 
5:If Exists(Select*FromDBO. sysobjectsWhereId = object_id (n'[DBO]. [blog]')AndObjectproperty (ID, n'Isusertable') = 1)
 
6:Drop Table[DBO]. [blog]
 
7:Go
 
8: 
9:If Exists(Select*FromDBO. sysobjectsWhereId = object_id (n'[DBO]. [blogclass]')AndObjectproperty (ID, n'Isusertable') = 1)
 
10:Drop Table[DBO]. [blogclass]
 
11:Go
 
12: 
 
13:Create Table[DBO]. [blog] (
14:[ID] [Int]Identity(1, 1)Not Null,
 
15:[Title] [nvarchar] (255)CollateChinese_prc_ci_asNot Null,
 
16:[Content] [text]CollateChinese_prc_ci_as,
 
17:[Blogclassid] [Int],
 
18:[Createdatetime] [datetime]Not Null,
 
19:[Updatedatetime] [datetime]Not Null,
20:[Timestamp] [Timestamp]Null
 
21:)On[Primary]
 
22:Go
 
23: 
 
24:Create Table[DBO]. [blogclass] (
 
25:[ID] [Int]Identity(1, 1)Not Null,
 
26:[Name] [nvarchar] (255)CollateChinese_prc_ci_asNot Null,
27:[Description] [nvarchar] (255)CollateChinese_prc_ci_as,
 
28:[Timestamp] [Timestamp]Null
 
29:)On[Primary]
 
30:Go
 
31: 
 
32:Alter Table[DBO]. [blog]With Nocheck Add
33:Constraint[Pk_blog]Primary KeyClustered
 
34:(
 
35:[ID]
 
36:)On[Primary]
 
37:Go
 
38:Alter Table[DBO]. [blogclass]With Nocheck Add
 
39:Constraint[Pk_blogclass]Primary KeyClustered
40:(
 
41:[ID]
 
42:)On[Primary]
 
43:Go
 
44:Alter Table[DBO]. [blog]With NocheckAdd 
 
45:Constraint[Fk_blog_blogclass]Foreign Key 
 
46:(
 
47:[Blogclassid]
48:)References[DBO]. [blogclass] (
 
49:[ID]
 
50:)
 
51:Go

Here, the generation of database scripts draws on some advantages of ROR, such as adding an id Primary Key and timestamp for each object. In addition, the object data field type corresponds to the specific database type (bool-> bit, text-> nvarchar (255), and the relationship between entities corresponds to the relationship between tables. The specific rules and generation methods can be adjusted based on the project and the database used.

The advantage of automation is that every time we modify the entity XML, we can update the database as long as the database script is re-generated. This is useful in team development, anyone who modifies the database only needs to notify the companion to update the database. Database scripts can be easily obtained when each iteration of the project is completed and released.

The builder code is in the YD. Data. entityprase and YD. Data. databasescriptgenerater namespaces. It is very simple. It is nothing more than a string. You can refer to it and write your own generator as needed (I always think that the code generation tool isProgramBasic skills ).

In the next article, we will study how to generate entity classes and what kind of entity classes to generate.

Code download

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.