Archive for the ‘T4’ Category

Code Generation in .Net

July 3, 2009

When developers think of code generation they think generally of ORM tools like LinqToSql or Entity Framework. My feelings on these technologies are putting it midly, “crap”.

I was listening to an interesting podcast this morning on DotNetRocks with Peter Vogel ( in which he explains other uses of code generation in .Net.

An interesting point he made (and which I think is very applicable) was avoiding to write redundant code. He was not referring to ORM but rather other functions like reading information from a web.config or app.config file. The example he gave was generating code for connection string settings in app.config file. Every time he saves an app.config file, his code generation tool kicks off and generates a ConfigurationManager class which generated code to the settings in the config file.

An example is having multiple connection strings in the config file. For instance, DevConnectionString and StagingConnectionString. His code generation tool will then allow him to access these properties using type safety and early binding. Thus, in code, he can call ConfigurationManager.ConnectionStrings.DevConnectionString or ConnectionStrings.StagingConnectionString and thus avoiding the annoying problems of miss spelling the connection string.

Maybe something to take a look at and invest in. He mentioned a variety of code generation tools in .Net including T4.