Scott Hanselman

Tiny Happy Features #1 - T4 Template Debugging in Visual Studio 2012

August 10, 2012 Comment on this post [67] Posted in Tiny Happy Features | VS2012
Sponsored By

(UPDATE: See other Tiny Happy Features)

At some point soon lots of people are going to start writing these epic blog posts about Visual Studio 2012. They will include LOTS of screenshots (some good and some bad), some small code samples and minimal context. I can't speak for other teams; I can only talk about what we worked on. The <AngleBrackets/> folks in Azure Platform and Tools (ASP.NET, IIS, WCF, EF, Azure much and more) have been putting a lot of work into what I sometimes call "Death by a Thousand Tiny Cuts." It's the little irritants that are as frustrating (or more so) as the big missing features.

Rather than a giant super post (although I'll do that at some point, I thought I'd showcase some Tiny Happy Features* that I or another passionate member of the team worked on just because it made life better. Some of these features might not be obvious or easily noticed. Actually it'll be better that you notice the absence of a tiny cut than the inclusion of a new shiny drag and drop toy. It's all part of a larger goal to make the experience of making Web Applications in Visual Studio enjoyable.

T4 Template Debugging

I've long said that T4 is one of the best kept "secret" features of Visual Studio. T4 (the Text Template Transformation Toolkit) doesn't have a whole pile of people working on it, but developers like Gareth Jones really care about it and we really like it in the Web team. Lots of our projects use and have used T4 to generate code. In Visual Studio 2012 Gareth worked with Tim Malone so Tim could make a T4 debugger! Be sure to check out the T4 Team Blog.

Sure, T4 fans have always wanted built-in T4 Syntax Highlighting, but in this case, there were already a great T4 SyntaxHighlighting solution or three good options and there was no FREE debugger. Until now.

T4 in Visual Studio 2012 has Debugging!

You can now right-click on a T4 templates and Debug T4 template. I've got a breakpoint there on a line within the T4. My own T4 is syntax highlighted with the Tangible Editor.

There's the Debug T4 Template menu

When I start debugging I can set breakpoints and watches and step through just like any other language.

Debugging a T4 template

Notice here in the Immediate Window I'm looking at this.GenerationEnvironment. I can see the generation AS it happens in this variable.

Viewing T4 output

If you like T4 and you want them to know, leave a comment below and I'll use your comments to bludgeon convince management that we should continue to invest.

* Ya, some features aren't Tiny, but some are refinements and I like saying Tiny Happy Features. Sue me.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
August 10, 2012 13:17
I absolutely love the T4 templates. I've created lots of template to generate a big amount of our layers automatically (our own DSL) and it saves us tons of time! I can only recommend this to every new (bigger) project to invest a little time into codegen using T4 templates, you'll be glad you did in the long run. Debugging them will be a great step forward when we migrate to Visual Studio 2012 (hopefully soon :) ).
August 10, 2012 13:22
Long time reader, first time commenter.

I love T4 Templates and use them extensively, however one of the "tiny paper cuts" that breaks my heart is that the autogenerated code autogenerates a base class for the Template class to inherit from.

This means my partial classes that add context and functionality to the template cannot inherit from a base class.

It is (IMHO) a questionable design decision. If you're generating the code, why not generate everything you need in one class?

I know it's not your area mate, but you did mention T4 Templates :)
August 10, 2012 13:23
Brilliant! Autogenerating certain types of classes using T4 saves us a lot of time, but developing and maintaining the T4 can be a bit of a pain point sometimes - this will make those things easier.
August 10, 2012 13:31
This is great news! T4 templating is a powerful tool I've used in many projects but it can be very hard to set up and understand why it doesn't work. Hopefully it's going to be much easier with a debugger!
Ni
August 10, 2012 13:37
T4 templates are an amazing feature. We use them to create multiple artifacts for our products and we also use them within our custom code generation framework, replacing our own templating system. The speed at which we can generate code with them is astounding!

They are a superb way to generate domain specific code either with, or without, the excellent DSL tools!

I do have a few things that could be improved, but that's not for here :) But MS definitely need to continue to invest in development of T4 !
August 10, 2012 14:07
Sweet! Whilst it has always been possible with two instances of visual studio in the past (one attaching to the the other which is running the template), this is a much needed improvement.

