Web Design
Mobile Internet
Brand Design
Innovative
News
Encyclopedias

[Beijing website production] NET programmers should not miss such a powerful T4 template

Date:2012-10-20 Source: Shangpin China Type: website encyclopedia
Word Size: small   medium   big

Powerful T4 text template

I hope you can at least realize that even if there is an automatic code generator, changes in database structure and other factors will bring us a lot of trouble. The benefits of putting the. tt file directly into the project need not be mentioned. There are special business requirements, which are implemented with the partial class in. to avoid overwriting the handwritten code when the template file automatically updates the code. ( Beijing website production )

What's the use of T4 text template? When will it be used?

Take a look at the following example and you will understand:

Create a new console application, delete all the files in it, create a new Program.tt file, and enter the following:

  1. < #@ template  language ="C#" debug ="true"# >  
  2. < #@ output  extension =".cs" encoding ="utf-8"# >  
  3. < #  
  4. stringClassName  =  "Program" ;  
  5. # > stringClassName  =  "Program" ;  
  6. # >  
  7. namespaceHelloAngel  
  8. {  
  9. usingSystem;  
  10. class < #=ClassName # >  
  11. {  
  12. staticvoidMain(string[] args)  
  13.  {  
  14. Console. WriteLine("Hello Angel");  
  15. }  
  16. }  

After saving, we will see this style in Solution Explorer:

The Program.cs file is generated by our Program.tt file, and its contents are as follows:

  1. namespaceHelloAngel  
  2. {  
  3. usingSystem;  
  4. classProgram  
  5. {  
  6. staticvoidMain( string [] args)  
  7. {  
  8. Console. WriteLine( "Hello Angel" );  
  9. }  
  10. }  

Look at the following two commands:

  1. < #@ template  language ="C#" debug ="true"# >  
  2. < #@ output  extension =".cs" encoding ="utf-8"# >  

The language in the<# @ template #>instruction specifies the language used by our template to run, which can be VB or C #. Debug declares whether to use debugging. If debugging is not available, it can be set to false

The extension in the<# @ output #>directive specifies the extension of the output file, and encoding specifies the encoding scheme of the output file.

That is to say, we can use C # language to write the logic in this template file. The template will output a file with the same name as the template file, the extension is. cs, and the code is utf-8.

Now let's take a look at the syntax of the instructions:

The syntax of the directive is as follows:

  1. < #@ DirectiveName [ AttributeName  =  "AttributeValue" ] ...  # >  

All attribute values must be enclosed in double quotation marks. If the value itself contains quotation marks, they must be escaped with characters.

A directive is usually the first element in a template file or included file. They should not be placed in code blocks<#... #> Should not be placed in the class function block<#+... #> After.

T4 template instruction

<#@ template [language="VB"] [hostspecific="true"] [debug="true"] [inherits="templateBaseClass"] [culture="code"] [compilerOptions="options"] #>

T4 parameter command

<#@ parameter type="Full.TypeName"name="ParameterName"#>

T4 output command

<#@ output extension=".fileNameExtension"[encoding="encoding"] #>

T4 Assembly Instruction

<#@ assembly name="[assembly strong name|assembly file name]"#>

T4 import instruction

<#@ import namespace="namespace"#>

T4 contains instructions

<#@ include file="filePath"#>

After reading the above instructions, try to write a template file to generate some classes for practice:

  1. < #@ template  language ="C#" debug ="true"# >  
  2. < #@ output  extension =".cs" encoding ="utf-8"# >  
  3. < #@ import  namespace = "System.Collections.Generic" # >  
  4. < #  
  5. List < List < string > classNames  =  
  6. new List < string > ()  
  7. {  
  8. "Hello",  
  9. "Test"  
  10. };  
  11. List < string > callMethods  =  new  List < string > ();  
  12.  # >  
  13. namespace HelloAngel  
  14.  {  
  15. using System;  
  16. < #  
  17. foreach (string className in classNames)  
  18. {  
  19. callMethods. Add(string.Format("{0}.Show();", className));  
  20. # > foreach (string className in classNames)  
  21. {  
  22. callMethods. Add(string.Format("{0}.Show();", className));  
  23. # >  
  24. class  < #=className # >  
  25. {  
  26. ///  < summary >  
  27. ///  < #=className # > Notes for
  28. ///  </ summary >  
  29. public static void Show()  
  30. {  
  31. Console. WriteLine("ClassName: < #=className # > ");  
  32. }  
  33. }  
  34. < #  
  35. # > }  
  36. # >  
  37. class Program  
  38. {  
  39. ///  < summary >  
  40. ///Comments for the entry method
  41. ///  </ summary >  
  42. static void Main(string[] args)  
  43. {  
  44. < #  
  45. foreach (string callMethod in callMethods)  
  46. {  
  47. # > foreach (string callMethod in callMethods)  
  48. {  
  49. # >  
  50. //  < #=callMethod # > Call
  51. < #=callMethod # >  
  52.   < #  
  53. }  
  54. # > }  
  55. # >  
  56. }  
  57. }  

Our general projects are divided into three simple layers. With some experience above, you should think. If only three class. tt files were written, it would be exciting to automatically maintain the entire three layers for us! Indeed, I did a project to share with you:

Look at the whole project now:

There are more than three projects here. I added a UnitTest project and an Adapters project. It's not hard to see the project name. The UnitTest project is a test project. The Adapters project is an entity adapter project. Leave them alone.

With just such a few things, the erection of the third floor has been completed, but it is still in Paramter mode. There is no support for stored procedures. If no one accompanies me, I will add support for stored procedures.

When the project was built, it was built on. net4. Here I changed it to. net2.0. However, I only install visual studio 2010. If you use Visual Studio 2008, please correct the version number in the project file by yourself. There are many methods on the Internet. I won't talk too much.

label: Beijing website production High end website construction



Please contact our consultant

+86 10-60259772

Please provide your contact number. The project manager of shangpin China will contact you as soon as possible.