don't know about you but when I think about writing applications I think about all of the things I want the application to do for myself or for the customer. What I don't think about is all of the little details that turn an elegant design into a workable implementation, because so often the beauty of the design gets lost during the implementation process.
Alleviating this loss of design integrity is the main reason I'm so enamored with code generation. By using a program to translate an abstract model of my design into code I can maintain the design and also gain a measure of portability that no single implementation language can provide. While code generation provides many benefits, it's the portability aspect that I want to analyze in this article.
For those not familiar with the software engineering technique of code generation, I'll run through a brief introduction. Code generation is the technique of using a program to write production code for you. There are many models of code generator. The example in this article uses an abstract model of the design as input, and it outputs one or more implementation files.
There are four primary benefits to code generation; quality, consistency, productivity, and abstraction. Generated code is of high quality because it is being built using a set of templates. When bugs are found the templates are modified and the bugs are fixed across all of the generated code. Because it uses templates, generated code has consistent naming conventions across all of the output. The technique increases productivity by letting developers spend more of their time on the design and the unique aspects of the code.
|Part of the design of the application is abstracted into the generator's input files; this abstract model can then be ported to other platforms and technologies simply by altering the templates of the code generator. |
But it's the abstraction benefit that is the primary focus of this article, because abstraction is what gives our applications portability. Part of the design of the application is abstracted into the generator's input files; this abstract model can then be ported to other platforms and technologies simply by altering the templates of the code generator.
To demonstrate this I will use a code generator to build the data access layer of an application with two very different implementation technologies. I'll begin by defining the design of the database that I am going to generate.