Would be great to see syntax highlighting be a native part of VS2012 - plugins are a OK, but to me you're signalling that T4 is on it's way to becoming a first class citizen, so would be great if you could give it full citizenship :)
August 10, 2012 14:18
Binary - I don't understand. My example doesn't have a base class. What part of T4 generates something you don't like. Please forgive my ignorance.
August 10, 2012 14:21
Great news! I'd like to see inbuilt syntax highlighting too. The 3rd party editors are ok, but the quality needs to be better.
August 10, 2012 15:21
Well of course we love T4 because it makes our lifes so much easier and prevents us from putting typos in repeated tasks and helps us code faster by using reusable templates. But I suppose that syntax highlightig (although provided by third party tools) would work better when provided within VS since VS supports this file format. This is like adding third party tools to syntax highlight C# or Javascript or CSS or any other code that VS does out of the box. T4 should be no exception.
August 10, 2012 15:24
I don't use T4 for a lot of development but T4MVC is a huge help when coding calls to routes, helpers, redirects etc Without it refactoring of controller/action names is very hit and miss.
August 10, 2012 15:27
@Binary: T4 doesn't enforce any base classes to your generated files. Nobody even said that T4 should spit out code. It could as well be content. What base class can content have? None of course. Content is content. One could easily generate a text file with lines each with a random number in it for whatever purpose of their application...

I think you're referring to particular T4 templates provided by either some library/framework or something completely unknown to us.
August 10, 2012 15:33
Like Raf, I've used T4 alot for our own app framework to autogenerate BLL and DAL layers, based on a DSL we created with the SDK. We've updated it a few times and have used it in pretty much every project we've worked on. So much time and code writing can be saved with T4, especially the kind of code that follows the same tried and tested pattern (CRUD ops, etc).

T4 is definately underestimated and not talked about enough, especially for such a powerfull tool.

Debugging T4 is always painful. Small mistakes go somewhat unnoticed and most of the error messages tend to be cryptic. After a while you get used to it (lol).

But please do continue to invest in T4!
August 10, 2012 17:27
Super! A reason T4 syntax highlighting has been so valuable is as a poor man's debugger. Now we get a rich man's debugger. Kudos to Gareth Jones and others for caring enough to do something. Long live scaffolding!
August 10, 2012 17:56
Yes! I love T4 - and that debugging is built into Visual Studio it will be even easier to use than [an unnamed templating tool, rhymes with "ModeSmith"].

:-)
August 10, 2012 17:56
Yes! I love T4 - and that debugging is built into Visual Studio it will be even easier to use than [an unnamed templating tool, rhymes with "ModeSmith"].

:-)
August 10, 2012 17:56
Yes! I love T4 - and that debugging is built into Visual Studio it will be even easier to use than [an unnamed templating tool, rhymes with "ModeSmith"].

:-)
August 10, 2012 18:02
I love T4! The only real barrier for adoption in our company (until now) has been in debugging the templates! This is an awesome leap forward and I really do hope that Microsoft keeps improving T4.
August 10, 2012 18:10
I would love to be able to watch it spit out code as it goes. That looks great.

One more thing to look at would be the generation of multiple files from a single tt file. It can be done, but it doesn't feel right. It's not a huge pain, just a tiny little cut...
August 10, 2012 18:20
Love T4 templates - they are the backbone to most basic object mapping systems, and make life a little easier.
August 10, 2012 18:28
I discovered the T4 Templates when I found SubSonic. And it opened my eyes to the power of "writing code that writes code". This scratched the same itch that writing SQL that writes SQL does.

In my life, I used T4 to generate a simple tool that would parse a FreeMind MindMapper file (which is persisted as XML) and turn it into a decision tree. Using FreeMind as a UI, I was able to empower non techinical users to build dynamic text generation algorithms, complete with verbs like "CHOOSE", "APPEND", and "RANDOM" which determined whether to pick a single node, append all nodes, or randomly choose a node based on conditions respectively.

Once the non technical user was done, they would check in their changes, and then the automated build would check out the file, run my T4 utility to parse it and turn it into C# code. Then the code file was checked in, compiled and tested. The NON Tech users would actually have the ability to break the build and need to fix it themselves! How cool is that?

To this day, it's one of my greatest professional accomplishments.

