Do You Know About T4 (Text Template Transformation Toolkit) Code Generation In Visual Studio?

by Vahid 14. April 2009 03:15
Working with Microsoft products always has some surprises, now be it a defect, missing functionality or an unknown cool new feature. What's the story? Well recently i was listening to a DotNetRocks show in which i heard the word "T4" which is something regarding to code generation in Visual Studio 2008! i was like, what is this? How come that i have not heard it before? Anyway I was really surprised. So as a code generation passionate i did some R&D about it and here is the outcome.  What is T4? T4 is a code generator built right into Visual Studio. To be clear, we HAVE THIS NOW on our system. So now is the right time to introduce code generation to our company. this means If we are doing something twice or more, manually, in our project, we should stop it and generate it instead. However, there is an issue with this technology and that's the fact that Microsoft does not support it completely as of now and this reason why it is kind of hidden in VS. There's no item templates in File | New Item and there's no intellisense or syntax highlighting. However you don't need this, but if you want really get the most out of T4, first, have a look at "T4 Editor Community Edition." That'll get you some basic coloring.

Now, let's start doing something with T4. go into Visual Studio and create a new Console(or any other type) Application and add a Text File. Then rename it something with a .tt extension. You'll get a warning. Click OK

 

Now, look in Solution Explorer at the .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 with .designer files you've seen elsewhere in Visual Studio. If you look in the Properties for the .tt file, you'll see it's using a CustomTool 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. You can use it in your projects, as above, or you can call T4 from the command-line. In the following example, I use Damien's T4 templates against the sample Chinook Database.

Notice that I've named the .tt file the same as the .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 effectively 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. It looks like a lot like ASP.NET Web Forms. The code blocks are where your logic is and outside the code blocks 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. In this example his generated file is Chinook.generated.cs.

 

anyway i believe this would be great technology for code generation and we will be hearing much more about it. following are some good resource which i have found in internet and thought of sharing them with you. hope you like them:

*       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 *       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 *       How to use T4 to generate strongly-typed AzMan wrapper *       How to generate multiple outputs from single T4 template *       T4 template for generating ADO.NET Entity Framework Stored Procedures  T4 script for generating ADO.NET Entity Framework Views *       T4 template for generating LINQ to SQL Data Context *       T4 template for generating WiX source files *       T4 template for generating SQL view from C# enumeration *       MSBuild task for transforming T4 templates *       T4 template for generating state machines in C# *       T4 Toolbox project in CodePlex

 

Tags: , ,

.Net | Technical

Customizing Entity Framework Classes in VS 2010

by Vahid 17. February 2009 15:32

 need to know about new cool features in visual studio 2010? check this link which describes new features for entity framwork in vs 2010

http://blogs.msdn.com/efdesign/archive/2009/01/22/customizing-entity-classes-with-t4.aspx

Tags: ,

.Net | Learning resource | Technical