In the latest episode of our Meet the Author podcast, Fritz Onion speaks to Dustin Davis about his course T4 Templates. In the interview Dustin explains how T4 can be used for more than just source code generation as well as how it compares to writing other language applications. He also discusses some of the topics covered in his course such as using templates to keep source code updated with an external resource.
Listen to the Audio (MP3)
[Fritz] Hello. This is Fritz Onion with another Pluralsight author interview. Today we’re talking with Dustin Davis about his new course T4 templates. Dustin is a co-founder of Developer Advocates, a software consulting firm. He’s a PostSharp MVP and speaks frequently about aspect oriented programming. This is Dustin’s first course with Pluralsight. Hello, Dustin, it’s good to have you here today.
[Dustin] Hi, Fritz. Thanks for having me.
[Fritz] So I have to be honest with you, when I hear the word T4 what comes to mind is not really a code generator, but a futuristic robot who’s taken on Arnold Schwarzenegger’s form. But I understand it’s actually an acronym, is that right?
[Dustin] Yeah, that’s right. It’s an acronym. It stands for text template transformation toolkit.
[Fritz] And, you know, so I’ve run across T4 myself quite a bit and I’ve noticed that developers are often aware that it exists for the tools, but I don’t think a lot of people are aware that it’s sitting there for them to use. There’s a full-blown templating engine built right into Visual Studio that you can actually leverage as a developer. And in your course you give a really nice, concise description of what T4 does. Could you just go over that with us?
[Dustin] Well, T4 is actually a dynamic text generation engine. So it can produce not only source code, but regular documents as well. And like I said in one of the videos it’s kind of like a mail merge with super powers. So using these templates we can instruct the text engine on what to produce and how to produce it. And the best thing about T4 though is it lets us use a language that we’re already familiar with being C# or Visual Basic. Which means we have full access to the .NET framework.
[Fritz] Yeah I like that analogy– mail merge with super powers. Now, so I know T4 is used in a lot in Visual Studio for various tools like Entity Framework and MVC, but in your course you focus not so much on the existing tools, but on how to use T4 yourself and create your own templates. Is that right?
[Dustin] Right. T4 is used by a lot of popular products and frameworks and even by Visual Studio itself, but you just don’t see it sometimes. So frameworks like MVC and Entity Framework, they allow customization and extension through the T4 templates. There’s a lot of templates and template packages available online that address many different problem areas, but I always find myself in a one off situation compared to the can templates found online. Or there just isn’t a template available to fit in my needs specifically. As developers use T4 they’ll continually discover new areas where T4 can automate a task or just make life easier like generating the code that they don’t want to write.
[Fritz] I see. And since we’re on the topic perhaps you could tell us exactly what’s involved with creating your own templates in Visual Studio?
[Dustin] Right. The experience of writing a template is very much like writing an ASP.NET page spaghetti code and all. And just like in ASP.NET there’s certain control blocks that have to be used depending on what you’re trying to accomplish. For example the control block for data binding isn’t the same as a control block where you would put a for loop. This is the same in T4. Specific rules apply to each block type and that’s pretty much the learning curve. After that it’s all C# or VB.
[Fritz] Well, so one of the things I really like about T4 as an ASP.NET developer is that it uses expression blocks that are very similar to ASP.NET’s server side blocks. So a lot of what I’m used to doing in ASP.NET works exactly the same way in T4. In your course you go into a lot of advanced topics as well in T4. Could you outline a few of those for us?
[Dustin] The things that can be done with templates are almost limitless. In the real world examples module we built a template that reached through the source code in the project using the CodeDOM API and then generates partial classes based on what it finds. Templates like these are powerful, but they can also be very troublesome too. So it’s important to know how to debug templates when things go wrong. But you can’t debug a template like you do a normal code file. So I show how templates work under the hood and how to troubleshoot common errors and at which stages they occur and how to identify them. And then I show how to attach the debugger to the template execution process and step through the actual execution. In another example we go through a template that build code structures using external resources. Since templates have to be executed manually forgetting to sync up the code when the external resources change is a pretty common problem. So you get stale code blocks. Now Visual Studio doesn’t give us an option to auto execute so I show the viewers how to edit the project file manually so the templates will be executed when the project is built. Because some things we just shouldn’t have to bother with.
[Fritz] Great. It sounds really useful. Well, good. So this was Dustin Davis with his new course on T4 templates. Thanks very much, Dustin. I’m sure our subscribers will find your course as interesting and useful as I did.
[Dustin] Thanks a lot, Fritz. I sure hope so.