T4 template basics, t4 Template

Source: Internet
Author: User

T4 template basics, t4 Template

Many T4 templates were used to generate code in the educational administration system. So what exactly does the T4 template do? Why should we use the T4 template? Here, we will briefly understand its role.

When creating a T4 template, the following code segment is displayed by default:

<#@ template debug="false" hostspecific="false" language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #><#@ output extension=".txt" #>
The above code is the instruction block in the T4 template, that is

Template instruction: indicates that the C # code template, assembly instruction, import instruction, output instruction, and parameter instruction and include instruction are used here. I did not come into contact with so many details during the application process, but I just understood it briefly. Here I will explain a small example in the system:

<# @ Template language = "C #" debug = "false" hostspecific = "true" #> <# @ include file = "EF. utility. CS. ttinclude "#> <# @ output extension = ". cs "#> // output here is. CS file <# CodeGenerationTools code = new CodeGenerationTools (this); MetadataLoader loader = new MetadataLoader (this); CodeRegion region = new CodeRegion (this, 1 ); // region code MetadataTools ef = new MetadataTools (this); string inputFile = @".. \ ITOO. basic. model \ BasicEntity. edmx "; // here EdmItemCollection ItemCollection = loader. createEdmItemCollection (inputFile); // create an EdmItemCollection and load the metadata string namespaceName = code. vsNamespaceSuggestion (); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager. create (this); #> using ITOO. basic. model; // These are text blocks using ITOO. library. core; namespace ITOO. basic. IBLL {<# foreach (EntityType entity in ItemCollection. getItems <EntityType> (). orderBy (e => e. name) // These are the control blocks {#> public partial interface I <# = entity. name #> BLL: IBaseService <# = entity. name #>>{}<##>}

Text block: directly copy to the output content.

Control Block: the program code that inserts variable values into the text and controls the conditions or repeated parts of the text. control blocks cannot be nested in control blocks. This includes changeable parameters, for example, <# = entity. Name #> expressed by <#... #>.

The above are some simple understandings of T4 templates. In terms of functions, they mainly aim to reduce code duplication. This allows developers to reduce repetitive work. For more details, see http://www.cnblogs.com/heyuquan/archive/2012/07/26/2610959.html

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.