T4 Templates in ASP.NET MVC

asp.net, asp.net mvc 0 comments suggest edit

Here’s a little known fact about T4 Templates…well besides that they exist in the first place. Visual Studio 2005 had support for T3 templates. T4 is the natural successor to T3. T3 is also the abbreviation used when marketing the movie Terminator 3: Rise of the Machines. I heard that the names are related, but I haven’t confirmed it beyond a doubt, which seems appropriate given the title of the movie. But you didn’t hear that from me, deal?

terminator3 Dude looks ready to write some code!

Although they’ve been around forever, I haven’t really dived into T4 templates until recently as I’ve been using them in Subtext. I’m excited that we’re now incorporating them into ASP.NET MVC.

The Visual Web Developer Team Blog just posted an in-depth look at T4 templates in ASP.NET MVC entitled, T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers.

Be sure to check out their other post covering What’s New for MVC Tools in ASP.NET MVC 1.0 Release Candidate.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

11 responses

  1. Avatar for Daniel Cazzulino
    Daniel Cazzulino January 31st, 2009

    Wanna know the true story from one of the key actors? Wojtek holds the key ;)
    blogs.msdn.com/.../414129.aspx
    T3 came out after quite intensive research on template languages, including stringtemplate, nvelocity and others...

  2. Avatar for Thanigainathan S
    Thanigainathan S January 31st, 2009

    Hi,
    Please let me know th procedures to create our own templates.
    Thanks & regards,
    Thanigainathan.S

  3. Avatar for Mads
    Mads January 31st, 2009

    "Dude looks ready to write some code!"
    Very funny :)

  4. Avatar for allan
    allan February 1st, 2009

    @Thanigainathan, you can find what you need here: http://tinyurl.com/adhl43
    ;)

  5. Avatar for Xzibit
    Xzibit February 1st, 2009

    Yo dawg, I heard you like code, so we put some T4 in your code so it can generate code while you code.

  6. Avatar for Mike
    Mike February 1st, 2009

    "John Connor? It is time..."
    When I first heard about T4 I was amazed. Though I don't see a way to use it in my project, I thinks it's great for frameworks and tools. I love how Subsonic uses T4 as well to generate data access classes, too smart!

  7. Avatar for gb
    gb February 1st, 2009

    Trying to get to grasp on how I could increase productivity by using T4 templates to generate code.
    Any example on how to use it in by invoking them from a windows form?
    thanks

  8. Avatar for allan
    allan February 1st, 2009

    @gb: t4 templates can be generated at the command line, so can be integrated into your CI build, or even mapped to a toolbar in the express versions of VS. msdn.microsoft.com/en-us/library/bb126245.aspx It should be found here: <root>\Program Files\Common Files\Microsoft Shared\Text Templating\1.1 As to productivity, the most common uses are for ORM and CRUD type forms, but they can be applied to anything repetitive. With any code generation tool you need to determine whether you will generate once and hand edit forever after, or regenerate whenever your model (or other 'source') changes, overwriting the generated code. MS tends to present the former pattern in Visual Studio with their project and page templates. Most ORM type solutions (including linq2sql) tend to follow the latter pattern. I personally would advise you to follow the latter as much as possible.
    Hope that helps!

  9. Avatar for GB
    GB February 1st, 2009

    @Allen:Thanks for your reply.I have built some in house code generator before linq came on the scene using CodeDom.That was hard work.
    I generate entities,MVP etcc,I have a Gui where you can select table,Column etc... I would like to use T4 Template as they seem easy to maintain.But struggling to find even a noddy example that I can download where you generate anything from a windows form.
    Thanks for your time
    GB

  10. Avatar for Guo Jing
    Guo Jing February 3rd, 2009

    That's very good

  11. Avatar for David
    David December 24th, 2010

    Hi!
    I've used "hand made" tools which performed similar tasks in older versions of VS. Wonder what took so long to realize that this is highly important! Anyways, very nice to have it now.
    DvD