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".