代码生成:被动生成与主动生成


在CodeSmith的帮助文档 Advanced Topics > Active vs. Passive Generation > Active vs. Passive Generation 有这么一段话:

Broadly speaking, there are two different types of code generators: passive code generators and active code generators.

一般来说,代码生成有两种,被动代码生成和主动代码生成

 

Passive code generators generate code once and then give up all responsibility for it. The wizards and builders that you find in modern IDEs are typically passive code generators. They're good for coming up with code that the developer later customizes, but once the code has been generated, a passive code generator can't regenerate it with changes.
被动代码生成一旦把代码生成出来,就不管了。现代IDE里的向导和创建器就是典型的被动代码生成。这些代码生成出来之后让开发人员很方便地去修改。但是代码一旦生成,被动代码生成就就无法在保留修改代码的基础上再次进行生成。

In contrast, active code generators are designed to maintain a link with the code that is generated over the long term by allowing the generator to be run multiple times over the same code. The key point to keep in mind about active code generators is that the template is the source code.Suppose you're generating 500 class files from a single template. With an active code generator, if you find a bug in the architecture of those classes (say, you've made a mistake in the way that you're handling object persistence),

it's not a huge problem. You just fix the one template and regenerate the 500 classes. This obviously saves you an incredible amount of time over fixing the same bug over and over again in 500 separate class files. 

相反,主动代码生成可以保持与生成代码的关联,允许对同一份代码进行长期多次的再生成。记住,主动代码生成的关键在于其模板就是源代码。假定你要从某个模板中生成500个类文件。使用主动代码生成,如果你在那些代码架构中发现了一个bug(比如说,你在处理对象持久化的时候搞错了。。),不是什么大问题,fix掉这个模板重新生成500个类就可以了。这明显节约你很多很多时间,不用去一次次去改那500个独立的文件。


 

But what happens when a template can't generate everything that needs to appear in the source code file? Suppose some of those 500 classes need custom methods, and the custom methods are different in different classes.For an active code generator to be effective, it must provide some way for a developer to customize its output, and then allow code regeneration without destroying those customizations. 如果模板不能够生成源代码文件里所有的东西,怎么办?假设500个类中有些需要有自定义方法,这些自定义的方法在不同的类里又各自不同。对于牛逼的主动代码生成,他肯定会提供一种方法给开发人员去定制它输出的代码,并且保证重新生成代码也不会破坏原来的定制。

 

By default, CodeSmith doesn't allow for custom code in the files that it generates. When you execute a template, it overwrites any existing output file completely. But there are ways to use CodeSmith in conjunction with custom code. Here are three strategies to enable active code generation and custom code together with CodeSmith:

默认情况下,CodeSmith不允许在他生成的文件里面修改代码。当你运行一个模板,它会完全覆盖现有的输出文件。但是用CodeSmith可以有好几种办法保留定制的代码。这里有三种策略来实现主动代码生成

 

Use inheritance

使用继承

Use merge strategies

使用合并策略

Use .NET 2.0 partial classes

使用 .net 2.0 部分类


-------------------

 

大概翻译了一下,这段话让我对代码生成器的评价有了个标准。(动软的代码生成器因此被抛弃)

CodeSmith提出的被动生成和主动生成的概念之后,很明显,我们要用代码生成器的话,就是要选择一款能够做到主动生成的,而不是生成完了什么都不管、要再生成一次怕后来做的修改都不见的代码生成器。

最后,本人觉得用.net 2.0部分类的方式还是比较推荐的。用在实战上了,很爽很放心~~

 


翻译的不好,比较模糊。以上觉得还不是很清楚的xdjm,建议直接去看CodeSmith的帮助文档。没用过CodeSmith的xdjm建议尝试下:)

posted @ 2010-01-24 17:41  allentranks  阅读(581)  评论(1编辑  收藏