background preloader

T4

Facebook Twitter

T4 Toolbox. T4 Tutorial: Creating complex code generators. This post is a part of the series that introduces code generation with Text Templates (also known as T4 Templates) in Visual Studio using C# and Visual Basic; explains how to create reusable templates and combine them in complex code generators.

T4 Tutorial: Creating complex code generators

In order to follow examples in this article, you need to have Visual Studio 2008 Standard Edition or higher, SQL Server 2005 or later, T4 Toolbox and T4 Editor installed on your computer. Introduction In the previous article of this series, we created a reusable code generation template called DeleteProcedureTemplate that generates a DELETE stored procedure for a given database table using SMO. We have also created a simple code generation script shown below to generate a DELETE procedure for the Products table in the Northwind database. Visual Basic In its current form, this code generator has several drawbacks. Creating a new code generator Enter CrudProcedureGenerator.tt as the item name and click the Add button.

Reuse the code generator Download. Creating a Custom Tool to Generate Multiple Files in Visual Studio 2005. Introduction Visual Studio is a great environment for Rapid Application Development.

Creating a Custom Tool to Generate Multiple Files in Visual Studio 2005

It also provides a rich extensibility API for customizing it for your specific needs.A good example of this is the highly utilized "Custom Tool" facility. When a file is selected in the Solution Explorer, the property grid displays several attributes, common across any file type. One of these is the "Custom Tool" attribute. When this attribute is set correctly, it refers to an Assembly/Type that can generate a new file (whenever the source file is saved), based upon the source file as input, and store it as a child node of the source file, in the Solution Explorer.Examples of this are the MSDiscoGenerator, MSDataSetGenerator and ResXFileCodeGenerator.

The complexities behind creating and deploying your own Custom Tools are surprisingly few. How to generate multiple outputs from single T4 template. Update: A new version of the code described in this article is available in T4 Toolbox.

How to generate multiple outputs from single T4 template

For details, click here. Overview For some code generation tasks, the exact number of code artifacts to be generated may not be known upfront. For example, when generating CRUD stored procedure for a given database table, you may want to have one SELECT stored procedure created for each index. As new indexes are added to the table, additional SELECT procedures need to be generated. Unfortunately, T4 was designed to generate a single output file per template. Both of these approaches are less than ideal. Strongly-typed DataSet generator compromised between having one source file per code artifact and producing multiple artifacts from a single project item by generating a single DataSet class with multiple DataTable and DataRow classes nested in it.

Walkthrough: Generating Code by using Text Templates. Code generation allows you to produce program code that is strongly typed, and yet can be easily changed when the source model changes.

Walkthrough: Generating Code by using Text Templates

Contrast this with the alternative technique of writing a completely generic program that accepts a configuration file, which is more flexible, but results in code that is neither so easy to read and change, nor has such good performance. This walkthrough demonstrates this benefit. The System.Xml namespace provides comprehensive tools for loading an XML document and then navigating it freely in memory. Unfortunately, all the nodes have the same type, XmlNode. It is therefore very easy to make programming mistakes such as expecting the wrong type of child node, or the wrong attributes.

In this example project, a template reads a sample XML file, and generates classes that correspond to each type of node. Here is the sample file: <? Contrast this with the untyped code that you might write without the template: To create the project. Writing a T4 Text Template. A text template contains the text that will be generated from it.

Writing a T4 Text Template

For example, a template that creates a web page will contain "<html>…" and all the other standard parts of an HTML page. Inserted into the template are control blocks, which are fragments of program code. Control blocks provide varying values and allow parts of the text to be conditional and repeated. This structure makes a template easy to develop, because you can start with a prototype of the generated file, and incrementally insert control blocks that vary the result.

Text templates are composed of the following parts: How to use T4 to generate CRUD stored procedures. This article demonstrates how to use SMO and T4 templates to generate CRUD stored procedures in Visual Studio.

How to use T4 to generate CRUD stored procedures

Overview Stored procedures can be used to implement CRUD (Create, Read, Update and Delete) database operations. As Andrew Novick explains in his article “Implementing CRUD Operations Using Stored Procedures” (Part 1 and Part 2), using stored procedures helps to improve application performance, encapsulate SQL code in data access layer, reduce SQL injection vulnerability and prevent casual table browsing and modifications. The idea is to have a “Create” stored procedure that encapsulates INSERT SQL statement, one or more “Read” procedures that encapsulate SELECT statements, “Update” procedure that encapsulates UPDATE statement and “Delete” procedure that encapsulates DELETE statement. Typically, CRUD stored procedures contain repetitive code and can be generated using information about database schema.

Implementation Attached source code contains the following items. Visual studio 2010 - connect to a database with T4.