Condividi tramite


Tip 3 - How to get started with T4

So if you've been reading the Entity Framework Design Blog you will have heard us talk about T4. It is a technology that ships with Visual Studio 2008 (and 2005 as a separate download).

In .NET 4.0 the Entity Framework is leverage T4 to help with Code Generation and Model First scenarios.

In fact T4 is also being used in lots of other MS products now, including ASP.NET MVC and Dynamic Data.

So imagine you want to start using T4 now to get familiar with the technology now. How do you do that?

Its actually pretty simple. You can do some pretty useful things very easily:

  • Add a new text file to your project with the extension renamed to ".tt"

  • Drop some template code in the text file.

    <#@import namespace="System.Collections.Generic" #>
    <#
    Dictionary<string,Type> properties = new Dictionary<string,Type>();
    properties.Add("Age",typeof(int));
    properties.Add("Firstname", typeof(string));
    properties.Add("Surname", typeof(string));
    #>
    using System;

    public class <#="MyClass"#>{
    <# foreach(string name in properties.Keys) { #>
    public <#=properties[name].Name#> <#=name#>{
    get; set;
    }
    <# } #>
    }

    This template code, produces a class with a property for each Key in the dictionary called 'properties'. 

  • Save the ".tt" file. When you do this auto-magically a dependent ".cs" file will appear, that looks something like:

    using System;

    public class MyClass{
          public Int32 Age{
                get; set;
          }
          public String Firstname{
                get; set;
          }
          public String Surname{
                get; set;
          }
    }

As you can see T4 is very simple and should be pretty easy for people familiar with ASP.NET...

Give it a try.

Comments

  • Anonymous
    March 02, 2009
    PingBack from http://www.anith.com/?p=14849

  • Anonymous
    March 03, 2009
    ASP.NET MVC uses T4...Dynamic Data is build on top of T4... Entity Framework will based on T4... There is no support for T4 editing in Visual Studio 2008/2005. Currently, there are no plans, on supporting T4 editing in Visual Studio 2010... (https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=404748)Isn't it a bit odd, something that is used more and more, and there are no plan in supporting it, through Visual Studio?

  • Anonymous
    March 12, 2009
    George has got a very good point there.Just some simple syntax highlighting and an entry in the project's Add New Item dialog for .tt templates would make life easier.

  • Anonymous
    March 16, 2009
    Daniel,Sorry for the delay in processing your Comment. I was on holiday for a week!Yes I agree completely. It would be great if VS shipped with built-in syntax highlighting for .tt files.Alex

  • Anonymous
    March 25, 2009
    Hopefully if you're reading this you've noticed that I've started a series of Tips recently. The Tips

  • Anonymous
    May 19, 2009
    In the first version of the Entity Framework code generation was implemented internally using CodeDom

  • Anonymous
    May 20, 2009
    Thank you for submitting this cool story - Trackback from DotNetShoutout

  • Anonymous
    May 25, 2009
    In the first version of the Entity Framework code generation was implemented internally using CodeDom

  • Anonymous
    July 01, 2009
    One Ammend : This does not work if you add the .tt file to a website, it needs to a be a class library project or console app etc

  • Anonymous
    July 07, 2009
    @MemeDeveloper,Thanks for that extra insight!Alex

  • Anonymous
    March 19, 2010
    Egads, that looks horrible.I'm in the camp that feels the IDE doing initial codeGen is something I can live with, but writing templates for code is something I'd really, really, really want to avoid.I guess I've had far too many MS technologies loose steam/support and projects that used them just a few years before suddenly become completely unmaintainable.I'm a very experienced developer, I think in code and algorithms not in drag-n-drop.   While I like things that make me more productive, I don't if they make my code dramatically less transparent and less stable.