Automatically Generate Code Using Visual Studio's Text Template Transformation Toolkit (T4)
page 1 of 3
Published: 24 May 2011
Unedited - Community Contributed
Abstract
Visual Studio has built in support for generating code called the Text Template Transformation Toolkit. This article will demostrate how to create a class with a property for each column in a database table. The sample can easily be extended to fit any pattern you use when developing applications.
by Vince Varallo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 5097/ 100

Introduction

How many times have you found yourself creating a class that has a property for each field in a table, or creating select, insert, update, and delete stored procedures for each table in a database?  As a developer even we are subject to such mundane tasks which, as a developer, we are supposed to automate.  Did you know that Visual Studio has a built in toolkit which will allow you to automatically generate code for these tasks and it is free?  If you've installed Visual Studio then you already have it.  It's called the Text Template Transformation Toolkit (T4) and it works right within Visual Studio.

This article will demonstrate how to create a C# class file that is generated by T4.  The class will contain a property for each field in the database.  The example is simple but it will demonstrate the power of the toolkit and you can enhance the code to match the patterns you follow.  The code for the sample can be found here.

The example code uses the AdventureWorks sample database which you can download here.  Once you've installed the database you need to create a SQL Login that the application can use to connect to the database.  My sample code expects a SQL Login to be called "aspalliance" and the password should be set to "aspalliance".


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 8 and type the answer here:

User Comments

Title: External   
Name: Manikandan Ganesan
Date: 6/19/2011 6:26:51 PM
Comment:
Hi,

I used to create Project and Item templates before start any project and that helps me a lot. This is useful information and thanks for posting.

Thanks,
Mani
Title: External   
Name: Pyrros
Date: 6/10/2011 12:54:06 PM
Comment:
How can we use this template to generate code outside this project (from another project)? Something like Add>New Item… and select the template above?
Title: mr   
Name: henry
Date: 5/25/2011 6:28:02 AM
Comment:
Nice one. Pls can u modify it to iclude a method to insert to the able
Title: Mr.   
Name: Ranjan
Date: 5/25/2011 4:37:19 AM
Comment:
A very good and useful thing i have found in this article for my project. Many times we work on the project which is n-tier architecture and coding for business layer is little time taking to create classes for each and every table of database in our project. So it makes it easy for developer to save their time.
Title: Mr.   
Name: S Krupa Shankar
Date: 5/24/2011 10:40:54 PM
Comment:
A very good, short and crisp introduction.

And should it be dt.Columns instead of dt.Rows?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2012 ASPAlliance.com  |  Page Processed at 5/24/2012 10:10:25 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search