如何使T4文本模板数据库驱动

how to make T4 text template database driven

我正在试验代码生成 atm,我编写了一个小模板来完成我需要它做的事情,但我需要自己对属性进行硬编码,我想提取 class 和 属性 来自数据库的名称,其中 tables 是 classes,索引字段是属性。想法是根据 table 索引编写过滤器 class。我现在拥有的代码假定我有 1 个 table 和 3 个字段用作过滤条件。 目前没有任何代码来生成编译版本,因为 VS 在内部执行此操作。 这是我到目前为止所拥有的: </p> <pre><code><#@ template debug="false" hostspecific="false" language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ parameter name="namespacename" type="System.String" #> <#@ parameter name="filterobjectname" type="System.String" #> <#@ parameter name="property1" type="System.String" #> <#@ parameter name="property2" type="System.String" #> <#@ parameter name="property3" type="System.String" #> <#@ output extension=".cs" #> using AzzWork.Business; using Ventura.Business.Entities; <# string _namespacename; if(this.namespacename==null){_namespacename = "Ventura.Business";}else{_namespacename = this.namespacename;} #> <# string _classname; if(this.filterobjectname==null){_classname = "BusinessObject";}else{_classname = this.filterobjectname;}#> <# string _property1; if(this.property1==null){_property1 = "LastName";}else{_property1 = property1;}#> <# string _property1c = _property1.Substring(0, 1).ToLower() + _property1.Substring(1);#> <# string _property2; if(this.property2==null){_property2 = "Email";}else{_property2 = property2;}#> <# string _property2c = _property2.Substring(0, 1).ToLower() + _property2.Substring(1);#> <# string _property3; if(this.property3==null){_property3 = "FirstName";}else{_property3 = property3;}#> <# string _property3c = _property3.Substring(0, 1).ToLower() + _property3.Substring(1);#> <# string seperator1; if((_property1!=null && _property2!=null) || (_property1!=null && _property2==null && _property3!=null)){seperator1=", ";}else{seperator1="";}#> <# string seperator2; if(_property2!=null && _property3!=null){seperator2=", ";}else{seperator2="";} #> namespace <#= _namespacename #> { public static class <#= _classname #>FilterFactory{ internal static AzzFilterCollection<<#= _classname #>> GetFilterFor<#= _classname #>(<#if(_property1!=null){#>string <#=_property1c #>Part<#}#><#=seperator1#><#if(_property2!=null){#>string <#=_property2c #>Part<#}#><#=seperator2#><#if(_property3!=null){#>string <#=_property3c #>Part<#}#>) => new AzzFilterCollection<<#=_classname#>>() <#if(_property1!=null){ #> .AddFilterFor<#= _property1 #>(<#=_property1c #>Part) <#}#> <#if(_property2!=null){ #> .AddFilterFor<#= _property2 #>(<#=_property2c #>Part) <#}#> <#if(_property3!=null){ #> .AddFilterFor<#= _property3 #>(<#=_property3c #>Part)<#}#>; <#if (_property1!=null){#> internal static AzzFilterCollection<<#=_classname #>> AddFilterFor<#= _property1 #>(this AzzFilterCollection<<#= _classname#>> filterCollection, string <#= _property1c #>Part) { if (<#= _property1c #>Part == null) return filterCollection; var filter = new AzzFilter<<#=_classname#>>(<#=_classname#>.ColumnNames.<#=_property1#>, <#=_property1c#>Part); return filterCollection.AddFilter(filter); } <#}#> <#if (_property2!=null){#>internal static AzzFilterCollection<<#=_classname #>> AddFilterFor<#= _property2 #>(this AzzFilterCollection<<#= _classname#>> filterCollection, string <#= _property2c #>Part) { if (<#= _property2c #>Part == null) return filterCollection; var filter = new AzzFilter<<#=_classname#>>(<#=_classname#>.ColumnNames.<#=_property2#>, <#=_property2c#>Part); return filterCollection.AddFilter(filter); } <#}#> <#if (_property3!=null){#> internal static AzzFilterCollection<<#=_classname #>> AddFilterFor<#= _property3 #>(this AzzFilterCollection<<#= _classname#>> filterCollection, string <#= _property3c #>Part) { if (<#= _property3c #>Part == null) return filterCollection; var filter = new AzzFilter<<#=_classname#>>(<#=_classname#>.ColumnNames.<#=_property3#>, <#=_property3c#>Part); return filterCollection.AddFilter(filter); } <#}#> } }

我猜你正在寻找 SQL 的东西。

这对你有帮助吗?

This, This (same),

还有this