What’s new with T4 in the Developer Preview of Visual Studio 11?

In among all the excitement of new ALM features like integrated Code Review and MyWork to manage your daily workflow in the developer preview, there are, of course, a few nice new features for T4.

First of all, a naming change that’s underway.  We’ve heard from you that ‘Preprocessed’ isn’t the clearest choice of name for our templates, so we’ve switched to a new naming scheme:

  • Design-time templates - Templates that run in the VS IDE to produce output based directly on the template content
  • Run-time templates - Templates that are preprocessed in the VS IDE to produce a class that you incorporate into your application for use at runtime. Calling a method on the class produces output based on the template content.

For the Developer Preview, you’ll see this change mostly in our documentation; the new terms will find their way into the product UI in time for Beta.


Now on to new features we’ve added in the preview:

1) Improved template inheritance for runtime templates that need access to features of the host

Previously, if you had a runtime template that declared itself as host-specific,

<#@ template hostSpecific=”true” #>

it was hard to reuse the template through inheritance cleanly.  Derived templates that also declared host-specific would create a second ‘Host” member that would hide the one from the base class, giving a warning.
To address this, we’ve added a third value for host-specific, “TrueFromBase”, that makes the engine generate a host specific template, but expects the ‘Host” property to come from the base class.

We’ve also made the comments generated from runtime templates more helpful.

2) Better error processing for design-time templates

When your design-time template gets past the compile stage and starts to run the compiled code, T4 now accurately reports line numbers for any exceptions that get thrown.
Don’t forget to set debug=”true” to make sure the compiler is emitting line number information.

3) COM Service helper

For folks using host-specific templates to access VS services, there’ve been a few reports of exceptions being thrown for one or two specific services.
Typically, it turns out that these services are written in managed code, but exposed over COM and then marshaled to T4’s AppDomain over .Net Remoting. (Phew!)

This rather complex path leads to the exceptions, so we’ve included a simple extension method on IServiceProvider that you can use to get hold of those services and force them to behave properly:

    public static class ServiceProviderExtensions
        public static Object GetCOMService(this IServiceProvider provider, Type type)

Just call this method instead of the regular GetService() method and your COM troubles should be over.

4. Item templates now support LINQ out of the box.

We’ve updated our VS item templates, so when you add a new T4 template to your VS project, you’ll automatically get all the directives you need to use LINQ right off the bat.

<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>

5. Correct disposal of MEF container when T4 AppDomain recycles.

We provide a simple helper method (CompositionServices.InitializeServices() ) to set up a MEF composition container in the T4 AppDomain if you want to use VS’ MEF components from your template.

T4 has a heuristic that decides when to recycle the secondary AppDomain for design-time templates. This one is really more of a bug fix, but I’ll mention it just because not many folks know about the MEF setup helper.
The fix is that now when the AppDomain gets recycled, the MEF container is correctly Disposed.


We’ll have some more features to talk about once the Beta comes along, but in the mean time, I hope you enjoy these improvements to T4 in our Developer Preview.

Technorati Tags: ,,
Digg This

Comments (4)
  1. Luciano Evaristo Guerche (Gorše) says:


    I wish ASP.net much cleaner Razor syntax be incorporated into T4 templates any time soon.


  2. Any plans on including capabilities like those of T4toolbox & Tangible Editor in the main product?

    T4Toolbox capabilities are key for an effective use of this tool:

    – Multiple file generation from a template (it can be done with the current code base, but it ties files to the template when it should allow to generate them in other folders/projects to facilitate source control).

    – Don't overwrite output files if there is no change (also important for source control)

    – Allow the generation process to output to a folder structure.

    With all the modeling tools available now my time is split like this:

    – 10% designing and maintaining the underlying model

    – 85% designing and maintaining template code

    – 5% doing customizations on generated & standalone code.

    Yet the part where I spend more time is the one that lacks most features (which doesn't help). I pay for VS 11 licenses while use most of my time in a VS 6 type environment.

    I know you guys can do better.

  3. Binoj Antony says:

    Gallaeci is spot on, couldn't have summarized it better.

    May of us are facing the exact same problem.

    Hope that we at least we get some of this in VS2012 SP1 !!

  4. GarethJones says:

    Please use UserVoice to express your wishes for T4 and to get vote consistency.  visualstudio.uservoice.com/…/121579-visual-studio

    There is already a multiple file output feature that you can upvote.

Comments are closed.

Skip to main content