Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Dependency Injection, Debugging added to T4 for Visual Studio 2012

Dependency Injection, Debugging added to T4 for Visual Studio 2012

This item in japanese

There are quite a few improvements added to Visual Studio’s T4 system for Visual Studio 2012. The highlights include MEF support for deploying directive processors and easier debugging.

Directive processors are used to inject code and feed external data into a T4 template. In the past it could be quite tedious to develop and deploy directive processors. With VS 2012 and MEF it is supposed to become much simpler. To start with one needs to implement the IDirectiveProcessor interface. The details are not yet clear, but supposedly the attributes DirectiveProcessor and SupportedDirective handle the heavy lifting.

Preprocessed Templates have been renamed “Design-time templates”. For those of you who don’t know, a preprocessed template is a T4 template that has been compiled into a normal .NET assembly. Once this is done it loses its dependency on Visual Studio and can thus be called from any other application.

With Visual Studio 2012, developers gain the ability to add custom attributes to the classes that represent the preprocessed templates via directive processors. The specific use case in mind is adding MEF exports to the template class.

You can also change the visibility of the template class using the @template directive.

To address a bad interaction between Visual Studio and COM, the new ServiceProviderExtensions. GetCOMService function should be used instead of the normal GetService function. This is only needed when “using host-specific templates to access VS services”.

The final addition to T4 is easy template debugging. In the past you would need to add a debug directive and Debugger calls. Now you set simply set a breakpoint in your T4 templates, right-click on the file, and immediately start debugging.

Rate this Article