What’s new in T4 in Visual Studio 2010


OK, Visual Studio 2010 is well and truly released, so I thought I’d better catalog what goodness is in the box for all you T4 fans out there..

 

For template authors:

  • Preprocessed templates to allow embedding of template-based generation in arbitrary applications without a runtime T4 dependency. There is a new custom tool, new service APIs and new engine APIs to support this feature.
  • Runtime error handling is improved, with better handling of null expression blocks and fewer exceptions being eaten.
  • Include directives and Assembly directives now support both Windows environment variables and VS Macro variables.
  • Include directives and Assembly directives now resolved from VSIX Visual Studio extensions downloaded from VS gallery.
  • Include directives are now also resolved relative to the file that they are declared in to support libraries with relative includes.
  • Templates always use C#/VB 4.0 and .Net 4.0 types – language version is no longer needed.
  • Project/Add item templates for C# and VB and Web projects for both regular and preprocessed templates.
  • T4 now works in Web Site projects.
  • Generate All Files only runs the T4 custom tools in your project, not arbitrary ones like WPF.
  • The service tag project stamp is no longer written willy-nilly by T4.
  • New CompilerOptions flag to the template directive allows arbitrary flags to be sent to the template compiler, allowing optionstrict, optioninfer, optionexplicit etc. to be set for VB.
  • VS’s Custom Tool Namespace is available in CallContext with the tag “NamespaceHint”.
  • Templates no longer support the neutral culture, rather they always go to the invariant culture.
  • T4-MsBuild to transform your templates at build time.
  • The parser requires that templates with a class feature block finish with a class feature block to avoid boilerplate write statements hanging free at the end of the class causing hard-to-debug errors.
  • T4’s assembly set is completely separated from the containing project’s assembly set to avoid picking up the wrong assemblies when a project targets previous framework versions. Project assemblies are no longer used to resolve template assembly directives.
  • Assembly references will resolve from Visual Studio’s PublicAssemblies folder.
  • WindowsBase.dll automatically referenced from templates.
  • Ability to use VS MEF components in the T4 AppDomain – also new CompositionServices helper class to make setting this up simple.
  • The options page is fully localized.
  • Better error messages when templates are included from internet zones or downloaded templates are included.
  • The Generate All Files button is disabled while a build is in progress so there aren’t any clashes.

For custom tool builders:

  • Hosts can implement ITextTemplatingSessionHost to signal to the engine that they can provide a dictionary of arbitrary user data for the lifetime of the processing session. The data is accessed via a new Session property on the TextTransformation base class.
  • The built-in VS host implements ITextTemplatingSessionHost and provides a default session implementation to pass data from custom tools into templates while they run in the secondary appdomain, including resolving any necessary assemblies.
  • New “Parameter” directive with Name and Type properties, similar to the one in the GAX host. This allows custom tools to pass data to templates in a standard way.  It resolves from the Session dictionary, the Host via ResolveParameterValue or the CallContext.
  • T4 interfaces have been split out into a separate assembly to facilitate longer term versioning without taking a dependency on T4’s implementation assemblies.
  • RequiresProvidesDirectiveProcessor uses a new order for processing required parameters to support richer overriding.
  • BaseCodeGeneratorWithSite now exposes both GlobalServiceProvider and SiteServiceProvider members.
  • New base class BaseTemplatedCodeGenerator makes it even simpler to create a T4-based custom tool with one override.
  • ProvideCodeGeneratorExtension attribute makes it easy to hook up custom tools to the web site project system.
  • Public access to the CacheAssembliesOptionsString in the Engine class to help control caching without magic strings.
  • Directive Processors support IRecognizeHostSpecific interface to participate in decisions around forcing this flag on.
  • The correct system implementation of IVsSingleFileGenerator is used rather than a T4 internal one.
  • Access to the internals of the Visual Studio built-in host via ITextTemplatingComponents service interface.

For DSL Tools users:

  • ModelBus support in templates.

 

Phew, that’s a long list – you can see that Visual Studio 2010 is a big release for T4.

I’ll come back and edit this post to include live links as I fill in more details about some of these topics.

 

Comments (2)

  1. Vish says:

    Hi,

    Great to learn that T4 preprocessed templates can be run without VisualSyudio. I need to re-generate code on my build server whic doesn’t have VisualStudio on it. Does this apply only for preprocessed templates or just for regular templates too? Oleg slych’s blog post seemed to suggest that VisualStudio and the Visualiztion addin is required to run the MsBuild target to generate code without source code. If these DLLs & targets are needed, can they be freely redistributed?

    Thank You,

    Vish

  2. Gareth says:

    Preprocessed templates are still transformed in VS, but they just generate simple code that can run anywhere, so you could run that code from a custom build task.

    Regular templates are VS only.

    The targets and tasks in the V&M SDK are redistributable persuant to the license agreement of that SDK.