Doug King

Updating the GAC...

Code generation with NANT and IIS/ASPX

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.

Comments

Damian said:

This sounds very similar to what CodeSmith does, but it doesn't have the IIS requirement. The templating langauge is very similar to ASPX though and can be run from a command line (or by NANT).
# February 10, 2004 5:40 PM

JosephCooney said:

I wrote a sample on GotDotNet that hosts the ASP.NET runtime to do code generation like this (similar to what you're doing but without IIS). You specify an XML document that gets passed to the ASP.NET runtime, and a template file (really a .aspx page) to run. My demo also has a command line version. Here is the URL

http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=dfef25fa-f545-4289-b9fc-05270ea41fb3

There are a couple of other templating type code-gen tools out there for .NET. OlyMars is one, and CodeSmith is another. NVelocity is another one (a port of the Apache Jakarta Velocity project from Java to .NET). To the best of my knowledge mine is the only one that re-uses the ASP.NET templating engine.

I think that using these sort of templating tools is a good way to generate code. It give you a lot of power (since you can use all of the .NET framework), lets you work in an environment that you are familiar with (if you are a .NET developer) and lets you make changes to your templates and view the results almost immediately.
# February 10, 2004 6:08 PM

Doug King said:

I am familiar with CodeSmith and other ASP style generators such as ProcBlaster. I even wrote my own recently, complete with editor and debugger, ready for prime time. At one point I thought I would turn it into a commercial product. Although CodeSmith is flexible (no framework lock-in), you still don't get the quality of tools that you get with Visual Studio, which in my scenario is probably what you would be using to build and debug your ASPX generator pages. Using IIS doesn't seem to be a stringent requirement - it's available to most developers and the VS IDE makes it easy to create and debug web apps.

Joseph I think you've got the right idea. I must have researched just about every generator out there, installed and tested demo versions of 6 or 7 of them (CodeSmith was the best of the bunch in my opinion) and even built my own. Finally I settled on the approach outlined above. Leveraging common well documented technology that can be configured in a number of ways is what I was striving for. The missing link was a flexible build tool to drive the process and with NANT that is taken care of. In the end I think this approach is the most open, flexible and powerfull for a .NET developer.
# February 10, 2004 6:45 PM

Michael Hensen said:

Keep me posted on this if you will..

Currently I am working on a new website. This needs to be as flexible as one can get in either frond, cms and database.

I now start to build it up using a way to define objects. Build the objects in binary format and store its data in a serialized format in sql. As a page consists of modules and each module constists ob objects I came up with this idea.

Regards..
Michael Hensen
# February 11, 2004 3:26 AM

TrackBack said:

^_^,Pretty Good!
# April 10, 2005 6:35 AM

TrackBack said:

^_^,Pretty Good!
# April 15, 2005 6:57 PM