修改ASP.NET MVC中的默认代码生成/脚手架模板

image

One of the things people like the most about my ASP.NE MVC 2 Ninja Black Belt Tips video, besides the Bill Cosby sweater, is the tip where I show how to modify the default CodeTemplates that are used in Code Generation in ASP.NET MVC (either version).

人们最喜欢我的ASP.NE MVC 2 Ninja Black Belt Tips视频的事情之一,是Bill Bill Cosby毛衣,它是向我展示如何修改ASP.NET MVC中代码生成中使用的默认CodeTemplates的技巧(任一版本)。

Eilon mentioned it on an internal mailing list this week so I thought it'd be worth surfacing again in case you haven't heard of this, Dear Reader.

Eilon本周在内部邮件列表中提到了它,因此,亲爱的读者,如果您还没有听说过,我认为值得再次提出。

Soon, we'll see even more powerful, flexible, and fun ways to customize your own Scaffolding in ASP.NET MVC 3, so keep an eye out, 'cause it's coming.

很快,我们将看到更强大,灵活且有趣的方法来在ASP.NET MVC 3中自定义自己的脚手架,因此请注意,因为它来了。

将CodeTemplates带到您的项目本地 (Bring the CodeTemplates local to your project)

When you use the Visual Studio "tooling" (that means dialogs and stuff) to Add View or Add Controller, you're actually executing a T4 template and generating a little bit of code. Where does this start and how can you change it?

当您使用Visual Studio“工具”(即对话框和内容)来添加视图或添加控制器时,实际上是在执行T4模板并生成一点代码。 它从哪里开始,如何更改?

Try this. Create a new ASP.NET MVC project.

尝试这个。 创建一个新的ASP.NET MVC项目。

Now, go to:

现在,转到:

