[转]T4 Code Generation
[原文:https://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx]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.
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.
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.
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.
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. ;)
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.
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.
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.
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:
- 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.
- How to create a simple T4 template
- How to use T4 to generate .config files
- How to use T4 to generate Decorator classes
- How to use T4 to generate CRUD stored procedures
- How to use T4 to generate strongly-typed navigation class in ASP.NET (by Kirill Chilingarashvili)
- How to use T4 to generate strongly-typed AzMan wrapper
- How to generate multiple outputs from single T4 template
- T4 template for generating ADO.NET Entity Framework Stored Procedures (by David DeWinter)
- T4 script for generating ADO.NET Entity Framework Views (by ADO.NET team)
- T4 template for generating LINQ to SQL Data Context (by Damien Guard)
- T4 template for generating WiX source files (by New Age Solutions)
- T4 template for generating SQL view from C# enumeration
- MSBuild task for transforming T4 templates (by Elton Stoneman)
- T4 template for generating state machines in C# (by 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.
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.
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.
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.
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.
Plus, it all works in Visual Studio without installing anything.
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!
微信公众账号:大内老A
微博:www.weibo.com/artech
如果你想及时得到个人撰写文章以及着作的消息推送,或者想看看个人推荐的技术资料,可以扫描左边二维码(或者长按识别二维码)关注个人公众号(原来公众帐号蒋金楠的自媒体将会停用)。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
最后更新:2017-10-27 14:04:45
上一篇:
采用一个自创的"验证框架"实现对数据实体的验证[扩展篇]
下一篇:
关于CLR内存管理一些深层次的讨论[上篇]
我们为什么需要SDN?
如何在 CentOS 7 中在 HHVM 和 Nginx 之上安装 WordPress
线性表之顺序存储结构(C语言动态数组实现)
使用LayoutAnimationController为RecyclerView添加动画
【避坑】初次接项目的血与泪,扎坑了老铁(二)
笔记:The Seven Steps to Building a Successful Software Development Company
python-代码库 :requests 模拟登陆网站
Skype-Type:一款通过声音窃取键盘记录的Keylogger工具
WIKIOI-1146 ISBN号码
First Doc