T4 Template Goodness

I must admit I kinda missed the boat with T4 templates and what was capable of them, which annoys me, so I set out to try and have a look at them the other day, but low and behold Karl Shifflett has beaten me too it.

Karl has an excellent blog post http://karlshifflett.wordpress.com/2009/10/30/t4-preprocessed-text-templates-in-visual-studio-2010/ which talks about the new partial class support available for T4 templates in VS2010.

In case you have totally missed the boat, T4 templates are a text template that when saved will run some code. Now this code can do all sorts of things as demonstrated so beautifully by Daniel Vaughan in his excellent T4 EnvDTE article over at codeproject

http://www.codeproject.com/KB/codegen/T4Metadata.aspx

Daniel basically shows how to use T4 to create type safe metadata to get rid of magic strings (which we all hate).

Ok so getting back to T4 basics, T4 uses some classic ASP type syntax and you can embed c# code between special characters. Again see Karl for an example of this.

One thing is for sure T4 looks to be getting a whole lot better in VS2010. can’t wait.

Oh and here are some other good T4 resources:

T4: Text Template Transformation Toolkit

http://www.olegsych.com/2007/12/text-template-transformation-toolkit/

How to create a simple T4 template

http://www.olegsych.com/2007/12/how-to-create-a-simple-t4-template/

Advertisements

5 thoughts on “T4 Template Goodness

    • sacha says:

      I just may do that, though I really like the new Partial class idea, that would help me with a nice idea I had the other day.

  1. Miguel Alho says:

    T4 templates are just amazing! I’ve been using them for the past year as a foundation for code generation for my apps. Using the DSL tools from the VS SDK, and based on a my own convenient meta definition, I get a great part of any app built, especially the middle and data layers, and customize from there. Prototypes get built very quickly. With the right abstractions you can get a really nice framework built up, and very customizable to tecnical and domain specifications.

    I haven’t tested the 2010 precompiled templates as such, but from what I read, it’s a very nice step foward!

    • sacha says:

      I have not delved into DSL tool myself, but I do know what they it can do, and it is very powerful. I agree the new features do look very good indeed.

  2. T4 sure looks to be coming of age in VS2010. Although a better editor included with VS would be nice.
    Thank you kindly for the mention Sacha.

    Cheers,
    Daniel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: