Recently I have been experimenting with a simple technique for code generation that has a lot of power and flexibility. I use NANT to intelligently drive the rendering of code. This is accomplished via HTTP GET of asp.net web pages or via XSLT transforms from an NANT task. The ASP.NET web pages are written to transform metadata (database schema, UML model, O/R mapping, UI descriptions, etc.) into source code plain-text in the same way that you would render HTML from data in an ordinary web application.
The advantages of this approach are:
-
Known technology lowers the barriers to acceptance by management and other programmers. IIS/ASP is a known factor that does not need to be explained and researched.
-
Using a build tool such as NANT is a good practice and should also be well accepted as a familiar technology and process.
-
The technique for building the code generation templates (ASPX pages) is familiar to a programmer. You will be using the same toolset as you do today, so the process is mature and low risk. You get to use a million dollar IDE (Visual Studio) to build and debug your templates, as opposed to a simple template editor or proprietary wizard environment that ships with many code generators.
NANT has many useful commands that assist in managing code generation. This is key, because generation of output files is only half the battle. Management of templates, dependencies, configurations, metadata files, and source control integration are major issues in making code generation a success on any non-trivial project. NANT has facilities for source control integration, unit testing integration, CRC/Hash generation and comparison (useful for ensuring generated code does not get modified), XML manipulation via Xpath and XSLT, logging and more. The key command that allows us to use NANT for generation with IIS/ASPX is the HTTP GET command. For a list of all the NANT commands look here and here.
Using this open ended toolset is in contrast to many code generators on the market today that are proprietary, closed and require some education, acceptance and a certain amount of faith in the vendors framework and worldview. The value of this shouldn’t be underestimated. Some of the most serious problems hindering code generation acceptance are the barriers imposed by closed, proprietary systems that have little flexibility, minimal documentation and tribal knowledge. ASPX and IIS together are extremely flexible and robust, incredibly well documented and there is a glut of tribal knowledge and source code out there.
In part II I will describe some of the details of how I implement this method, with some tips and discussion of trade-offs in various approaches.