Introducing T4 Awesome

I have always been a fan of code generation.  In 2006, my buddy Steve showed me CodeSmith and  I started using it to build out stub aspx and html files for my views from database tables.  This worked pretty well but there was one catch, sometimes your data model does not exactly match your business model.  I  hit this problem a number of times and began searching for a better solution.  At first I tried creating my own schema provider in CodeSmith that used reflection to inspect my the classes in my business model and allow me to use that meta data in my templates.  In theory this was a good idea but quickly fell apart when I tried it on a real project.  The major problem was the disconnect between my code in Visual Studio and the compiled assembly that my schema provider used to generate the meta data.  Changes to my source files were not reflected immediately in the assembly requiring me to build my solution before CodeSmith could see my changes.  There were also file locking problems that would not allow the assembly to be updated, it was not an easy to use solution.  My search continued and I discover the T4 Framework that was already built into Visual Studio (version 2008 at the time)  so that was great, nothing to buy!  I started trying to learn the framework and use templates in my projects but was not happy with the process.  Coming from a tool like CodeSmith, I was use to a nice UI that allowed me to define parameters and easily manage and execute my templates.  The in the box support for T4 at the time was not very good, you could add templates to a project but executing them was nothing like the CodeSmith experience.  There was no UI to pass parameters into your templates and controlling the files your template creates all had to be handled in your template code.  Still today this experience has not changed much, Microsoft has continued to work on the core plumbing of T4 but has decided to leave the UI for 3rd party vendors to enhance. With my hopes of a better code generation solution dashed I once again searched Google and eventually found this open source project called “Scaffold It” a few years later (now at version 2010).  Scaffold It was much closer to my vision, it used T4 templates, allowed me to define and pass parameters, and control the file outputs of my templates with a UI.  And, instead of stand alone product like CodeSmith, it was a Visual Studio extension.  This was a much nicer experience being directly in Visual Studio.  But the best part was it solved my problem of the mismatch between the data model and the business model.  Unlike CodeSmith, Scaffold It had a special parameter that would read your source files in your project and pass any selected class as metadata to your template.  It did this by using the DTE CodeModel interfaces available to extension developers. This gave Scaffold it a great advantage over my CodeSmith schema provider since it had access to my live code and not a compiled assembly.  I thought my search was finally over and I began using Scaffold It on the very next work project I could.  After a bit of real world use I quickly developed a love/hate relationship for Scaffold It.  Template management was not that great, I constantly found myself having to manually open the template files from the File/Open menu where I had to drill down in directories to find them.  I also was not fond of the UI layout and the way parameters were defined.  Even with these negatives it was still a better solution than my CodeSmith one so I continued to use it until I upgraded to the next version of Visual Studio in 2012.  Scaffold It was released in 2010 and only supported the 2010 version of Visual Studio.  I attempted to manually upgrade the project install manifest so that it could be used with 2012 but ran into too many errors and had to give up.  I decided to once again look for a better solution and came up with nothing that matched my vision.  So I had two choices, give up or create my own.  I decided to give it shot and create my own.  I create T4 Awesome using the years of experience I gained in trying to find and use other solutions.  T4 Awesome is a Visual Studio extension that helps you use the built in T4 Framework to generate code/text in your projects.  Currently in beta, T4 Awesome has a number of features and supports Visual Studio 2012 or greater.  If you are still reading this I assume you are interested in code generation and I would like to invite you to try out T4 Awesome yourself.  All you have to do is sign up for an account on the T4 Awesome website and follow the getting started guide.  Feel free to let me know what you think by posting comments here.

Leave a Reply

Your email address will not be published. Required fields are marked *