Tiny Happy Features #1 - T4 Template Debugging in Visual Studio 2012
(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.
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.
When I start debugging I can set breakpoints and watches and step through just like any other language.
Notice here in the Immediate Window I'm looking at this.GenerationEnvironment. I can see the generation AS it happens in this variable.
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.
About Newsletter
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 :)
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 !
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 :)
I think you're referring to particular T4 templates provided by either some library/framework or something completely unknown to us.
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!
:-)
:-)
:-)
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...
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!!!
So +1.
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.
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
It's not just about code generation, it makes our life easier and safer from typos and mistakes.
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()'
"...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
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!
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.
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!
Senthil
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
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.
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.
Scott: Since most people will not be reading
Will
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.
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
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)
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.
Any idea how to get it back?
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?)
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.
Need better integration with modeling projects.
Cheers
Comments are closed.