T4(文本模板转换工具包)代码生成-最佳保存的Visual Studio秘密

本文主要是介绍T4(文本模板转换工具包)代码生成-最佳保存的Visual Studio秘密,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Rob beat me to it. Blogging about T4 (the Text Template Transformation Toolkit) had been on my list literally for a year. He and I were singing its praises last night. Hopefully I can add something small to the conversation.

罗伯击败我。 从字面上看,有关T4的博客(文本模板转换工具包)在我的名单上已经存在了一年。 我和他昨晚在赞美他。 希望我可以在对话中添加一些小东西。

What's the story? Well, T4 is a code generator built right into Visual Studio. To be clear, you HAVE THIS NOW on your system…go play. Now's the time to introduce code generation to your company. If you're doing something twice or more, manually, in your company, generate it.

来龙去脉是什么? 好吧, T4是Visual Studio中内置的代码生成器。 需要明确的是,您现在已经在系统上……开始游戏。 现在是时候向您的公司介绍代码生成了。 如果您要执行两次或多次操作,请在公司中手动生成。

However, it's not deep-deep built in, because there's no item templates in File | New Item and there's no intellisense or syntax highlighting.

但是,它不是内置的,因为File |中没有项目模板。 新项目,没有智能感知或语法突出显示。

You don't need this, but if you want really get the most out of T4, first, head over to Clarius Consulting and get their "T4 Editor Community Edition." That'll get you some basic coloring. They have a pay version that gets you more if you want.

您不需要这个,但是如果您想真正从T4中获得最大收益,请首先前往Clarius Consulting,获取其“ T4编辑器社区版” 。 这将为您提供一些基本的着色。 他们提供付费版本,可让您获得更多收益。

Now, go into Visual Studio and make a Console App (or any app) and add a Text File, but name it something with a .tt extension. You'll get a warning since it's a generator, that someone could generate evil. Click OK if you are cool with potential evil. ;)

现在,进入Visual Studio,制作一个控制台应用程序(或任何应用程序),并添加一个文本文件,但是将其命名为.tt扩展名。 您会收到警告,因为它是发电机,有人可能会制造邪恶。 如果您对潜在的邪恶感到很冷静,请单击“确定”。 ;)

Security Warning

Now, look in Solution Explorer at the .tt file. If you're using C#, you'll have a sub .cs file, or if you're using VB, a sub .vb file. That's the file that will hold the result of the generation. This is the same visual metaphor used to the express the template/generated file relationship with .designer files you've seen elsewhere in Visual Studio.

现在,在解决方案资源管理器中查看.tt文件。 如果使用的是C#,则将有一个.cs子文件;如果使用的是VB,则将有一个.vb子文件。 那就是保存生成结果的文件。 这与您在Visual Studio中其他地方看到的用于表示.designer文件的模板/生成文件关系的可视化隐喻相同。

If you look in the Properties for the .tt file, you'll see it's using a CustomTool listed as the "TextTemplatingFileGenerator." This custom tool extensibility point is how things like XSD and WSDL code generators hook in to generate their artifacts.

如果在.tt文件的“属性”中查找,您会看到它正在使用列为“ TextTemplatingFileGenerator”的CustomTool。 这个自定义工具的可扩展性点就是XSD和WSDL代码生成器之类的东西如何挂钩以生成其工件。

T4 can generate any kind of text file, not just code. Whatever you like. You can use it in your projects, as above, or you can call T4 from the command-line.

T4可以生成任何类型的文本文件,而不仅仅是代码。 随你喜欢。 您可以如上所述在项目中使用它,也可以从命令行调用T4 。

Aside from Kzu and the folks at Clarius, Rob notes that Oleg Sych has a great collection of T4 resources. He's got some great Tutorials:

除了Kzu和克拉里乌斯(Clarius)的亲朋好友之外,罗布(Rob)注意到奥列格塞奇(Oleg Sych)拥有大量的T4资源。 他有一些很棒的教程:

  • Creating your first code generator

    创建您的第一个代码生成器

  • Troubleshooting code generation errors

    解决代码生成错误

  • Debugging code generation files

    调试代码生成文件

  • Creating reusable code generation templates

    创建可重用的代码生成模板

  • Creating complex code generators

    创建复杂的代码生成器

  • Reusing code generators on multiple projects

    在多个项目上重用代码生成器

Here's some of the links from Oleg's blog.

这是Oleg博客的一些链接。

  • How to create a simple T4 template

    如何创建一个简单的T4模板

  • How to use T4 to generate .config files

    如何使用T4生成.config文件

  • How to use T4 to generate Decorator classes

    如何使用T4生成装饰器类

  • How to use T4 to generate CRUD stored procedures

    如何使用T4生成CRUD存储过程

  • How to use T4 to generate strongly-typed navigation class in ASP.NET (by Kirill Chilingarashvili)

    如何在ASP.NET中使用T4生成强类型导航类(作者Kirill Chilingarashvili)

  • How to use T4 to generate strongly-typed AzMan wrapper

    如何使用T4生成强类型的AzMan包装器

  • How to generate multiple outputs from single T4 template

    如何从单个T4模板生成多个输出

  • T4 template for generating ADO.NET Entity Framework Stored Procedures  (by David DeWinter)

    用于生成ADO.NET实体框架存储过程的T4模板(作者David DeWinter)

  • T4 script for generating ADO.NET Entity Framework Views (by ADO.NET team)

    用于生成ADO.NET实体框架视图的T4脚本(由ADO.NET团队提供)

  • T4 template for generating LINQ to SQL Data Context (by Damien Guard)

    用于生成LINQ to SQL数据上下文的T4模板(由Damien Guard撰写)

  • T4 template for generating WiX source files (by New Age Solutions)

    用于生成WiX源文件的T4模板(由New Age Solutions提供)

  • T4 template for generating SQL view from C# enumeration

    T4模板,用于从C#枚举生成SQL视图

  • MSBuild task for transforming T4 templates (by Elton Stoneman)

    用于转换T4模板的MSBuild任务(作者Elton Stoneman)

  • T4 template for generating state machines in C# (by Andrew Matthews)

    用于在C#中生成状态机的T4模板(作者Andrew Matthews)

