Many friends are not familiar with the T4 template. It helps us reduce the workload, improve our development efficiency, and reduce the probability of errors during project development. Therefore, it is very important for developers to learn the application of T4 templates.
There are too many introductions and materials for the T4 template in the garden, so I will not detail the basic knowledge here, just talk about the specific application and practices of the T4 template in this framework.
1. Create a logical layer Project
2. Add reference
Add the three previously added projects to reference
3. Create a folder for the T4 template and name it SubSonic
4. Set Solution. multipleOutputHelper under the SubSonic folder in the DataAccess project. ttinclude, Settings. ttinclude, SQLServer. ttinclude and the App under the project root directory. copy the four files in config to the location corresponding to the logical layer. For details, see
5. Create the Test. tt template file in the SubSonic folder to practice the T4 template.
6. Compile T4 template instances and use the exercise Template
1. Exercise 1
Let's add several lines of code first.
1 <#@ template language="C#" debug="false" hostspecific="True" #>2 <#@ output extension=".cs" encoding="utf-8" #>3 <#@ include file="SQLServer.ttinclude" #>4 5 using System;6 7 namespace <#=Namespace#> {8 9 }
<# @ Template language = "C #" debug = "false" hostspecific = "True" #> it is a T4 template instruction. It indicates that the language used is C # And the debug mode is not enabled, add the property named Host to the class generated by the Text Template.
<# @ Output extension = ". cs" encoding = "UTF-8" #> it is the output instruction of the T4 template. The file extension generated by the current template is limited to. cs, and the storage format is UTF-8.
<# @ Include file = "SQLServer. ttinclude "#> This is the tool class file of the template. Use the Include command to Include this file in other text templates. The main function of this Code is to Include the tool class (function) of SQL operations into the code.
Using System; this is the text information output in the cs file. You can see the specific functions at a glance and will not explain them any more.
Namespace <# = Namespace #> Create a namespace name because SQLServer. the ttinclude file uses <# @ include file = "Settings. ttinclude "#>, read the configuration information of the SubSonic3.0 template at the same time, so you can directly use Settings. the variable set in ttinclude, which is used as the namespace name. For details, see
That is to say, if you want to use some variables in the template, you can define them in these tool classes or configuration files.
Click Save to generate the Test. cs file:
2. Exercise 2
Through Exercise 1, we understand the simple principle of code generation using the T4 template, so we can add some practical content to see the effect of template running.
Code first
1 <# @ template debug = "false" hostspecific = "True" language = "C #" #> 2 <# @ output extension = ". cs "encoding =" UTF-8 "#> 3 <# @ include file =" SQLServer. ttinclude "#> 4 5 using System; 6 7 namespace <# = Namespace #>{ 8 <#9 var tables = LoadTables (); 10 // traverse all tables 11 foreach (var tbl in tables) {12 #> 13 17 public class <# = tbl. cleanName #> Table {18 /// <summary> 19 // Table name 20 /// </summary> 21 public static string TableName {22 get {23 return "<# = tbl. cleanName #> "; 24} 25} 26} 27 <#28} 29 #> 30}
Var tables = LoadTables (); LoadTables () is SQLServer. functions in the ttinclude tool class are used to obtain all tables and fields in the database. (the Code of the file has been modified and all tables and views can be obtained)
Foreach (var tbl in tables) traverses all tables
<# = Tbl. CleanName #> read Table Name
Public class <# = tbl. CleanName #> Table uses the Table name + Table as the class name.
Click Save to generate the Test. cs file:
3. Exercise 3
Through the above exercises, we can see that using a few simple lines of code can easily generate the code we want and reduce the copy and paste operations, of course, the above generation is too simple. We want to use the class generated in this template to reduce strong encoding, so we need to get the names of all fields.
1 <# @ template debug = "false" hostspecific = "True" language = "C #" #> 2 <# @ output extension = ". cs "encoding =" UTF-8 "#> 3 <# @ include file =" SQLServer. ttinclude "#> 4 5 using System; 6 7 namespace <# = Namespace #>{ 8 <#9 var tables = LoadTables (); 10 // traverse all tables 11 foreach (var tbl in tables) {12 #> 13 14 public class <# = tbl. cleanName #> Table {15 /// <summary> 16 // Table name 17 /// </summary> 18 public static string TableName {19 get {20 return "<# = tbl. cleanName #> "; 21} 22} 23 24 25 26 // Add code to traverse all fields in the table cyclically and create the corresponding function 27 <#28 foreach (var col in tbl. columns) {29 #> 30 // <summary> 31 // <# = Replace (col. description) #> 32 // </summary> 33 public static string <# = col. name #> {34 get {35 return "<# = col. name #> "; 36} 37} 38 39 <#40} 41 #> 42 43 44 45 46} 47 <#48} 49 #> 50}
Foreach (var col in tbl. Columns) traverses all fields in the table and obtains the field structure.
<# = Replace (col. Description) #> in this Code, col. Description is used to obtain the word estimation comment. The Replace function replaces the linefeed in the field annotation (Description) with the corresponding format.
<# = Col. Name #> obtain the field Name.
Click Save to generate the Test. cs file:
4. Exercise 4
Is it easy to use up the exercises above. In the case of a small number of data tables, this generation is completely normal, but the tables are put in a file after they are too many, and problems may occur during DEBUG debugging, the main reason is that the number of lines of code is too large, so we have another solution, that is, file generation.
Code first (some content in the previous example will be deleted for better understanding)
1 <# @ template debug = "false" hostspecific = "True" language = "C #" #> 2 <# @ output extension = ". cs "encoding =" UTF-8 "#> 3 <# @ include file =" SQLServer. ttinclude "#> 4 <# @ include file =" MultipleOutputHelper. ttinclude "#> 5 6 <#7 // obtain all tables and views 8 var tables = LoadTables (); 9 // create multiple files to generate entities 10 var manager = Manager. create (Host, GenerationEnvironment); 11 12 // traverse all tables 13 foreach (var tbl in tables) {14 // determine whether the current table name is disabled (Disabled Can be added to the ExcludeTables string data in the Settings. ttinclude file.) 15 if (! ExcludeTables. contains (tbl. name) 16 {17 // set the output file Name to 18 manager. startNewFile (tbl. className + ". cs "); 19 #> 20 using System; 21 22 namespace <# = Namespace #> {23 24} 25 26 27 <#28 // The output file ends 29 manager. endBlock (); 30} // if (! ExcludeTables. Contains (tbl. Name) Judge to end 31 32} // end foreach33 34 // execute the compilation and generate the file 35 manager. Process (true); 36 #>
<# @ Include file = "MultipleOutputHelper. ttinclude" #> Generate a multi-file tool class
Var manager = Manager. Create (Host, GenerationEnvironment); Create multiple files to generate objects
Foreach (var tbl in tables) traverses all tables, which is placed before using, because each file must have using
Other newly added content is commented out in detail in the code, so it is not described here.
Click Save to generate the Test. cs file:
Complete template code
1 <# @ template debug = "false" hostspecific = "True" language = "C #" #> 2 <# @ output extension = ". cs "encoding =" UTF-8 "#> 3 <# @ include file =" SQLServer. ttinclude "#> 4 <# @ include file =" MultipleOutputHelper. ttinclude "#> 5 6 <#7 // obtain all tables and views 8 var tables = LoadTables (); 9 // create multiple files to generate entities 10 var manager = Manager. create (Host, GenerationEnvironment); 11 12 // traverse all tables 13 foreach (var tbl in tables) {14 // determine whether the current table name is disabled (Disabled Can be added to the ExcludeTables string data in the Settings. ttinclude file.) 15 if (! ExcludeTables. contains (tbl. name) 16 {17 // set the output file Name to 18 manager. startNewFile (tbl. className + ". cs "); 19 #> 20 using System; 21 22 namespace <# = Namespace #> {23 24 public class <# = tbl. cleanName #> Table {25 // <summary> 26 // Table name 27 /// </summary> 28 public static string TableName {29 get {30 return "<# = tbl. cleanName #> "; 31} 32} 33 34 <#35 foreach (var col in tbl. columns) {36 #> 37 // <summary> 38 // <# = Replace (co L. description) #> 39 // </summary> 40 public static string <# = col. name #>{ 41 get {42 return "<# = col. name #> "; 43} 44} 45 46 <#47} 48 #> 49} 50 51} 52 53 54 <#55 // The output file ends with 56 manager. endBlock (); 57} // if (! ExcludeTables. Contains (tbl. Name) Judge to end 58 59} // end foreach60 61 // execute the compilation and generate the file 62 manager. Process (true); 63 #>
Click Save to generate the Test. cs file:
: Http://yunpan.cn/QiBT84dGBaxpU access password 01d6
Copyright:
This article was originally published by AllEmpty and published in the blog Park. You are welcome to repost it. You must keep this statement without your consent and provide the original article link clearly on the article page,Otherwise, the right to pursue legal liability is reserved.. If you have any questions, you can use1654937@qq.comThank you very much for contacting me.
As long as you are interested in learning and making progress together, you can add Q group: 327360708 to this topic.
For more information, please refer to blog: http://www.cnblogs.com/EmptyFS/