Thanks T4 team!!!
August 10, 2012 18:58
Big fan of T4. Please continue to make productive improvements like this one.
tb
August 10, 2012 19:30
Great news! I love T4 templates and this will make them much easier to use.
August 10, 2012 19:47
We use T4 as an ORM of sorts. In our case, it replaced CodeSmith. It generates stored procedures, database layer, service layer and even some front-end stuff (as a starting point that we then hand edit).

So +1.

August 10, 2012 20:03
Great to hear that debugging T4 templates in VS2012 will be easier. The old method of debugging (launching a second VS instance to debug the first instance) was cumbersome and rarely ever worked correctly. I owe Gareth and Tim a beer for this one!

As for syntax highlighting... I've tried all three of the third-party T4 syntax highlighters, and haven't really been impressed with any of them. They're buggy, sluggish, and their intellisense is abysmal. I'd kill for VS.Net to have native support for T4 as a first-class citizen.
August 10, 2012 20:31
T4 Rocks. Debugging and more support is needed.
August 10, 2012 20:42
Hi Scott,

I am so glad that the <Anglebrackets/> team got around to tackle the "Death by a Thousand Tiny Cuts.". I always thought "great, now they implemented the next epic feature, but while working with VS you get poked by a thousand little things".
VS2010 was already a big step in the right direction. NuGet was an awesome feature making a lot of workflows a ton easier, and VS2012 will ease the whole process.

Also I am really "proud" of you guys for using awesome open source projects, contributing to them to get even better and not playing "big bad Microsoft" which would have tried to swallow a lot of those projects.
Even placing MVC as open source speaks volumes and just gives a "good feeling".

Keep on the good work at the <AngleBrackets/> team. :-)

T4 Templates Debugging is the last obstacle taken away that prevented me from extensive usage. This is an essential block you need to be able to cope with anything going wrong in the process of using T4 templates.

Thanks
Andreas
August 10, 2012 21:40
Never used it... BUT, I just got an awesome idea like 3 days ago that would only be possible with T4. So I'm happy with the update!
August 10, 2012 22:29
We use it in almost every web project such a great feature and best kept secret ever in VS!
It's not just about code generation, it makes our life easier and safer from typos and mistakes.
August 11, 2012 0:34
I use Code Generation in all my projects. Since I first met T4 this is my first choice for Code Generation.
August 11, 2012 1:32
Count me in as another fan of T4. :)

One minor annoyance I've run into with precompiled T4 in VS2010 SP1 is that when XML documentation is enabled for the project, there are two CS1591 warnings that appear to be unavoidable since they are caused by missing XML comments in the generated output of Microsoft.VisualStudio.TextTemplating:

MyTemplate.tt.cs(21726,39): warning CS1591: Missing XML comment for publicly visible type or member 'MyClassBase.ToStringHelper'
MyTemplate.tt(4,31): warning CS1591: Missing XML comment for publicly visible type or member 'MyClass.TransformText()'
August 11, 2012 1:32
I think this is what @Binary refers to...

"...When the template processor parses the template, it builds a class derived from the TextTransformation class in the Microsoft.VisualStudio.TextTemplating namespace. Once the engine finishes parsing the template it instantiates this class and invokes a TransformText method. The processor builds the TransformText method by taking all the code inside of standard control blocks and placing it inside the method. Since it is illegal in C# to have a named method inside of a method, we’ll create an error condition with the above code"

found on http://msdn.microsoft.com/en-us/data/gg558520.aspx

But I could be wrong :D

Mike
August 11, 2012 19:46
I am developing a template library at work to easy the programmers work.
I also post on my blog a suite of .tt files that I have wrote in the past years.
So - debug the template is best!
August 12, 2012 2:30
T4 has come in handy so many times and having a built in debugger is a big help! Would love to see T4 gain a little more prominence among the "big" features.
August 13, 2012 11:25
Just stopped by to let everyone know I could not do without T4. We use it to generate literaly hundreds of classes within our webservices. It saves a great deal of time and it provides us with great flexibility when it comes to adding or rethinking features in our framework.

It allowed us to build multiple cool API features like a fluint API for relations between EF entities.

