How many of you have ever used (or even know of) the Visual Studio Text Templating Engine?

I mean, T4 templates have been around for quite some years now. The where initially included in Visual Studio 2005 SDK, and they where moved to Visual Studio 2008 core just before releasing it.

What I want to know is if this is really used out there. I mean, why doesn't the VS team invested in a descent minimal editor for the templates? It doesn't even have syntax couloring! I know there are a couple of good T4 editors out there, but as they don't come built in, I tend to think that this is an unknown feature for most developers.

Have you ever used T4 templates in any of your projects? I'm really interested in answers that explain how they created their own T4 to accelerate, automate, or generate artifacts for your project.

| improve this question | | | | |
  • 2
    Never heard of it... – Wizard79 Oct 27 '10 at 19:34
  • 3
    @Lorenzo: T4 is a code-generation system similar to CodeSmith, built into Visual Studio. It uses an ASP-like syntax to inject variable values into code templates. T4 is what gets invoked when you open the New View dialog in ASP.NET MVC. It is possible to create your own code-generation templates, but Microsoft hasn't made it very developer-friendly yet. – Robert Harvey Oct 27 '10 at 20:22
  • Why did I first think of T4 Carrier lines? – TheLQ Oct 28 '10 at 0:00
  • Don't make people Google (or ask what it is) - link the first reference to a suitable page. – Peter Boughton Nov 19 '10 at 12:54

I just started to use them recently with Entity Framework in order to generate the SQL scripts and/or POCO classes.

Before that, I'd used String Template for quick code generation. Deklarit's dkts were also a great solution, plus, it had a nice template editor right on Visual Studio (those were the days) :)

| improve this answer | | | | |

We use T4 in our project for several things:

  1. Auto-generating POCO classes in our DAL when our Entity Framework model is modified.
  2. Auto-generating SQL scripts for triggers when our Entity Framework model is modified.

So far they have been working great and have cut out a decent amount of dev time.

| improve this answer | | | | |
  • 1
    EF is exploiding the use of T4 templates – sebagomez Oct 28 '10 at 19:54
  • I'm not sure if you meant to type 'exploding' or 'exploiting' :) – DaveK Oct 28 '10 at 20:35

One of my peer groups migrated a lot of custom templates from Codesmith to T4. They like it pretty well, but they have a couple of people who have been modifying and creating custom templates for years so the lack of tools and polish does not affect them so much. In my team we still use Codesmith.

| improve this answer | | | | |

A few years ago I was part of a couple of projects where we used T4 templates via the Guidance Automation Toolkit as well as the Smart Client Software Factory. On both projects we were able to take out most, if not all, of the repetitive setup steps for creating new modules that would work within the main Composite UI App Block shell applications we had created. The T4 templates we created would ask the developer/user for various pieces of project-specific data and then do everything from initial Visual Studio solution creation to view/controller creation. I agree with you that editing the templates isn't the easiest thing in the world, but the payoff was always worth it for us. There were some add-in's if I remember correctly that would add syntax coloring for you too...

| improve this answer | | | | |

I use the strong typed Page reference template from this article: http://blog.devarchive.net/2008/01/auto-generate-strong-typed-navigation.html

| improve this answer | | | | |

I agree, the support in Visual Studio is extremely lacking but the T4 framework is very useful and flexible. So much so I created an extension that makes creating, organizing, and running templates easier. It supports simple parameter prompting and allows you to completely control the output location of your rendered results. You can download it from my website or the vs gallery. You can use the community edition for free but to use some of the more advanced features requires a full license which cost money.

| improve this answer | | | | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.