BANGALORE, INDIA: If you look at the work that a normal developer does most of the time, it's a fairly repetitive job wherein he writes code for doing a particular module which is similar to everything else that is done with small differences being say, the fields that need to be displayed/updated to a different table in a different database. There are many other things that are similar as well.
However, there is a way to not just automate this work so that the developer has to only fill in the requirements and the code gets generated, but also ensure that the code is up to standards as defined by policy. This can make the architect's and project manager's, not to mention the developer's life also much better. There is a technology in the .NET world that allows you to do this called T4.
T4 is an abbreviation for Text Template Transformation Toolkit. This is way of writing a 'template' that can generate code from a set of rules, other code, or data input. You can write a template that can be used multiple times with a slight change in input which will output code for different scenarios. Fairly complex scenarios are possible by using this. Let's start with some simple examples and then move on to some more complex ones to see how they work.
Direct Hit!
Applies To: Web DevelopersUSP: Learn how to automate developer's job using T4Primary Link: http://bit.ly/d3vVy1Search Engine Keywords: T4, visual studio
First of all, fire up Visual Studio 2010 (or Visual Studio 2008 with the free T4 Toolbox installed). Note that in VS2008, you will not see an item type in the new item list and will need to create a text file yourself. In VS2010, simply create an 'Empty Project' and then add a new item of type 'Text Template'. This will create a new file with the extension of '.TT'. When the file opens in VS, you will see the following lines:
<#@ template debug="false" hostspecific="false" language="C#" #><#@ output extension=".txt" #>
There are a couple of things to note here. The language attribute in the first line specifies that language that is going to be used for creating the template ? not the language that is the output. The extension attribute in the second line specifies the type of file that is going to be generated. Expand the .TT file you created in Solution Explorer and you'll see a .TXT file with the same name. Now change the value in the extension to say '.cs' and it will change the file as well. Change the .TT file to the following:
<#@ template debug="false" hostspecific="false" language="C#" #><#@ output extension=".cs" #>using System;namespace Hello{class Program{public void Main(){ Console.WriteLine("Hello World"); }}}
As soon as you save the file, open up the associated .CS file. You should see the entire code (other than the T4 directives at the top) generated for you. Congrats, you've just auto-generated your first piece of code.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.