2014-01-12 101 views
1

我已经达到了我的应用程序开发中令人困惑的部分。使用T4模板ENum的

我正在使用自定义属性进行身份验证,名为“Allowed For”,然后使用用户类型。

例如,通过方法将有数据注解[Allowfor(UserType.Admin)]但我遇到的问题是,我想在数据库中存储UserTypes其下述性能的列表:

  • 编号
  • 删除
  • 名称
  • 说明

所以在运行时,甚至在按钮的点击我想建立一个Enum类,它拥有未被删除的枚举的列表。

任何人都可以摆动一些建议我的方式关于这一点,因为我似乎无法做到这一点,我的自我,无法找到很多帮助谷歌搜索。

我只需要知道以下几点:

  • 如何使用T4模板或类似的东西在VS创建一个类2013
  • 如何得到它该在每次构建的启动/或者我在UI
  • 任何数据库查找最好使用web配置文件中的字符串或使用的DbContext按下一个按钮(EF如果可能的话)

回答

1

我已经做了T4模板非常相似的东西秒。技巧是知道需要哪些dll,找到它们等(例如,在Visual Studio 2013中,EnvDET被引用为“import namespace =”EnvDTE“而不是EvnDTE.dll)

基本上你需要和他们一起玩尝试并得到你想要的结果在你的解决方案中创建一个T4项目,然后开始填充它。

我使用命令对象(并且使用下面的代码使用MySQL库),但它应该给你一个想法怎么我跑了。

<#@ template debug="true" hostSpecific="true" #> 
<#@ output extension=".generated.cs" #> 
<#@ Assembly Name="EnvDTE" #> 
<#@ Assembly Name="System.Data" #> 
<#@ Assembly Name="$(SolutionDir)Services.Entities\bin\DevTest\Libraries.DB.dll" #> **// * custom location for custom libraries** 
<#@ Assembly Name="$(SolutionDir)Services.Entities\bin\DevTest\MySql.Data.dll" #> **// custom location for custom libraries** 
<#@ import namespace="EnvDTE" #> 
<#@ import namespace="System.Data" #> 
<#@ import namespace="System.Data.SqlClient" #> 
<#@ import namespace="System.IO" #> 
<#@ import namespace="System.Text.RegularExpressions" #> 

<# 
     string tableName = ""; 
     //string path = Path.GetDirectoryName(Host.TemplateFile); 
     string connectionString = "mysqlConnectionString"; **// replaced with regular value, could pull from web.config but it's only updated rarely** 

     // Get containing project 
     IServiceProvider serviceProvider = (IServiceProvider)Host; 
     DTE dte = (DTE)serviceProvider.GetService(typeof(DTE)); 
     //Project project = dte.Solution.FindProjectItem(Host.TemplateFile).ContainingProject; 
#> 
using System; 
using System.CodeDom.Compiler; 

namespace Services.Entities 
{ 
    [GeneratedCode("TextTemplatingFileGenerator", "10")] 
    public enum Store 
    { 
<# 

    using (var cmd = new Libraries.DB.Mysql.Command(connectionString)) **//Custom libraries to open a disposable command object** 
    { 
     cmd.Clear(); 
     cmd.AppendCmd("select id, storecode, StoreName \n"); 
     cmd.AppendCmd("from stores \n"); 

     var reader = cmd.ExecuteReader(); 
      while(reader.Read()) 
      { 
      int storeId = Convert.ToInt32(reader["id"]); 
      string storecode = reader["storecode"].ToString(); 
      string description = reader["StoreName"].ToString(); 
#> //We now have the data, let's use it! The code in <#= gets populated #>** 
    [Libraries.Utils.Enums.Info(Code = "<#= storecode #>", Name = "<#= description #>")] <#= Sanitize(description) #> = <#= storeId #>, 
<# 
      } 
    } 
#> 
    } 
} 
//This is a method which takes a name like "Some Descripton" and Sanitizes it to Some_Description so it can be usable as an enum** 
<#+ 
     static string Sanitize(string token) 
     { 
       // Replace all invalid chars by underscores 
       token = Regex.Replace(token, @"[\W\b]", "_", RegexOptions.IgnoreCase); 

       // If it starts with a digit, prefix it with an underscore 
       token = Regex.Replace(token, @"^\d", @"_$0"); 

       // Check for reserved words 
       // TODO: Clean this up and add other reserved words (keywords, etc) 
       if (token == "Url") token = "_Url"; 

       return token; 
     } 
#> 

需要注意这些文件的关键问题是,之间< ##家居>是代码即运行,并且您可以在该代码中创建变量。显示,很像ASP,<#=变量#>。

我不确定这些文件是否在您每次构建解决方案时自动构建,但似乎这将是一个简单的构建脚本。要编辑它们,请在保存时进行编辑。您可以右键单击该文件并从上下文菜单中单击“运行自定义工具”。您将通过文件和相关的生成文件来查看错误,以检查它。

顺便说一句,上面可能会产生这样的事情:

using System; 
using System.CodeDom.Compiler; 

namespace Services.Entities 
{ 
    [GeneratedCode("TextTemplatingFileGenerator", "10")] 
    public enum Store 
    { 
    [Libraries.Utils.Enums.Info(Code = "ST1", Name = "Store 1")] Store1 = 1, 
    [Libraries.Utils.Enums.Info(Code = "ST2", Name = "Store 2")] Store2 = 2, 
    } 
} 

顺便说一句,这是一个标签(<#,<#=,<#+)之外的一切都将被发送作为班级的输出。您需要确保您的模板在运行时正确设置,否则您可能因缺少括号而导致编译错误等。