T4 template to Generate Enums

前端 未结 2 1319
说谎
说谎 2020-11-30 23:30

I\'m looking at creating a T4 template to generate enums of my database. Essentially, I want the same feature as SubSonic e.g. Product.Columns.ProductId for Linq-to-SQL or E

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 23:52

    I have written one for my needs that converts a lookup table of your choice to an enum: Put this code inside an EnumGenerator.ttinclude file:

    <#@ template debug="true" hostSpecific="true" #>
    <#@ output extension=".generated.cs" #>
    <#@ Assembly Name="System.Data" #>
    <#@ import namespace="System.Data" #>
    <#@ import namespace="System.Data.SqlClient" #>
    <#@ import namespace="System.IO" #>
    <#@ import namespace="System.Text.RegularExpressions" #>
    <#
        string tableName = Path.GetFileNameWithoutExtension(Host.TemplateFile);
        string path = Path.GetDirectoryName(Host.TemplateFile);
        string columnId = tableName + "ID";
        string columnName = "Name";
        string connectionString = "data source=.;initial catalog=DBName;integrated security=SSPI";
    #>
    using System;
    using System.CodeDom.Compiler;
    
    namespace Services.<#= GetSubNamespace() #>
    {
        /// 
        /// <#= tableName #> auto generated enumeration
        /// 
        [GeneratedCode("TextTemplatingFileGenerator", "10")]
        public enum <#= tableName #>
        {
    <#
        SqlConnection conn = new SqlConnection(connectionString);
        string command = string.Format("select {0}, {1} from {2} order by {0}", columnId, columnName, tableName);
        SqlCommand comm = new SqlCommand(command, conn);
    
        conn.Open();
    
        SqlDataReader reader = comm.ExecuteReader();
        bool loop = reader.Read();
    
        while(loop)
        {
    #>      /// 
            /// <#= reader[columnName] #> configuration setting.
            /// 
            <#= Pascalize(reader[columnName]) #> = <#= reader[columnId] #><# loop = reader.Read(); #><#= loop ? ",\r\n" : string.Empty #>
    <#
        }
    #>  }
    }
    <#+
        private string Pascalize(object value)
        {
            Regex rx = new Regex(@"(?:[^a-zA-Z0-9]*)(?[a-zA-Z0-9])(?[a-zA-Z0-9]*)(?:[^a-zA-Z0-9]*)");
            return rx.Replace(value.ToString(), m => m.Groups["first"].ToString().ToUpper() + m.Groups["reminder"].ToString().ToLower());
        }
    
        private string GetSubNamespace()
        {
            Regex rx = new Regex(@"(?:.+Services\s)");
            string path = Path.GetDirectoryName(Host.TemplateFile);
            return rx.Replace(path, string.Empty).Replace("\\", ".");
        }
    #>
    

    Then whenever you'd like an enum to be generated, just create a tt file with the same name as database table like UserType.tt and put this code in:

    <#@ include file="..\..\T4 Templates\EnumGenerator.ttinclude" #>
    

    An even more advanced template is now available in my blog post.

提交回复
热议问题