Code Generation Made Easy
Microsoft works to promote the Text Template Transformation Toolkit, a code generator built into Visual Studio 2008.
A code generator built into Visual Studio 2008 is also available with the previous release, a fact that appears to be unbeknownst to developers. Microsoft is making an effort to promote this feature.
Text Template Transformation Toolkit (T4) first shipped with the domain-specific language (DSL) tools add-in for Visual Studio 2005. The DSL tools help coders define the DSL and generate the code of a graphical designer. The designer uses the same underlying modeling technology used by the Class Designer and Distributed System Designers in Visual Studio 2005.
Senior Program Manager Rob Conery describes T4 as "one of the Great Hidden Secrets of Visual Studio 2008" in a recent blog post.
"There are a lot of tools out there that do code generation," Conery says. "Great tools like CodeSmith and Clarius Consulting's T4 Editor. And a lot of people use them. But I always wanted something built into Visual Studio exactly the way T4 works. It's crazy that so many people don't know about it. I sort of wanted to get the word out."
Learning from ASP.NET
The code generation for which T4 is responsible is a process that involves human-authored text templates that spit out code based on the control code and the boilerplate in the template. Microsoft defines a text template as a file containing a combination of text blocks and control logic. The control logic joins the text blocks with data in a model to produce an output file. Text templates are used to create such text artifacts as code files and HTML reports.
Gareth Jones, a developer and architect in Microsoft's DSL tools group, is responsible for putting T4 into the DSL tools add-in.
"We were looking for a code-generation technology for DSL tools, and we knew we needed something that would enable users to customize the code that was generated," he explains. "In the past, one of the failings of CASE tools was that they generated very fixed code, and we knew that that wasn't a mistake we wanted to repeat. I'd worked on an internal system many years ago that reused the ASP.NET engine to do code generation in a very hacky, unpleasant way that you wouldn't want to put into a product -- but it was an internal tool, so it was OK. But it seemed a smart idea to take the lessons of ASP.NET and the way it generates Web pages and apply that to generating code."
||"The truth is, some people don't like code generation, because it's an automated way of doing the job. That just doesn't appeal to some folks."
|Rob Conery, Senior Program Manager, Microsoft
Lost in the Shuffle
How has T4 slipped under the radar of many .NET developers? "Well, we didn't make a big fuss about it. We were concentrating on the features of DSL tools," Jones says.
The reason T4 hasn't gotten much developer mindshare is due to the lack of promotion by Microsoft, says Roger Jennings, principal at OakLeaf Systems, a Northern California-based software-consulting firm.
"The thing about Visual Studio is it's so extensible and has so many great features that it really would take a lot of learning to know all the little things that are in it," Conery adds. "And the truth is, some people don't like code generation, because it's an automated way of doing the job. That just doesn't appeal to some folks."
Why does code generation seem to turn off some developers? "Microsoft object-relational mapping tools depend on code generation, specifically LINQ to SQL and Entity Framework, as do several third-party .NET ORM tools," Jennings says. "Some agile types distrust code generation, because it's usually not optimized for performance. But overall, developers have reacted positively to it."
Forrester Research Inc. analyst Jeffrey Hammond suggests the reason may be more pragmatic. "Developers don't always get a lot of time to sit back and think about how they can build a better mousetrap, because they're so busy catching mice," he says.
In the past people have been wary of code-generation tools, Jones adds, because they expect them to produce large amounts of code that somebody has to maintain by hand. But with features like partial classes and partial methods in Microsoft's programming languages, which let developers split up the definition of code into a generated piece and a hand-written piece, there's absolutely no need ever to change the generated code. That's one more barrier to code-generation removed.
Conery recently used T4 to develop a Model-View-Controller (MVC) add-in for SubSonic, a .NET open source project developed by Conery and a core team of developers that included Eric Kemp, Jon Galloway, Scott Watermasysk, Phil Haack and Gavin Joyce.
SubSonic is a toolset that helps a Web site build itself. It works up the data access layer, throws in some utility functions and generally speeds up the development cycle, Conery says.
The MVC add-in generates scaffolded Views and a Controller using a LINQ-to-SQL DataBase Markup Language metafile repository.
Although he's happy with the results so far, Conery hastens to add that the add-in is just a prototype. "It's not even alpha, but it's working pretty well," he says.
Conery isn't the only one at Microsoft promoting T4. Microsoft Principal Program Manager Scott Hanselman recently posted an extensive look at the code generator on his Computer Zen blog. Conery also points to MVP David Hayden's screencast on the subject, as well as "T4 ninja" Oleg Sych's online article.