views:

55

answers:

2

I've just started playing with T4, as I eventually want to use it to generate POCO's that map to LINQ to SQL entity classes, but even the simplest example has me slightly confused. Some advice on what is happening, and some advice on some good reading would be greatly appreciated.

I am of the understanding that the following template should produce a file containing only the text Hello World!, but instead it produces a class that outputs the text Hello World! How do I get my plain and simple Hello World only file?

The template:

<#@ template language="C#"#>
<#@ output extension=".cs" #>
class HelloWorld
{

}

The output:

#line 1 "C:\Development\PocoGenerator\PocoGenerator\HelloWorld.tt"
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "10.0.0.0")]
public partial class HelloWorld : HelloWorldBase
{
    public virtual string TransformText()
    {
        this.GenerationEnvironment = null;
        this.Write("class HelloWorld\r\n{\r\n\r\n}\r\n\r\n");
        return this.GenerationEnvironment.ToString();
    }
}

#line default
#line hidden

This is followed by a relatively complex HelloWorldBase class in the same output file. It seems to me that this is an intermediate class that is supposed to be used to generate the actual intended template realization, but what is supposed to happen to make that occur?

+1  A: 

Never seen that... The output in your case should simply be:

class HelloWorld
{
}

Just tested this, and that's what it produces here...

What is the custom tool for your .tt file? You can check this by right-clicking on the file in solution explorer and selecting 'Properties'. It should be 'TextTemplatingFileGenerator'.

In T4, everything between <# and #> is interpreted as code (C# in this case). Similar to ASP.Net's <%= or <%:, you can also use '<#=' to automatically convert a statement to string. So, in order to simply output 'Hello World' you can use either literal text:

<#@ template language="C#"#>
<#@ output extension=".cs" #>
Hello World

or use this:

<#@ template language="C#"#>
<#@ output extension=".cs" #>
<#= "Hello World" #>

or even another way:

<#@ template language="C#"#>
<#@ output extension=".cs" #>
<# var text = "Hello World"; #>
<#= text #>

Now, more fundamentally

  • there are already several T4 implementations for L2S, maybe you should have a look at those (google search on 'linq to sql poco T4' already reveals several links). E.g. http://blog.tonysneed.com/2009/01/16/t4-poco-templates-for-l2s-and-ef/
  • you might also consider using Entity Framework instead of L2S. There's a POCO template available out of the box.
jeroenh
Thanks. For some reason, the custom tool was 'TextTemplatingFilePreprocessor'. I changed to to -Generator and all works as expected.
ProfK
+1  A: 

ProfK,

You are looking at results produced by a preprocessed template. If you choose a regular "Text Template" in the new project item dialog of Visual Studio, you will see the complete transformation results.

Oleg

Oleg Sych
You sir, are a star!
ProfK