If we would not have T4 we would have to build or buy tooling, which would cost us dearly. With T4 and the templates already provided by Microsoft we were able to do a lot of work for a fraction of the cost.
August 13, 2012 19:11
This is great news! However, how would it work with precompiled text templates? Since they are run during runtime like regular classes and objects, it it possible to step into the TransformText() method and go straight to the template? Or just the generated code from the template?

The third-party T4 syntax highlighters are okay at best. They've gotten better over the past 3 years I've been using T4, but never that great. I would love to see VS fully support a file type that is specific to .NET and Visual Studio.
It never really made sense to me that it .tt files can really only be used by Visual Studio and yet VS doesn't provide basic syntax highlighting or intellisense for them at all.

My thanks to the T4 team and all they've done with a great product!
August 13, 2012 23:08
Bludhound, Excellent.
August 13, 2012 23:35
Installing VPCSRVR@))*VRTL
August 14, 2012 10:15
I love T4; use it to generate my simple business layer methods based on EF's edmx model. Saves me a lot of boring coding.
August 15, 2012 12:47
We use T4 templates to make skeleton of our UI layer. I love it and hope it will keep improving. Thanks to the team!
August 15, 2012 23:38
Thanks for the post. I'd absolutely love template debugging feature. It'd be nice to have native syntax highlighting & intellisense and ability to generate multiple output files without third-party extensions. I currently use T4 Toolbox (https://t4toolbox.codeplex.com/) extension to generate multiple output files but it doesn't support VS2012.

Senthil
August 16, 2012 1:02
Scott, please let the Connect team review the bug #757600. It's a really insidious bug in ASP.NET 4.5 RTM.

In short, web sites with a lot of files (I don't known the threshold) does not compile right by aspnet_compiler when sources are read from a network share.

Check it.
Thanks
August 16, 2012 1:13
Glat - I can't find that bug. Can you email me?
August 16, 2012 2:19
I use a T4 template several times a year, and it saves my life each time.

As to futures, just keep in mind that T4 is used to generate all sorts of text artifacts. I most recently used it to generate SQL Server user-defined functions.
August 16, 2012 22:06
Have been using T4 since long time, we use it regularly to generate code for business, data access and model/DTO layers of our projects along with stored procedures to select, update, delete and insert.
But the material available on the web for T4 is not much, last time we had a tough time converting the T4 projects from Visual Studio 2008 to 2010 due to breaking changes.

Hope MS puts more resources on this to take it further.

More than the debugger having a built in (not third party) syntax highlighter with intellisense would have been nice.
August 18, 2012 2:11
Instead of In addition to leaving a comment, to convince management that they should continue to invest, I suggest that all interested in T4 also vote on Visual Studio UserVoice - better support for T4 and Visual Studio UserVoice - T4 code generator to create multiple files

Scott: Since most people will not be reading all comments, it would help if you can include these two links on the article itself :)
September 25, 2012 6:25
It rocks T4 is a great tool.
October 02, 2012 19:05
Glad that the T4 features have been enhanced in VS 2012 and would like to see more progress in this direction for the future.

Will
October 06, 2012 7:19
I view code generation as a best practice. Similar to DRY.

I prefer to put common logic in base classes rather than in generated code. However, if using T4 templates with metadata will avoid repetitive work, then that is what I do. It started to pay big dividends very quickly. So I have been putting more and more effort into it. Ironically it has not been saving me time because I reinvest the time saved into building better templates, metadata, and code generation libraries. T4 makes writing templates is quick and easy. Not duplicating metadata is what takes the effort. Now I use it for everything from sql code, Dal, BLL scaffolding, controllers, web apis, javascript view model/ajax code, role based security, multi tenant security, and embarassingly I even generate many of my tests through code generation. I found it surprising how many patterns are repetitive, and how much meta data is reused in an application.

I believe my apps are more responsive to change, and of significantly higher quality because of code generation.

Please keep the T4 features coming. As great as it is, it seems to have only touched the surface of what could be done.
October 18, 2012 2:54
T4 Rocks it is a very productive tool
November 07, 2012 0:06
I'm extremely happy that T4 debugging is available now. I initially introduced this tool to my organization when I realized we didn't have enough Code Smith licenses for all our developers. Since then, we've been relying on it heavily in conjunction with T4 Toolbox for some massive amounts of code generation - all of it tightly integrated into our code solutions. Please continue to support this awesome tool.
Dan
November 09, 2012 14:08
Hi,

I wonder if there is a way to run multiple TT files at once.

We use tt files to generate managers, filterextensions, repositories and Viewmodels.

So when changing the model i want to run all the different tt files.

Frans
December 05, 2012 2:41
I love and use T4 extensively to generate proxies for UPnP services directly from the official service XML published by the UPnP forum. In actual fact, I generate control point and device side code in a range of different langues - c#, java, javascript, c, and c++.

UNFORTUNATELY, I cannot use the MS T4 executable directly because it does not seem to really support command line invocation. It seems to be overly focused on being invoked directly from visual studio. I have been forced into using a slightly modified version of the clone published as part of MonoDevelop. But as far as I am concerned, command line invocation with the ability to pass some template-aware arguments into the generation process would move this program from being vey good to truly great.

It would also be awesome if MS would open source this project - just feels exactly like the kind of project that MS, in its new and hugely appreciated more open spirit, might like to consider opening up. (PS please open source Powershell as well, pretty please)
December 12, 2012 17:40
Please, don't kill T4 templates... we need it!
December 30, 2012 10:13
I am a big fan of templating. I have been writing custom templates for years to make my programming life easier. It's very cool that you guys are spending time developing such a useful feature. Thanks!
January 05, 2013 0:43
Professionaly used it as a poor Man's Volta to generate Javascript code from .NET code. Been using T4 generated DAL layers and WCF services. Pleasant to use.

Privately used it tot torture the mono .NET compiler by generating all possible Curry function for 0-16 arguments functions. It generates 2*2^16 functions. When I digg up the code from backup(disk crash) I will try again on Windows and blog about it.
February 08, 2013 14:00
I've created an empty VS2012 MVC4 project but when I add a new T4 template to the project (from File -> New) the default TextTemplate1.tt doesn't have any syntax highlighting nor intellisense.

Any idea how to get it back?
February 08, 2013 14:47
Oh I didn't notice the syntax highlighting and intellisense isn't included in VS2012. It should be.
March 10, 2013 17:20
T4 is awesome and your post as well, we are using it in our current projects. You are absolutely right its hidden secret of VS.
April 08, 2013 14:33
T4 is great, I just have a serious issue with VS2012: Whenever a T4 processing throws an exception, VS2012 crashes immediately, not giving any chance to debig. This even happens when Iadd a Debugger.Break(). Am I the only one experiencing this issue?
April 08, 2013 15:01
Ok, as always when crl crashes without any notice it was a stack overflow exception. However having VS surving this by running out of process would certainly be fine.
May 07, 2013 9:34
T4 is great!

I've been using to automate and add more rigour to a existing dev process that would otherwise involve the maintenance of 300 separate .css files.

Managed to do so by wrapping the .tt file in a data context and iterating to a list of records in a database table that has the file specific settings (client specific style guide) in it.

:)

What I would love for .tt to do is to be able to generate multiple files off the one .tt - To be honest, I think it already does (Entity Framework being an example of this - just not sure how - any pointers?)
May 14, 2013 21:14
I'm glad you linked us to the T4 blog, but it seems abandoned. Eleven months since the last post? Not a good sign.
June 06, 2013 17:36
Scott,
So, I've been scouring the web, and haven't found an answer. Maybe you can help. I am working on three websites that all use my custom t4 templates (the "CodeTemplates" directory copied from the ItemTemplates in the visual studio directory). Right now I'm copying them to each project manually as I change them so that they stay in sync. Is there a way to have a shared library of these? I can't change the ones in the VS directory, as I have other projects that shouldn't use my customizations (oh, the joy of being a consultant).

Any help would be appreciated. Thx.
September 02, 2013 21:20
Please, Visual Studio needs to support native T4!!
Need better integration with modeling projects.
September 20, 2013 0:37
T4 Templates are extremely useful. I have only used them a few times, but they have made some difficult problems easy to solve. Please keep investing in them.
October 15, 2013 17:19
If we are able see integrated debugger with it, it would be awesome.
November 01, 2013 15:15
Does anyone know how can one create a custom data annotation attribute, apply it to a model class and then read that attribute values in the T$ template. I am talking about the MVC View and Controller scaffolding .tt files.
Cheers

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.