Welcome to MSDN Blogs Sign in | Join | Help
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.

Posted: Tuesday, March 03, 2009 2:02 AM by AlexJ
Filed under: , ,

Comments

George J. Capnias said:

- 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?

# March 3, 2009 4:13 PM

danieldsmith said:

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.

# March 12, 2009 8:49 AM

AlexJ said:

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

# March 16, 2009 1:49 PM

Meta-Me said:

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

# March 25, 2009 8:03 PM

ADO.NET team blog said:

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

# May 19, 2009 4:42 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# May 20, 2009 8:46 AM

Web开发技术 said:

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

# May 26, 2009 7:08 AM

MemeDeveloper said:

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

# July 1, 2009 5:25 PM

AlexJ said:

@MemeDeveloper,

Thanks for that extra insight!

Alex

# July 7, 2009 8:30 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker