Code generation has been an integral part of software development. The idea that a piece of code will generate additional code is both fascinating and time saving. In this article we will demonstrate how to generate code using T4 Templates.
Getting Started with T4:
The first task you need to perform is to download the T4 framework which is approximately 3GB in size. Just kidding!
If you are using Visual Studio 2008 then you already have T4 templates available to get started with code generation. Create a new project in your solution. You can use any project type for your application. We will be using a console application project.
Add a text file in your project and give it a ".tt" extension. You will see a message box pop up which will give you a warning about the execution of the file. Ignore that and click the OK button.
Congratulations you are now ready create your first T4 template.
Getting Your Feet Wet with T4 Templates:
Let's start with easy stuff. We are going to generate code to call Console.WriteLine multiple number of times. Open the .tt file and type the following code:
The above code is our T4 template. It contains some static text and also text that will be executed once the template is run. All the code inside the <# #> will be executed by the template. As, you can see that inside the Foo() method we are running a for loop. The loop is contained inside <# #> which means that the loop will be evaluated and the body of the loop will be executed. The body of the loop contains the Console.WriteLine statement with the message "Hello T4 Templates".
As, soon as your save your template it is executed and the .cs file associated with the template is updated. Here is the generated code:
This was a very simple example which demonstrated the power of the T4 templates. Let's see how we can push it to the next level.
Generating Entity Classes Using T4 Templates:
A lot of complicated applications are based on the framework associated with entity classes. You can handcode the entity classes which is less fun and more work or you can use T4 templates to generate entity classes for you. In this example we are going to demonstrate how to create entity classes which maps to your database table.
The first thing we need to do is to add assembly references, import namespaces and set the template language type.
Now, we will initialize our containers, connections and commands.
Our task is to get the name of all the tables from the databases. Once, we have all the tables we will iterate through the table schema and get the columns. Using the columns we will generate the properties for our entity classes.
It will be much easier if we take a look at the complete T4 template and then dig into the details of each step.
The first foreach loop goes through all the table names in the schema. The table name is contained in the row["TABLE_NAME"] column. Inside the class we use SqlCommand object called command to execute queries against the table name to get the table schema. The schema is returned in the form of DataColumns. We iterate through the columns and create our private fields and public properties. There is some special code used to create camel casing for the private fields.
As, you can see we only needed a few lines of code to achieve this solution. If we were using CodeDom then it would have been a lot of extra code.
Here is part of the entity classes created using the above templates:
You can start using the generated classes by adding a reference to the desired namespace as shown below:
Conclusion:
In this article we learned how to generate code using the T4 Templates. T4 templates can be extended in many different ways to fit according to the needs of the developer. In later articles we will see different aspects of the T4 templates and how it increases our productivity by generated code.
[Download Sample]