How to use T4 template to promote asp.net MVC development speed

Source: Internet
Author: User

Recently because of the need to provide some custom template functionality in the framework, found a blog, unfortunately seems to be the translation tool directly turned, read not fluent, try their own translation, I will not completely translate the original sentence, may be a small scope of the original text I think more appropriate sentences and add some notes, the original address is as follows:

Http://blogs.msdn.com/b/webdev/archive/2009/01/29/t4-templates-a-quick-start-guide-for-asp-net-mvc-developers.aspx

In the ASP.net MVC Release candidate mentioned in our recent blog, we used the T4 (Text Template Transformation) template to implement the Add Toolkit and add View's code generation function. We want to be able to use T4 to increase the speed of development, because users can customize these templates to a large extent.

Location and overriding of templates

Add controller and add view are automatically generated by executing the T4 template in the background. Some tools allow you to modify the template to customize the code you want to generate. The default template location is: [Visual Studio Install Directory]\common7\ide\itemtemplates\[csharp | Visualbasic]\web\mvc\codetemplates\

You can also copy the "Codetemplates" directory to the root of the project to rewrite and customize the template in the above location as a basis for each project (just create a directory named "Codetemplates" and create "Addcontroller" and " subdirectory of the AddView "). There are some templates that need to be overridden in the default directory. Overrides may not work because the matching templates in the project directory will be selected preferentially when code is generated automatically. Another important point to note is that you can add your own. tt files for the Add View feature, either in the public directory or in a stand-alone project, which can be automatically displayed in the Drop-down box in the view selection of the Add View window.

In addition, when you copy the above folder (add a. tt file) to your project, you will have a warning prompt, as shown in the following figure:

(Running these text templates can be harmful to your computer.) If the template source is not trusted, do not run. )

Clicking Cancel will not use the T4 template (if you copy the "Codetemplates" directory and add more than one. tt file, you have selected "Cancel"). Because once the project finds a. tt file, the custom tool (Customtool①) property of the file is set to "Texttemplatingfilegeneraror", which notifies Visual Studio to use the default T4 host (Host②) To execute this template and create a new file based on the content of this template (in the directory where the template resides).

①: The custom tool is a special component that implements the Ivssinglefilegenerator interface defined by Visual Studio. T4 custom tools are implemented by the Microsoft.VisualStudio.TextTemplating.VSHost.TemplatedCodeGenerator class and registered in the registry in Visual Studio Under the name of TextTemplatingFileGenerator. Visual Studio will use this name to find the custom tool, create a COM object, and call its Ivssinglefilegenerator.generate method

②:microsoft.visualstudio.texttemplating.vshost.10.0.dll Texttemplatingservice Service implements the Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost interface and provides host (host) for the code generation engine. In the template conversion process: Engine is responsible for code generation, and host is responsible for providing the specific implementation of the behavior during the conversion process and all the external resources needed. ITextTemplatingEngineHost and template content are passed to the Engine.processtempalte method for code generation

The generator can be a good implementation for one-time file generation based on templates or simple use of T4---but because the Add View and add controller templates for hosts that rely on custom templates all contain code (which is mentioned later), Using the default Builder to execute the template will generate an error. You need to copy the template to the project and then empty the custom tool property values.

Note: If you want to remove replication . TT automatically set the mapping value of the custom tool when you file, you can use the registry to implement --- Note If you want to restore the registry to the default value, you can perform Visual Studio recovery installation, or manually changed back. Start Registry Editor and locate the specified directory (depending on your machine selection is the position or the level):

- bit : Hkey_local_machine\software\microsoft\visualstudio\9.0\generators

the bit: Hkey_local_machine\software\wow6432node\microsoft\visualstudio\9.0\generators

Related Article

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.