Creating Enumerations from Database With T4

I personally have not spent a lot of time studying T4, but I am aware of tremendous power of the tool.  I ran across a good use case, and I am totally convinced that I will not do another project now without using this code generation tool.

To gets started, I picked up an editor to install.  I just used Extensions Manager from Tools menu of Visual Studio to search for T4 editor, and picked Visual T4.  It provides very impressive development environment, including color coding and IntelliSense.  T4 files are just code files that mix code and text.  When the tool is run, it runs the code in the file, and processes the text in it at the same time.

My task is to create an enumeration in C# that is mirroring data in a table.  My table looks as following:

USE [SampleEnum]
GO

CREATE TABLE [dbo].[EnumTable](
    [ID] [int] NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Description] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_EnumTable] PRIMARY KEY CLUSTERED ([ID] ASC)
)

GO

 

Now, I just choose New Item menu, and select Text File option in my project.  I then name the file with the same name as my class and .tt extension.  Once I open in in Visual Studio, I get IntelliSense support.  On top of the file I have assembly references and using statements, similar to any other plain ol’ C# class.  Syntax is different though, and looks as following.

<#@ Template Language="C#" Hostspecific="True" Debug="True" #>




<#@ Assembly Name="EnvDTE" #>


<#@ Assembly Name="System.Data" #>


<#@ import namespace="System.Data" #>


<#@ Import Namespace="System.Data.SqlClient" #>


<#@ Import Namespace="System.Data.Common" #>

Then, I have a combination of plain text and C# code:

<#@ Template Language="C#" Hostspecific="True" Debug="True" #>


<#@ Assembly Name="EnvDTE" #>


<#@ Assembly Name="System.Data" #>


<#@ import namespace="System.Data" #>


<#@ Import Namespace="System.Data.SqlClient" #>


<#@ Import Namespace="System.Data.Common" #>


/// <summary>


/// User types

/// </summary>
public enum UserTypes
{

<#
SqlConnection connection =
new SqlConnection("Data Source=(local);Initial Catalog=SampleEnum;Trusted_Connection=true");


    connection.Open();

   
SqlCommand commandTotal = connection.CreateCommand();

    commandTotal.CommandType =
CommandType.Text;

    commandTotal.CommandText =
"SELECT Count(1) FROM [EnumTable]";

   
var total = (int)commandTotal.ExecuteScalar();

   
SqlCommand command = connection.CreateCommand();

    command.CommandType =
CommandType.Text;

    command.CommandText =
"SELECT [ID],[Name],[Description] FROM [EnumTable] ORDER BY NAME ";

   
var reader = command.ExecuteReader();

   
int counter = 1;

   
while (reader.Read())

    {

#>


/// <summary>

///
<#= reader[2] #>


/// </summary>

<#= reader[1] #> = <#= reader[0].ToString() #><# if (counter < total) #>,

<# counter++;#>




<#
}

reader.Dispose();

    connection.Close();

    connection.Dispose();

#>



}

The code is pretty self-explanatory.  I just use data reader and command classes to get the data

from my table, then write code as regular enumeration – comment, then enumeration name, then value.  Easy.  Then I just add data to my table, then right-click on tt file and select Run Custom Tool menu.

This is just a small taste of extreme power of T4.  If you never used the tool, take a look, you will not be disappointed.

Thanks

2 Comments

  1. Indeed, there would be. In my case I did not need that because I had a description and name columns, and name column was set up specifically to be a valid identifier in .NET, and description was there to provide content, hence it is output into comments section, which is a nice touch I think.
    Thanks.

Leave a Reply to Daniel Cancel reply

Your email address will not be published. Required fields are marked *