T4 templates are yet another way to add generated code via templates into your projects. It plugs directly into VS 2005/ 2008. The idea is generate code output via templates which can then be used directly from your custom application.
A common sample would be to generate a data-layer out of our SQL database. This data-layer (C# or VB code) is then used by our custom application. Here is a sample run to generate class objects from our SQL database.

Add the project type : TXT file with .tt extension
Add a text file
Initially the file does not appear as a template . Change the extension to .tt. Different blocks can be added to the template – more like ASP.Net

Intellisense is available as illustrated in the screen shot
Behind the .tt file a .cs is being generated. This is based on the template file being written (.tt extension). Using the “output extension directive” this can be changed to a VB if you like !
Here is a side-by-side comparion of writting a template file, and the output (.vb file) being produced as we type.
I’ve tested out two options to generate the output code:
1) Direct inline code (also includes writting expressions as loops, etc)
2) Call external source code and (sub) functions
Everything within the brace gets generated as an output

.tt template: Using loops
Note: When accessing external assembly à we need to add the fully qualified name to the template

.tt template: Generate Database objects.Get the names of all the tables in the Northwind database and generate the respective class objects.
Sample output: Respective database classes generated
As an alternative – have a look into the Entity Framework and the WCF Adapters to generate the datalayer for your projects
Reference:
Sample code and videos: http://www.visualt4.com/
Happy coding




