T4 Code Generation

Source: Internet
Author: User

[Original: describeat me to it. Blogging about T4 (Text Template Transformation Toolkit) Had been on my list literally for a year. He and I were singing its praises last night. Hopefully I can add something small to the conversation.

What's the story? Well,T4 is a code generator built right into Visual Studio. To be clear, you have this now on your system... Go play.Now's the time to introduce code generation to your company.If you're doing something twice or more, manually, in your company, generate it.

However, it's not deep-deep built in, because there's no item templates in File | New Item and there's no intelliisense or syntax highlighting.

You don't need this, but if you want really get the most out of T4, first, head over to your us Consulting and get their "T4 Editor Community Edition. "That'll get you some basic coloring. they have a pay version that gets you more if you want.

Now, go into Visual Studio and make a Console App (or any app) and add a Text File, but name it something with. TtExtension. You'll get a warning since it's a generator, that someone cocould generate edevil. Click OK if you are cool with potential edevil.

Now, look in Solution Explorer at. tt file. if you're using C #, you'll have a sub. cs file, or if you're using VB, a sub. vb file. that's the file that will hold the result of the generation. this is the same visual metaphor used to the express the template/generated file relationship. designer files you 've seen elsewhere in Visual Studio.

If you look in the Properties for. tt file, you'll see it's using a mmtool listed as the "TextTemplatingFileGenerator. "This custom tool extensibility point is how things like XSD and WSDL code generators hook in to generate their artifacts.

T4 can generate any kind of text file, not just code. Whatever you like. You can use it in your projects, as abve, or you can call T4 from the command-line.

Aside from Kzu and the folks at least us, Rob notes thatOleg SychHas a great collection of T4 resources. He's got some great Tutorials:

  • Creating your first code generator
  • Troubleshooting code generation errors
  • Debugging code generation files
  • Creating reusable code generation templates
  • Creating complex code generators
  • Reusing code generators on multiple projects

Here's some of the links from Oleg's blog.

  • How to create a simple T4 template
  • How to use T4 to generate. config files
  • How to use T4 to generate Decorator classes
  • How to use T4 to generate CRUD stored procedures
  • How to use T4 to generate strongly-typed navigation class in ASP. NET(By Kirill Chilingarashvili)
  • How to use T4 to generate stronugly-typed AzMan wrapper
  • How to generate multiple outputs from single T4 template
  • T4 template for generating ADO. NET Entity Framework Stored Procedures(By David DeWinter)
  • T4 script for generating ADO. NET Entity Framework Views(By ADO. NET team)
  • T4 template for generating LINQ to SQL Data Context(By Damien Guard)
  • T4 template for generating WiX source files(By New Age Solutions)
  • T4 template for generating SQL view from C # enumeration
  • MSBuild task for transforming T4 templates(By Elton Stoneman)
  • T4 template for generating state machines in C #(By Andrew donews)

Oleg also has a CodePlex project called T4 Toolbox that is a library of T4 templates that get added to File | New Item.

Also, check out Damien Guard's T4 templates that are a wholesale replacement of code that LINQ to SQL generates. here's an example, where I use Damien's T4 templates against the sample Chinook Database.

Notice that I 've named. tt file the same as. dbml, so Damian's code can find it. I also continue to let original LINQ to SQL generate it's. designer. cs file, but make that file's Build Action "None" so it's not ever compiled. that could tively puts Damian's code in charge.

Here's a screenshot showing a bit of Damian's T4 template using the syntax highlighting from the Clairus T4 Visual Studio free download. if I 'd pay them, I 'd get intelliisense and syntax highlightingInsideThe code blocks also. it looks like a lot like ASP. NET Web Forms, or Velocity, or any templating language really. the code blocks are where your logic is and outside the codeblocks is the template for whatever you want to generate. notice how Damien gets input and sets output. you have full control, you can read files off the file system, from with your project, etc. he sets the output extension also. I like to use. g. cs or. g. vb, myself. in this example his generated file is Chinook. generated. cs.

I particularly like Damien's example because he's swapping out parts of LINQ to SQL that he didn't like (the generated code) while keeping the part he did (the general mode, the designer, the dbml file .) if you don't like something, fix it.

Plus, it all works in Visual StudioWithout installing anything.

 

If you're doing Code Gen, or thinking about it, check outT4As it's a great place to start. also, search my blog for "Code Generation" as I was livign and breathing it with CodeSmith for the policyears I worked at Corillian. have fun!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.