If you aren’t up with the know, T4 Templates or the Text Template Transformation Toolkit is a Microsoft template-driven code generation toolkit baked right into Visual Studio 2008 (and available in Visual Studio 2005 as an additional download) which allows you to do some pretty cool stuff in the way of code generation — that being generating classes, sql scripts, MVC and/or Dynamic Data templates, or anything you like really. It can plug in and read database information quickly and has gained a lot more traction over the last year or so with the adoption by ASP.NET MVC and Dynamic Data really driving it’s name, however unfortunately with the lack of great IDE support, which I’m hoping might change a little come Visual Studio 2010 there is no in-built syntax highlighting or easy way to get started with using it (eg. no “Add new item”). However, it’s a great system and can provide loads of benefit out of the box. (If you would like further reading on the subject, check out Scott Hanselman’s article on the topic posted last year).
Whilst I love the concept of T4, I didn’t write this to discuss that, but rather a community developed tool entitled the T4 Toolbox and specifically their LINQ to SQL Classes Generator template they have designed. But first a little background on the T4 Toolbox from Oleg Sych’s blog (the primary developer of this tool):
T4 Toolbox is a set of ready-to-use code generation templates and essential supporting tools like custom directive processors, packaged in a simple installation program. Each completed code generation template includes a Visual Studio project item template to make it available in the Add New Item dialog of Visual Studio.
So what’s the “LINQ to SQL Classes Generator” I speak about then? Well it’s a component built off the toolbox which offers you an alternate method in generating your LINQ to SQL classes from the traditional MSLinqToSqlGenerator which is the actual process behind the scenes which generates your LINQ to SQL classes normally — fundamentally it will achieve the same thing, that being you receive a set of LINQ to SQL classes and a .dbml to work with, but delivering it in a more enhanced, layered-out and more cohesive pattern over the default single-file .dbml behemoth you typically get.
I will let Oleg explain:
![]() |
However since that was written, the team then introduced in May 2009 the “LINQ to SQL Schema” available right now. This is great for those who have always wanted to be able to develop from a “model up” approach but couldn’t, whereby you create/make changes to your application model in .NET, and have those changes reflect on your database schema, rather than upkeeping the two separately. Also loads more benefits if you are a VS Database Edition or Enterprise Architecture Edition user. A short quote below explains:
|
Some of the important differences between the LINQ to SQL Class Generator and that generated by the MSLinqToSqlGenerator are:
- While MSLinqToSqlGenerator uses fully-qualified, global:: type names, this template produces code with “using” directives and abbreviated names. This is done to make the generated code easier to read.
- While MSLinqToSqlGenerator generates field names as property name with an underscore prefix, this method simply converts the property name to camelCase. This is done for consistency with the StyleCop rule SA1306: Variable names must start with a lower-case letter.
- While MSLinqToSqlGenerator assumes that unknown data types are reference types this method assumes that unknown types are value types. Assuming that users will mostly use built-in types or known CLR types in the model, the most frequently used custom types will be enumerations. Treating unknown types as value types allows us to generate nullable enum types when model defines a nullable property.
- While MSLinqToSqlGenerator always emits InheritanceMapping attributes with inheritance code values enclosed in double quotation marks, making them strings, this generator emits them verbatim, preserving the original data type. This allows using enum values as inheritance code directly, without the need to convert. User can still use string values by enclosing them in double quotation marks in the model/designer.
- While MSLinqToSqlGenerator produces entity code that fires PropertyChanging event with empty property name, this template produces code fires PropertyChanging events with actual property names.
- While MSLinqToSqlGenerator produces a DataContext code that always uses an empty, explicitly created AttributeMappingSource in constructors that don’t receive mappingSource as a parameter, this generator simply calls the matching base constructor.
- Generation of method wrappers for stored procedures and functions is not supported at this time
So all this sounds pretty cool and no doubt you will want to get started straight away to see how it can benefit you.
I think it’s important to note that Oleg states that the T4 templates T4 Toolbox use are stored in C:\Program Files\T4 Toolbox\T4Toolbox\LinqToSql by default so perhaps if there is something you don’t like about the template, or you require additional functionality, such as a standard implementation all your entites derive from, it could be changed in the appropriate .tt file.
RESOURCES:
- T4 Toolbox (http://t4toolbox.codeplex.com)
- T4 Templating Explained (http://www.olegsych.com/2007/12/text-template-transformation-toolkit/, http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx)
- T4 Toolbox – “LINQ to SQL Classes Generator” (http://www.olegsych.com/2009/01/t4-toolbox-linq-to-sql-classes-generator/)
- T4 Toolbox – “LINQ to SQL Schema” (http://www.olegsych.com/2009/05/t4-toolbox-linq-to-sql-schema-generator/)
Remember, this article focusses primarily on the LINQ to SQL Class Generator provided by the T4 Toolbox, but there is tons of other great stuff the T4 Toolbox offers which is all layed out on Codeplex.
Thanks for the great stuff Oleg and Team!

Post a Comment