News

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."

Rob Conery, Senior Program Manager, Microsoft "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.

Great Beginnings
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.

About the Author

John K. Waters is the editor in chief of a number of Converge360.com sites, with a focus on high-end development, AI and future tech. He's been writing about cutting-edge technologies and culture of Silicon Valley for more than two decades, and he's written more than a dozen books. He also co-scripted the documentary film Silicon Valley: A 100 Year Renaissance, which aired on PBS.  He can be reached at [email protected].

comments powered by Disqus

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube