Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Visual Studio includes a code generator that creates files from a simple template definition. It has an awkward name; it's known as the Text Template Transformation toolkit. Most of us just call it T4. I am inside Visual Studio, and I have opened the CodeGenWithT4 project. Let's think about what we are going to do for a minute. I have code that I write over and over again that has some replaceable bits. So what I am going to create is a scaffolding of the basics of my code, and then I am going to have these little replaceable parts that the code generator will replace on my behalf.
To create this code template, I need to add a T4 template, by right-clicking and choosing Add > New Item. And I'll type in "template" in the Search dialog and pick this simple Text Template right here, and then I am going to call this one SimpleDemo and click Add. I get warned by Visual Studio that this template may harm my computer. I am going to go ahead and risk it by clicking OK. At the top of this template is some instructions of what to build. It's going to create a C#-specific class, and the extension on the file that it's going to generate is txt.
So if you come over here and look underneath these SimpleDemo.tt file, by expanding this node, you'll see that there is a text file there. I want this to be a C# file, so I am going to change the extensions from .txt to .cs, and then I am going to build the application. I'll accept the risk. And if you look over in the Solution Explorer now, you'll see that this it says SimpleDemo.cs. Next, I'll create the basic scaffolding.
Some of this text is never going to change, like a Using statement, at the top of my screen, using System, or public class Customer. This is known as a text block, which means it's entered exactly as I type it here. Watch what happens now when I do a build. I'll choose Build Solution, click through these dialogs. Then I am going to open the SimpleDemo.cs file, and you see it's starting to generate some code for me.
There are more sophisticated kinds of instructions you can put in your template. You can create something called an expression block. That computes the value, and then inserts it into your output file. You can also create something called the statement block, which allows you to run any arbitrary code. You can also create something called a class feature block, which allows you to embed code sections within the template. Let me show you how to do an expression block. I'll switch back to the template, "public void DoWork".
What the DateTime.Parse () method does is it takes a string and tries to convert it into a DateTime. I am going to get the time from the operating system. So inside these double quotes, I am going to put my expression block. I need the special symbol for T4, which is the open angle bracket, pound sign, and then the pound sign and a close angle bracket. And then I'll use the assignment operator here, equal. I think I've got my code correct.
Let's see if it compiles. Excellent! Now let's go back to SimpleDemo.cs, and you'll see that it created this function and inside the function is this line of code that's declaring a variable, and here is the piece of data that came from the code template generator. Rather than write a bunch of code, I am now going to switch over to this MakeFunction.tt file, which is a more complete example. Here is the same bit of code I showed you few minutes ago. I am also creating a variable here and asking the computer for the machine name. I am then going to declare a variable that's going to hold the list of string, one of the generic types in .NET.
And then look what is happening here. I am calling a function that I declared in my template code. So down here in this section, with this special starter--angle bracket, pound, plus sign, then it ends here on line 36-- I've declared a function called PrintFileNames, which basically goes out, given a folder name, and looks to see if the directory exists. If it does, it walks through all of the files in that folder and prints their name out to my code file, and then I call that function up here. Let's see what happens when I run this example.
Here is that code: var fileName = new List
Then down here, I am declaring a class, and I am saying I want that tableName. Then within the class, I am doing a foreach over the DataMembers--this would be like the column names of the table--and I am writing a little bit of code here to create a public property. So when I run this template, what I end up with is a list of classes that map to the database. That's why they call it Object Relational Mapping. There is a table named Categories. I created a class called Categories. There was a Territories up here, and it has a TerritoryDescription; that's the column name inside the database, and that becomes a public property on my type.
This is a very powerful mechanism. I've used it very successfully on some big projects, where we generate our schemas automatically. And if somebody changes the database schema, the next time we build our code, let's say they change this column name in the database, it automatically changes our codes to reflect the real name inside the database. I think code generation done right, it can be a productivity boost for you and your development team. The included T4 Editor is a little sparse, but you can always install a free Visual Studio extension from a company called Tangible, for better syntax help.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100158 Viewers
56 Video lessons · 113178 Viewers
71 Video lessons · 82038 Viewers
131 Video lessons · 39367 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.