I have been bitten by the code generation bug since last summer when I read the book Coder to Developer by Mike Gunderloy. This is an excellent book that has one chapter on code generation. It is a very rudimentary introduction to the topic, but once I realized I could have used it to create CRUD stored procedures in seconds (rather than hours), I became very interested. Repeat: very interested. I have used a few code generators that are available including CodeSmith and MyGeneration. More recently, I've been reading Kathleen Dollard's code generation book. Kathleen describes a technique for using XSL to generate code. I had only messed around a little with XSL a couple of years ago and found it a bit difficult and since I didn't have an urgent requirement to use it, I put it aside with a "that's useful and I'll keep it in mind for when I really do need it."
XSL is a wonderful text manipulation language and the bright idea that Kathleen had was to use it to generate code. I doubt the XSL/XML creators ever had this use in mind, but it is a great technology to use. Why? Well, you develop templates for the code you want to generate from a code sample, so the template looks somewhat like the code you will be generating. Second, it uses XML for the meta data. Even though XML is a bit bloated in some ways, it has to have some verbosity as it must describe the data it contains. Since database schema are in XML form, you can generate alls sorts of things as a result. Third, it is a language that allows loops, variables, conditionals, etc. So it has all of the things you might need.
Generated code comes in two flavors. There is code that is generated and never edited. It is used as is. Further, Next we have code that is generated but that you may need to tweak. If you use a code generator, it must be able to tell what is sacred or hand-crafted code from the non-sacred variety (which is safe to regenerate). One of the better ways to do this is create an MD5 hash of the file and if it's been subsequently edited, you'll know it because of the differing hash. Such a file is considered one of those sacred, handcrafted files.
There is another aspect to code generation that is important - a guiding principle of sorts. And that is you must be able to regenerate your code with a single click. Enter FinalBuilder. Two key aspects of code generation are being able to do XML/XSLT transforms. FinalBuilder has such an action. Furthermore, you can mark a file with an MD5 hash (also a FinalBuilder action) so your scripts will know not to overwrite a changed file. FinalBuilder also has actions to do basic file and directory manipulations, which are also part of a code generation scripting process. The idea of writing my own engine did not appeal much to me so I was very happy when the idea came to me to use FinalBuilder for this.
This weekend I was in crunch mode on a project and I needed to work out what was occurring with my application. I use Raize Software's CodeSite product. It has a custom formatter feature, that allows you to create an output window pane of data values of your choosing (e.g., properties of a class). I don't write these often and it's always been a trade-off of time. Now I have a template so I create an XML file of the output I want and run a script to generate the code. It works like a charm.
I will probably write an XML generator (when I have a few spare moments) for easily creating XML files that I might need for code generation. This would revolve around my own business needs, where there isn't already an existing data source or XML schema. Then I can use a FinalBuilder script to use the XML and XSL templates I develop.
Comments