C:\Program Files (or x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp (or Visual Basic)\Web\MVC (or 2) 3\CodeTemplates

C:\ Program Files(或x86 )\ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ ItemTemplates \ CSharp(或Visual Basic )\ Web \ MVC(或2 )3 \ CodeTemplates

See all those folders? Turns out everything under CodeTemplates are T4 templates that can be either modified in place or even better, can be brought local to your project. Local CodeTemplates will override the global ones.

查看所有这些文件夹? 下CodeTemplates原来一切都是T4模板,可以就地或者修改或者甚至更好,可以把您的本地项目。 本地代码模板将覆盖全局代码模板。

Copy that folder (or just subfolders like AddController, or AddView) to your ASP.NET project. I do this by dragging from Explorer directly into the Visual Studio Solution Explorer as seen  here:

将该文件夹(或只是子文件夹,如AddController或AddView)复制到您的ASP.NET项目中。 我是通过直接从资源管理器拖动到Visual Studio解决方案资源管理器中来实现的,如下所示:

You might get some errors when you drop the folder. Ignore them and instead, select the Templates using Ctrl-Click, then right click and select Properties. (You can delete any templates you won't be using.)

删除文件夹时,您可能会遇到一些错误。 忽略它们,而是使用Ctrl-Click选择模板,然后右键单击并选择“属性”。 (您可以删除不需要的任何模板。)

image

Now, see where it says Custom Tool? Clear that string out completely. You're tell Visual Studio that you don't want these T4 Templates to be run during a build. They will only be called manually by the Tooling dialogs like Add View and Add Controller.

现在,在哪里显示“自定义工具”? 彻底清除该字符串。 您告诉Visual Studio,您不希望在构建过程中运行这些T4模板。 它们只能通过“工具”对话框(如“添加视图”和“添加控制器”)手动调用。

You change these templates, or in the case of Add View you can make your own. For example here I've made a new "My Awesome List.tt" as a copy of List.tt and it shows up in the Add View dialog.

您可以更改这些模板,或者在添加视图的情况下,可以自己制作。 例如,在这里,我创建了一个新的“ My Awesome List.tt”作为List.tt的副本,它显示在“添加视图”对话框中。

Now I can not only totally customize what gets generated from the Add Controller and Add View dialogs, I can add custom templates to the drop down. Most importantly, these changes live in the CodeTemplates folder of my project and are carried along with my project in Source Control so others on my team or company can use them as well.

现在,我不仅可以完全自定义从“添加控制器”和“添加视图”对话框中生成的内容,还可以向下拉列表中添加自定义模板。 最重要的是,这些更改位于我项目的CodeTemplates文件夹中,并且随我的项目一起在Source Control中进行,因此我的团队或公司中的其他人也可以使用它们。

Have you customized yours? Enjoy.

你有定制你的吗? 请享用。

相关链接 (Related Links)

翻译自: https://www.hanselman.com/blog/modifying-the-default-code-generationscaffolding-templates-in-aspnet-mvc

对象关系映射架构(DBFramework)及代码生成器源码 一、使用对象关系映射组件Kenly.DBFramework.dll不用编写任何SQL或者存储过程即可实现下列功能: 1、数据表、视图和存储过程与对象之间的转换。 2、数据表、视图的自定义条件查询。 3、数据表、视图的分页查询。 4、根据ID、主键或自定义条件对数据表进行增、删、改操作。 5、实现一对一、一对多、多对一和多对多的关系映射。 6、支持单个对象和多个对象之间的事务控制。 7、支持查询结果排序。 8、支持查询表达式生成。 9、支持延迟加载。 二、代码生成器 1、根据指定的数据库连接,自动生成数据表、视图和存储过程对应的对象代码(C#代码)。 2、自动生成相关的工程文件,生成完整的业务层项目文件。 3、可以帮助生成自定义查询方法。 4、支持SQLServer2000、SQLServer2005和Oracle代码生成插件,支持插件扩展。 提供 1、对象关系映射组件: Kenly.DBFramework.dll 2、代码生成器源码:CodeHelper。 3、代码生成器插件源码(支持SQLServer2000、SQLServer2005和Oracle):Plugin。 4、使用手册:DBFramework.Manual V4.5.3.pdf。 主要API: public abstract class ViewGateway where T: new() { // Methods static ViewGateway(); protected ViewGateway(); protected static int Count(); protected static int Count(string condition); protected static List CountGroup(string groupBy); protected static List CountGroup(string condition, string groupBy); public static List CreateInstances(int count); protected static List Distinct(string columnName); protected static List Distinct(string columnName, string condition); protected static List Distinct(string columnName, string condition, bool ignoreNull); protected static bool Exists(string condition); public bool ExistsById(); public bool ExistsByPK(); protected static List Find(PagingArg pagingArg, params string[] propertyNames); protected static List Find(string condition, params string[] propertyNames); protected static List Find(PagingArg pagingArg, bool sqlServer2000, params string[] propertyNames); protected static List Find(string condition, PagingArg pagingArg, params string[] propertyNames); protected static List Find(string condition, PagingArg pagingArg, bool sqlServer2000, params string[] propertyNames); protected static List FindAll(params string[] propertyNames); protected static void FindTop(T firstOne); protected static List FindTop(int topCount, params string[] propertyNames); protected static void FindTop(string condition, T firstOne); protected static List FindTop(string condition, int topCount, params string[] propertyNames); protected static void InitializeGateway(GatewayConfig config); protected static void InitializeGateway(DatabaseType dbType, string connectionString); protected void InitializeInstance(T entity); protected static object Max(string columnName); protected static object Max(string columnName, string condition); protected static List MaxGroup(string columnName, string groupBy); protected static List MaxGroup(string columnName, string condition, string groupBy); protected static object Min(string columnName); protected static object Min(string columnName, string condition); protected static List MinGroup(string columnName, string groupBy); protected static List MinGroup(string columnName, string condition, string groupBy); protected static DataTable Query(PagingArg pagingArg, params string[] propertyNames); protected static DataTable Query(string condition, params string[] propertyNames); protected static DataTable Query(PagingArg pagingArg, bool sqlServer2000, params string[] propertyNames); protected static DataTable Query(string condition, PagingArg pagingArg, params string[] propertyNames); protected static DataTable Query(string condition, PagingArg pagingArg, bool sqlServer2000, params string[] propertyNames); protected static DataTable QueryAll(params string[] propertyNames); protected static DataTable QueryTop(int topCount, params string[] propertyNames); protected static DataTable QueryTop(string condition, int topCount, params string[] propertyNames); public ArrayList RetrieveAssociations(); public ArrayList RetrieveAssociations(Type elementType); public ArrayList RetrieveAssociations(params Type[] elementTypes); public void RetrieveById(); public void RetrieveByPK(); protected internal virtual void RetrieveBySql(string sql); public void ShadowCopyTo(object targetEntity); public void ShadowCopyTo(T targetEntity); protected static double Sum(string columnName); protected static double Sum(string columnName, string condition); protected static List SumGroup(string columnName, string groupBy); protected static List SumGroup(string columnName, string condition, string groupBy); public static string ToColumnName(string propertyName); public static T ToObject(DataRow adaptedRow); public static List ToObjects(DataTable adaptedTable); public static List ToObjects(DataRow[] adaptedRows); public static string ToPropertyName(string columnName); public static DataTable ToTable(IList entities); public static DataTable ToTable(params T[] entities); public static DataTable ToTable(bool isAdapted, params T[] entities); public static DataTable ToTable(bool isAdapted, IList entities); // Properties protected internal static string CommandText { get; } protected internal static GatewayConfig Config { get; } internal T Entity { get; set; } public object EntityId { get; } public object PrimaryKey { get; } protected static Order SortedOrder { get; set; } } public abstract class TableGateway : ViewGateway where T: TableGateway, new() { // Methods static TableGateway(); protected TableGateway(); public int AddNew(); public static int AddNew(IList entities); public static int AddNew(T entity); public int AddNew(bool returnIdentity); public static int AddNew(T entity, bool returnIdentity); public static int AddNew(IList entities, out Transaction transaction); public static int AddNew(IList entities, bool returnIdentity); public static int AddNew(T entity, out Transaction transaction); public static int AddNew(IList entities, bool returnIdentity, out Transaction transaction); public static int AddNew(T entity, bool returnIdentity, out Transaction transaction); public IDbTransaction BeginTransaction(); public IDbTransaction BeginTransaction(IsolationLevel isolationLevel); public IDbTransaction BeginTransaction(double activeTime); public IDbTransaction BeginTransaction(IsolationLevel isolationLevel, double activeTime); public bool Commit(); protected static int Delete(string condition); protected static int Delete(string condition, out Transaction transaction); public int DeleteById(); public static int DeleteById(T entity); public static int DeleteById(IList entities); public static int DeleteById(IList entities, out Transaction transaction); public static int DeleteById(T entity, out Transaction transaction); public int DeleteByPK(); public static int DeleteByPK(T entity); public static int DeleteByPK(IList entities); public static int DeleteByPK(IList entities, out Transaction transaction); public static int DeleteByPK(T entity, out Transaction transaction); protected void OnPropertyChanged(MethodBase propertyMethod); protected void OnPropertyChanged(string propertyName); protected internal override void RetrieveBySql(string sql); public bool Rollback(); protected static int Update(T entity, string condition); protected static int Update(T entity, string condition, out Transaction transaction); public int UpdateById(); public static int UpdateById(IList entities); public static int UpdateById(T entity); public static int UpdateById(T entity, out Transaction transaction); public static int UpdateById(IList entities, out Transaction transaction); public int UpdateByPK(); public static int UpdateByPK(IList entities); public static int UpdateByPK(T entity); public static int UpdateByPK(IList entities, out Transaction transaction); public static int UpdateByPK(T entity, out Transaction transaction); // Properties public bool Changed { get; } public List ChangedPropertyNames { get; } } public class StoredProcedure : IStoredProcedure { public bool BeginTransaction(); public void CloseReader(IDataReader reader); public bool Commit(); public DataSet ExecuteDataSet(); public DataTable ExecuteDataTable(); public DataTable ExecuteDataTable(); public List ExecuteEntity(); public int ExecuteNonQuery(); public IDataReader ExecuteReader(); public object ExecuteScalar(); protected static void InitializeGateway(GatewayConfig config); protected static void InitializeGateway(DatabaseType dbType, string connectionString); protected void InitializeInstance(T entity); public bool Rollback(); } public class AggregateEntity where T: AggregateEntity, new() { public static List Execute(); protected static List Execute(string condition); public static DataTable ExecuteDataTable(); protected static DataTable ExecuteDataTable(string condition); protected static void InitializeGateway(GatewayConfig config); protected static void InitializeGateway(DatabaseType dbType, string connectionString); // Properties protected static Order SortedOrder { get; set; } } public static class EntityMapper { // Methods public static void AdaptToDatabase(DataTable adaptedTable); public static void AdaptToEntity(DataTable rawTable); public static void CopyToEntities(IList entities, IList adaptedRows); public static void CopyToEntities(IList entities, DataTable adaptedTable); public static void CopyToEntity(T entity, DataRow row); public static List CreateEntities(int count); public static DataTable CreateTable(); public static DataTable CreateTable(bool isAdapted); public static string ToColumnName(string propertyName); public static List ToEntities(IList adaptedRows); public static List ToEntities(DataTable table); public static T ToEntity(DataRow adaptedRow); public static string ToPropertyName(string columnName); public static DataTable ToTable(IList entities); public static DataTable ToTable(params T[] entities); public static DataTable ToTable(bool isAdapted, params T[] entities); public static DataTable ToTable(bool isAdapted, IList entities); } public static class EntityUtility { // Methods public static List Inherit(IList entities); public static T Inherit(object entity); public static T Inherit(Tbase baseEntity, TransformAction method); public static List Inherit(IList baseEntities, TransformAction method); public static void ShadowCopy(IList targetEntities, IList sourceEntities); public static void ShadowCopy(object targetEntity, object sourceEntity); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值