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 (11)

  1. gcapnias says:
    • 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?

  2. Daniel Smith says:

    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.

  3. Alex D James says:

    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

  4. Anonymous says:

    Hopefully if you’re reading this you’ve noticed that I’ve started a series of Tips recently. The Tips

  5. Anonymous says:

    In the first version of the Entity Framework code generation was implemented internally using CodeDom

  6. Anonymous says:

    Thank you for submitting this cool story – Trackback from DotNetShoutout

  7. Anonymous says:

    In the first version of the Entity Framework code generation was implemented internally using CodeDom

  8. Anonymous says:

    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

  9. Alex D James says:

    @MemeDeveloper,

    Thanks for that extra insight!

    Alex

  10. Anonymous says:

    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.