Inspired by Nic Wise’s question regarding how I generate the mapping files for NHibernate, I wanted to write a bit about my experience with automatic code generation tools and O/R Mappers. This is not a discussion of O/R Mappers versus automatic code generation, but my own experience on how to combine the two.

When I started using O/R Mappers in the beginning of 2005, I looked around a lot for tools for me to generate all the trivial code that still has to be written: xml-mapping files, business entity classes, basic queries and the creation of the database structure.

What struck me was that most tools actually generate the mapping-files and the business class code based on a database structure. I find that a bit odd, since a basic principle in designing object oriented applications is to start with the domain model and then create a data structure to represent it.

Anyway, I played around with Paul Wilsons O/R Mapper and the open source NHibernate as well, and finally I found a tool called ObjectMapper made by Matssoft (Mats Helander) which actually proposed to do what I wanted (it doesn’t seem to be available right now though). What it permitted me to do (in theory) was to model my domain visually like in visio, and then automatically create the necessary classes, mapping files and database structure (I don’t remember now if it could do that for several different O/R Mappers or only NHibernate). Anyway, it sounded great, and perhaps too great, since I never got it to work and only got a few exception errors when I tried to use it. Hopefully the next version will be bugfree since the perspective was quite good :)

Finally I ended up doing the codewriting myself, which is also a learning experience, but a few months ago I tried out the two automatic code generation tools, CodeSmith and MyGeneration. CodeSmith comes with a 30-days trial, MyGeneration is freeware, and both tools are based on the same principle of generating code from a customisable template. You can create your own template or download already exisiting ones, and of course that last alternative was the one that I was looking for then. And I actually found templates to generate code for NHibernate with both tools. MyGeneration has three templates available, although two of them are enhancements of the third, and CodeSmith has a template as well. They all create almost identical code and seems to be at the same “basic” level. This means that they do some of the work for you, but in no way go all the way. You identify the database tables to be mapped, and then the mapping files and the business classes are generated automatically. This is all fine, but if you for example have tried out Paul Welters template for Wilson’s ORMapper then you would miss the automatic generation of a host of general save and get-methods that you need as well apart from the direct mapping of properties and relations.

What also caused me a bit of trouble was that the m-n relations where represented with an object for the relation as well, even though the relation had no properties in the database apart from pointing to the two entities it was relating. I realize that for future maintenance it is easier to start out like this so I wont have to refactor as much if the relation do need properties in the future, but adhering to the YAGNI principle from the methods of Extreme programming, I didnt want to create extra complexity, that I might not need later on.

Anyway my conclusion was (and remember that was a couple of months ago) that when you need to generate automatic code and mappings for NHibernate, either of the tools, CodeSmith and MyGeneration, are valid. In the case you want to enhance the templates (anyone?) that is a completely different question, since I didn’t dig into how the templates are programmed.