c# - 是否有基于 T4 旁边的模板生成 Razor View (.cshtml) 的方法?

标签 c# asp.net-mvc-3 templates code-generation

我正在开发一些代码生成器,用于根据数据库表生成模型、 Controller 和 View ,这样我就可以制作一个 Asp.Net MVC3 网站。我现在可以使用 CodeDOM 生成模型和 Controller ,但是为了生成 View ,我需要一些东西来制作模板,比如从 .cs 生成 .cshtml,我认为 T4 是个好主意,但我的一些同事坚持不使用 T4,是还有其他办法吗?谢谢

最佳答案

我不确定为什么他们会反对使用 T4,因为包括 Entity Framework 在内的许多代码库都在使用它们。听起来你正在做我刚做完的事情。我喜欢使用预处理的 T4 模板,因此我能够将数据从 C# 代码输入到模板中并以这种方式生成文件。这允许您有多个文件输出和基本参数来传递数据。

我使用它的方式是.. 我制作了一个类,用于收集有关数据库的所有信息.. 要包含或排除哪些表.. 然后是关于每个列的信息,如 pk 或身份可为空等。我将经过预处理的 t4 模板插入,该信息能够生成所有 SQL、 View 、模型、 Controller 信息。每当数据库发生变化时。我只需运行我的控制台应用程序,然后全部重新生成。

预处理: http://odetocode.com/Blogs/scott/archive/2011/01/03/preprocessed-t4-templates.aspx

Entity Framework : 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();#>     
    }
    <# } #>
}

关于c# - 是否有基于 T4 旁边的模板生成 Razor View (.cshtml) 的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876865/

相关文章:

asp.net-mvc-3 - MVC3 : Pass ICollection from View to controller

c# - 新的 ASP.NET MVC3 项目不能在 MonoDevelop 中编译

c++ - 错误 C2440 : 'initializing' : cannot convert from 'const int' to 'int *'

c++ - 如何typedef模板函数指针?

c# - 从 jQuery 在 C# 背后的代码中调用函数

c# - 从 C# 中的 div 中删除类

c# - 当我想检查参数的正确值时的圈复杂度

c# - 使用 iTextSharp 和 C# 自动调整表格列宽

jquery - MVC3 Html.DisplayFor -- 是否可以让该控件生成 ID?

c++ - 什么时候需要 "typename"关键字?