t4talk is the source repository for all samples of the T4 team blog.
T4 is a flexible code generation tool that can reduce development time and maintenance cost. T4 is easy to adopt in any project and in any stage of product development.
If you haven't done so already, please visit the T4 team blog at http://blogs.msdn.com/b/t4/
For a simple example
where T4 can help development, consider exception classes in C#. We like to have specific exception types for the errors in our application. Exception classes often are implemented similar to the following (comments and implementation removed).
public class TestException : System.Exception
public TestException ();
public TestException (string message);
public TestException (Exception innerException);
public TestException (string message, Exception inner);
protected TestException (
public override void GetObjectData (
This code reeks of redundancy as what I wanted to say was just: Give me a new exception named TestException that inherits System.Exception.
Yet, I have to write all of the above and more as it needs implementation and comments as well.
Redundancy is wasteful because it reduces productivity, maintainability, quality, consistency and stops us from doing the right thing. If you have ever sat in front of ~10,000 lines of redundant code and wanted to apply a refactoring to 30% of the code, you would appreciate a tool that can make such tedious and mundane tasks disappeared.
Code snippets are not the answer as they only address part of the problem (productivity) and ignore the rest.
Wouldn't it be great if I could say something like: Exception ("TestException", baseName:"System.Exception")
and all the tedious details are just automatically generated for me? T4 can do just that.
Since it's generated every time in the same way for all exception classes, it's maintainable, consistent and you can refactor the exception classes effectively.