Code Generation: passive generation and Active Generation

Source: Internet
Author: User
In the help document of codesmith, advanced topics> active vs. Passive generation> active vs. Passive generation has the following passage:

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

Generally,CodeThere are two types of generation: passive code generation and active code generation.


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 mizmizes, but once the code has been generated, a passive code generator can't regenerate it with changes.
Once the passive code is generated, no matter. Modern IDE's wizard and creator are typical passive code generation. After the code is generated, developers can easily modify it. However, once the code is generated, the passive code generation will not be able to be generated again on the basis of retaining the modified Code.

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 obviusly saves you an incredible amount of time over fixing the same bug over and over again in 500 separate class files.

On the contrary, active code generation can maintain the association with the generated code and allow long-term re-generation of the same Code. Remember, the key to active code generation is that its template isSource code. Suppose you want to generate 500 class files from a template. Use active code generation. If you find a bug in the Code architecture (for example, you are wrong when processing object persistence ..), It's not a big problem. You can fix this template and regenerate the 500 classes. This significantly saves you a lot of time and does not need to change the 500 independent files again and again.


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 either tive, it must provide some way for a developer to customize its output, and then allow code regeneration without destroying those mizmizations. what if the template cannot generate everything in the source code file? Assume that some of the 500 classes need custom methods. These custom methods are different in different classes. He will certainly provide a method for developers to customize the code output, and ensure that re-generation of the Code will not undermine the original customization.


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:

By default, codesmith cannot modify the code in the file generated by him. When you run a template, it completely overwrites the existing output file. However, codesmith can be used to retain custom code in several ways. There are three policies for active code generation.


Use inheritance

Use inheritance

Use merge strategies

Merge Policy

Use. NET 2.0 partial classes

Use. NET 2.0 categories



After a rough translation, this section sets a standard for my comments on code generators. (The soft code generator is discarded)

After the concept of passive generation and active generation proposed by codesmith, it is obvious that if we use a code generator, we need to choose one that can be generated proactively, instead of generating a code generator that does not care about what is generated, and generates another code generator that is afraid of subsequent modifications.

Finally, I think it is recommended to use. NET 2.0 categories. It's very easy to use in practice ~~


Poor translation, fuzzy. The preceding figure shows xdjm which is not very clear. We recommend that you go to the help document of codesmith. I have never used codesmith's xdjm. It is recommended to try again :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.