Code Generation

I am a huge fan of code generation.  I've used it to incredible advantage.  I'm talking about real dollar value impacting effect that could make or break a project.  If you're reading this and haven't yet drunk the Kool-Aid, then now is the time.  Read on.

Code Generation is the process of letting code write code, usually at design-time.  There are tons of examples on the net to demonstrate this and I won't repeat them here (follow some links below and you'll get to some fine examples).  Instead I'm going to discuss the concept pure theoretically.

When to "CodeGen" : The criteria for this are quite simple.  If you have something which already defines the structure you desire and you are trying to reflect that same structure in something else, then you should use code generation.  This is why data access layers are the classic example, because you are mimicking the structure of the database in your data access layer.

Why to "CodeGen" : Time and Agility!  The time is takes to setup a code-generation system is a fragment of the amount of time it takes to write the same code for all source entities.  Your template takes slightly longer than the time it takes for one class, but can then be instantly be applied to all your source entities.  If you have a database with 50 tables, write one class, turn that one class into a template and bingo ... 50 classes.

Agility is the other reason.  Let's say you code all 50 classes by hand, and then the next day the DBA refactors your database and adds a new table which has foreign keys to lots of other tables.  Your 50 classes, pretty much toast.  If you have generated those 50 classes using code generation, then this is no problem, simply re-run your same template (no changes there, it's agnostic to what database it's looking at) and bingo, 51 classes all with the right relationships.

What to "CodeGen" : Data Access Layers are popular, but so are lots of other things.  Databases make a good structured source of entities you could generate against, but so are XSD Schemas, XML Files, and lots of other stuff.

Where to "CodeGen" : There are lots of great tools to use for code generation out there, so many I could likely exhaust my fingers typing them all, but there are just a few which come with my "Stamp of Approval":

  • CodeSmith - From CodeSmith Tools this is, in my opinion, the greatest code generator going.  I use it for almost all my code generation.  The syntax is very much like ASP.NET and is easy to manage.  The product is also easy to integrate into a nightly build process.
    • .NET Tiers - This is a set of templates create by the community for use with CodeSmith.  These templates will generate stored procedures, data access layers, business objects and web controls all from your database at the push of a button.  Oh, and it all has Unit Tests.  Check this out.
  • XSDObjectGen - One of the few things CodeSmith does not do well, yet, is generate from XSDs.  XSDObjectGen creates Serializable classes which represent your XSD's structure completely.  XSD.exe, which comes with Visual Studio, does similar things, but XSDObjectGen does it 10x better.