[转]T4 Code Generation

[原文:http://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:

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

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
如果你想及时得到个人撰写文章以及著作的消息推送,或者想看看个人推荐的技术资料,可以扫描左边二维码(或者长按识别二维码)关注个人公众号(原来公众帐号蒋金楠的自媒体将会停用)。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文链接

时间: 2024-08-30 11:41:55

[转]T4 Code Generation的相关文章

MaxCompute 中的Code Generation技术简介

前言       在<数据库系统中的Code Generation技术介绍>中,我们简单介绍了一下Code Generation技术及其在大规模OLAP系统,特别是大规模分布式OLAP系统中的重要性.MaxCompute采用了Code Generation技术来提高计算效率.在MaxCompute2.0中,我们又引入了基于LLVM的JIT(Just In Time) Code Generation技术.结合向量化的执行引擎,基于SIMD技术的执行效率优化等方式,较之MaxCompute 1.0

Clang AST parsing for automated code generation

原文地址:http://www.seethroughskin.com/blog/?p=2172 Syntax traversal is a powerful tool. With it you can automate repetitive tasks, search for semantic errors, generate wrappers, and so much more.  A few months ago I hit a hump (read: a f***ing mountain)

创建代码生成器可以很简单:如何通过T4模板生成代码?[上篇]

在<基于T4的代码生成方式>中,我对T4模板的组成结构.语法,以及T4引擎的工作原理进行了大体的介绍,并且编写了一个T4模板实现了如何将一个XML转变成C#代码.为了让由此需求的读者对T4有更深的了解,我们通过T4来做一些更加实际的事情--SQL Generator.在这里,我们可以通过SQL Generator为某个数据表自动生成进行插入.修改和删除的存储过程.[文中源代码从这里下载] 一.代码生成器的最终使用效果 我们首先来看看通过直接适用我们基于T4的SQL生成模板达到的效果.右图(点击

Entity Framework 5.0系列之自动生成Code First代码

在前面的文章中我们提到Entity Framework的"Code First"模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Framework Power Tools如何基于现有数据库生成数据类和数据库上下等. Entity Framework Power Tools 基于现有数据库生成POCO数据类和数据库上下文需要借助Visual Studio一个扩展插件-- Entity Framework Power Tools(一个Code First反向工

代码生成技术--CodeDom VS T4

     在微软的自家代码生成方案中我们有两种选择方式:CodeDom 和Text Template Transformation Toolkit(T4)模板.同样我们可以利用简单的String或者StringBuilder来拼接字符串,但是那对于简单的还可以,但是对于复杂的问题就.其实在ASP.NET MVC 3.0中有多处了一个更简洁语法的模板-Razor,我们同样可以运用于我们自己的代码生成中,我随便有一篇简单的介绍Razor Templating Engine,在以后有机会了会写Razo

ASP.NET MVC 3发布 Asp.net Web应用程序的一个框架

Asp.net Mvc 是http://www.aliyun.com/zixun/aggregation/35971.html">微软官方提供的Mvc模式编写Asp.net Web应用程序的一个框架,它由Castle的MonoRail而来.目前已经历经数个版本.MVC (ModelViewController)把一个web应用分成了三个部分:model view和controller.ASP.NET MVC框架提供了一个可以代替asp.net web窗体的基于mvc的应用. ASP.NET

db2-sqoop连接DB2import 时报错,Connection timed out 求大神解答

问题描述 sqoop连接DB2import 时报错,Connection timed out 求大神解答 sqoop连接DB2导入数据至HDFS时,报错,显示连接超时. 用list-table命令连接没有问题?,结果正确: 测试过DB2远程连接,没有问题,telnet 测试端口也没有问题: DB2版本v9.7,用的安装包里面的JDBC插件. 以下是错误信息. [biadmin@Hadoop01 sqoop]$ ./bin/sqoop import --connect jdbc:db2://9.1

RIA Service 的 SOAP EndPoint

原文 www.cyqdata.com/cnblogs/article-detail-39983-english 越来越多的朋友都在使用Silverlight开发应用程序,其中我们常用的还会有一个特殊的服务,就是RIA Service,具体来说,它是一个所谓的Domain Service. 关于这个服务的基本概念,如果有不清楚,请参考下面这个网址 http://www.silverlight.net/getstarted/riaservices/   今天这一篇是要谈谈一个问题,就是如何让RIA

C语言多线程编程

原文:C语言多线程编程 注:本文内容来源于互联网,感谢作者整理! Windows的多线程编程 c语言 在Windows的多线程编程中,创建线程的函数主要有CreateThread和_beginthread(及_beginthreadex). CreateThread 和 ExitThread    使用API函数CreateThread创建线程时,其中的线程函数原型:  DWORD WINAPI ThreadProc(LPVOID lpParameter);在线程函数返回后,其返回值用作调用Ex