For what purposes have YOU used T4?

Viewed 3057

T4 has existed for several years in Visual Studio, but doesn't get a lot of attention. However, for those that know it, there seems to be some very creative and useful purposes.

I am researching some different ways that T4 is used, and I would appreciate to hear how YOU may have used it for real life scenarios. I am primarily interested in non-standard and creative uses.

Some interesting examples:

  1. Phil Haack uses T4 to create static CSS files from .less
  2. To Generate WPF and Silverlight Dependency Properties using T4 Templates

Note: I realize this is a discussion-oriented question, but the answers could be helpful to others. I have tagged it as subjective and also marked as "community wiki", so please allow the question to remain open. Thanks!

9 Answers

I've used the T4 Templates within the sharp-architecture to generate everything from models to controllers to basic views.

Definitely worth checking out, even if you just want to see some advanced examples of T4 templates in action

SubSonic 3.0 makes heavy use of T4 templates for generating your entity code.

Essentially it calls GetSchema() on your database connection and runs each table it finds through the T4 entity template. The great thing about using T4 here is that if you don't like the way it's handling your database schema, just edit the template.

I've tweaked the T4's to handle MySQL databases better for my situation, as I make use of many tinyint columns which the default T4 maps to byte types. A quick edit to the T4 gave me the type I wanted instead for my application entities.

LINQ to SQL templates for T4
http://l2st4.codeplex.com/

Templates replicating the functionality of the SQLMetal and the LINQ to SQL classes designer code-generators for both C# and VB.Net requiring just Visual Studio 2008.

I've used T4 to generate:

  • proxies (design time, for injecting/wrapping monitoring code/logging/... in a very specific exposed api).
  • interface generation for a one-on-one interface/class mapping
  • replace reflection code by "directly/real" calling code (maintenance advantage of reflection code, but performance of the actual code) for instance when allowing access to properties through an indexer, or something in that direction.
  • xml generation for a java project (couldn't find a T4-like solution for java, that is easily shared within a company, T4 is easy because it's build in and you can run it from command line)
  • generate enums from a master database (we generated them both for a delphi-code base and .Net code base)

T4 Templates are used heavily in the Web Service Software Factory (Service Factory).

Related