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.
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.
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.
By default, CodeSmith Generator 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 Generator in conjunction with custom code. Here are three strategies to enable active code generation and custom code together with CodeSmith Generator: