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

Software development security check list

by Vahid 6. April 2009 17:11

Recentely i have joined a new as a technical architect. the very thing after getting to the domain was to evaluate the team technical capabalities. so i went through some their codes and found some disaster. i can say that nothing is done regarding to security. i still cannot believe what i have seen. but anyway i shared a handy document about application security with them. hope it will be helpfull for them. so i thought of sharing the same document with you.

To build software that meets our security objectives, we must integrate security activities into our software development lifecycle. I used to use a very handy handbook about security check list. This handbook captures and summarises the key security engineering activities that should be an integral part of your software development processes.

This handbook is a quick reference for developers that summarises the key security engineering activities that should be an integral part of software development processes. These security engineering activities have been eveloped by Microsoft patterns & practices to build on, refine and extend core lifecycle activities with a set of security-specific activities. This handbook provides a snapshot view of the steps necessary to perform each activity, references for additional reading about each activity, and a comprehensive set of security checkliststhat you can use as job aids while developing our software. Audience This handbook provides security activity guidance, checklists and question lists for application architects and software developers who want to improve the security of the applications that they develop. Software developers are the primary audience, but the security engineering activities that this handbook summarises are designed to be used by team members from many different disciplines, including business analysts, architects, developers, testers, security analysts and administrators. The handbook is task-based and is centered on key security activities that you should perform at the various stages of the application lifecycle. The question lists and checklists in Part II of the handbook are job aids and
quick reference sheets that software developers should use when designing and implementing solutions.

 you can download pdf format of the book from the follwoing address:


.Net | Learning resource | Technical

The C# Programming Language Version 4.0

by Vahid 4. April 2009 16:02

 browsing in the internet i came across a blog post about the new C# programming language spesifications (C# 4.0) and thought of sharing them with you. hope you like it.


.Net | Technical