Oleg also has a CodePlex project called T4 Toolbox that is a library of T4 templates that get added to File | New Item.

Oleg还有一个名为T4 Toolbox的CodePlex项目,该项目是T4模板的库,这些库已添加到File | 新物品。

Also, check out Damien Guard's T4 templates that are a wholesale replacement of code that LINQ to SQL generates. Here's an example, where I use Damien's T4 templates against the sample Chinook Database.

另外,请查看Damien Guard的T4模板,这些模板是LINQ to SQL生成的代码的批发替代品。 这是一个示例,其中我将Damien的T4模板用于示例Chinook数据库。

image

Notice that I've named the .tt file the same as the .dbml, so Damian's code can find it. I also continue to let original LINQ to SQL generate it's .designer.cs file, but make that file's Build Action "None" so it's not ever compiled. That effectively puts Damian's code in charge.

注意,我已经将.tt文件命名为与.dbml相同,因此Damian的代码可以找到它。 我还继续让原始的LINQ to SQL生成它的.designer.cs文件,但使该文件的Build Action为“ None”,因此它永远不会被编译。 这有效地使达米安的代码负责。

Here's a screenshot showing a bit of Damian's T4 template using the syntax highlighting from the Clairus T4 Visual Studio free download. If I'd pay them, I'd get intellisense and syntax highlighting inside the code blocks also. It looks like a lot like ASP.NET Web Forms, or Velocity, or any templating language really. The code blocks are where your logic is and outside the codeblocks is the template for whatever you want to generate. Notice how Damien gets input and sets output. You have full control, you can read files off the file system, from with your project, etc. He sets the output extension also. I like to use .g.cs or .g.vb, myself. In this example his generated file is Chinook.generated.cs.

这是一个截图,显示了一些Damian的T4模板,该模板使用了从Clairus T4 Visual Studio免费下载的语法突出显示。 如果我付钱给他们,我也会在代码块获得智能感知和语法高亮显示。 它看起来很像ASP.NET Web窗体,Velocity或任何模板语言。 代码块是您的逻辑所在,代码块之外是您想要生成的任何内容的模板。 注意Damien如何获取输入并设置输出。 您拥有完全的控制权,可以从文件系统中读取文件,也可以从项目中读取文件,等等。他还设置了输出扩展名。 我自己喜欢使用.g.cs或.g.vb。 在此示例中,他生成的文件是Chinook.genic.cs。

I particularly like Damien's example because he's swapping out parts of LINQ to SQL that he didn't like (the generated code) while keeping the part he did (the general mode, the designer, the dbml file.) If you don't like something, fix it.

我特别喜欢Damien的示例,因为他将LINQ的某些部分(通用模式,设计器和dbml文件)替换为他不喜欢的LINQ(生成的代码),同时保留了他的部分(常规模式,设计器和dbml文件。)。修复它。

Plus, it all works in Visual Studio without installing anything.

另外,它们都可以在Visual Studio中运行,而无需安装任何程序

image

If you're doing Code Gen, or thinking about it, check out T4 as it's a great place to start. Also, search my blog for "Code Generation" as I was livign and breathing it with CodeSmith for the many years I worked at Corillian. Have fun!

如果您正在执行Code Gen或正在考虑它,请查看T4,因为它是一个不错的起点。 另外,在我在Corillian工作的许多年里,我经常浏览并在CodeSmith上呼吸它,以搜索“ Code Generation”。 玩得开心!

翻译自: https://www.hanselman.com/blog/t4-text-template-transformation-toolkit-code-generation-best-kept-visual-studio-secret

这篇关于T4(文本模板转换工具包)代码生成-最佳保存的Visual Studio秘密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/cunfusq0176/article/details/109083242
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/597423

相关文章

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

Olingo分析和实践之EDM 辅助序列化器详解(最佳实践)

《Olingo分析和实践之EDM辅助序列化器详解(最佳实践)》EDM辅助序列化器是ApacheOlingoOData框架中无需完整EDM模型的智能序列化工具,通过运行时类型推断实现灵活数据转换,适用... 目录概念与定义什么是 EDM 辅助序列化器?核心概念设计目标核心特点1. EDM 信息可选2. 智能类

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经

Zabbix在MySQL性能监控方面的运用及最佳实践记录

《Zabbix在MySQL性能监控方面的运用及最佳实践记录》Zabbix通过自定义脚本和内置模板监控MySQL核心指标(连接、查询、资源、复制),支持自动发现多实例及告警通知,结合可视化仪表盘,可有效... 目录一、核心监控指标及配置1. 关键监控指标示例2. 配置方法二、自动发现与多实例管理1. 实践步骤

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

MySQL 迁移至 Doris 最佳实践方案(最新整理)

《MySQL迁移至Doris最佳实践方案(最新整理)》本文将深入剖析三种经过实践验证的MySQL迁移至Doris的最佳方案,涵盖全量迁移、增量同步、混合迁移以及基于CDC(ChangeData... 目录一、China编程JDBC Catalog 联邦查询方案(适合跨库实时查询)1. 方案概述2. 环境要求3.

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2