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秘密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/597423

相关文章

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

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基