我不确定他们为什么反对使用 T4,因为包括实体框架在内的许多代码库都使用它们。听起来你在做我刚做完的事情。我喜欢使用 Pre-Processed T4 模板,因此我能够将数据从 C# 代码输入到模板中,并以这种方式生成文件。这允许您有多个文件输出和基本参数来传递数据。
我使用它的方式是..我创建了一个类,用于收集有关数据库的所有信息..要包含或排除哪些表..然后是每个列的信息,如 pk 或 identity 可空等。我将预处理的 t4 模板插入到该信息中,该信息能够生成所有 SQL、视图、模型、控制器信息......每当数据库发生变化时......我只需运行我的控制台应用程序并重新生成它。
预处理:
http://odetocode.com/Blogs/scott/archive/2011/01/03/preprocessed-t4-templates.aspx
实体框架:
http://msdn.microsoft.com/en-us/data/gg558520.aspx
MVCS脚手架:
http://blog.stevensanderson.com/2011/04/06/mvcscaffolding-overriding-the-t4-templates/
T4MVC:
http://mvccontrib.codeplex.com/wikipage?title=T4MVC
我再次知道这无助于回答您的问题,但 T4 非常棒,我很想听听关于为什么不使用 T4 的争论。它甚至是内置的!
顺便说一句,这里有一些智能感知!:http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html
如果您有任何问题,请随时联系我,我喜欢 T4,我愿意回答我能回答的任何问题。
这是我用来生成我的 POCO 模型的模板示例。由于使用我的普通 c# 方法传入数据的预处理能力,已经提取了很多内容。这个模板为我创建了 55 个基于模型的模型关闭数据库中的表。
我的“SchemeCollector”使用我创建的 DatabaseInfo、TableInfo 和 ColumnInfo 类来保存我需要的所有架构。然后我还有 9 个其他 t4 模板,它们也使用 SchemaCollector 类来填充数据。
这是我用来将数据传递到模板进行生成的扩展。我有这一切设置也可以使用 XML 文件进行配置,但我只是希望它真正可重用是不必要的。
public partial class PocoGenerator
{
public string Namespace { get; set; }
public string Inherit { get; set; }
public DatabaseInfo Schema { get; set; }
public bool Contract { get; set; }
public string SavePath { get; set; }
}
这是我用来调用和填充模板并保存它的方法。
static void GeneratePoco(Config config)
{
var template = config.Poco;
template.Schema = config.DatabaseInfo;
File.WriteAllText(template.SavePath, template.TransformText());
Console.WriteLine(" - POCOs generated for " + config.DatabaseInfo.DatabaseName + " complete");
}
这是模板
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Windows.Forms.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ assembly name="System.Xml.dll" #>
<#@ assembly name="System.Data.dll" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="CodeGenerator.Utilities" #>
using System;
using System.Runtime.Serialization;
namespace My.Models
{ <#
List<TableInfo> tables = Schema.Tables;
#>
//#########################################################################################################
// This file has been generated from the tables contained in the <#= Schema.DatabaseName #> database.
// Changes to this file will be overwritten when the file is regenerated. Generated: <#= DateTime.Now #>
//#########################################################################################################
<#
foreach (TableInfo table in tables)
{
#>
[DataContract]
public class <#= table.Name #> : IConfigTable
{
<#
PushIndent(" ");
foreach (ColumnInfo col in table.Columns)
{
if(col.IsKey || col.IsIdentity)
WriteLine("[Key]");
WriteLine("[DataMember]");
if(col.IsNullable && col.Type != "string")
WriteLine("public " + col.Type + "? " + col.Name+ " { get; set; }");
else
WriteLine("public " + col.Type + " " + col.Name+ " { get; set; }");
}PopIndent();#>
}
<# } #>
}