I don't know about you, but sometimes if you want to learn a new framework, you want to see results right away. Most of the time, it never happens that way. Let's take nHibernate as an example. For somebody that is very new, you go to tutorials, how to create one and hope it works. One thing that pissed me off when I was learning NH back in the days, was writing those .hbm files.
Don't you wish there was an easy way to learn nHibernate, learn how to use sessions, transactions, unit of work, and all that good stuff? Ofcourse we have fluent-nhibernate as a good way to start up. But what if (1)you're using .net 2.0? (2)you don't like how integrated the mapping is with your application?
One day, I decided to spike a generator that generates your .hbm on the fly based on your domain objects.
Word of caution. I just used this as a spike, not a solution - More like a fast way of developing a small app without the pain of .hbm.xml files. Also, this exercise shows that you can have a domain model, totally ignorant how stuff is being saved to some kind of a persistance layer - hence mapping files. And finally it's totally doable to generate a mapping file on the fly based on your domain object.
Now let's talk code. The way this exercise was done, I was taking advantage of the Chain of responsibilty pattern to build the mapping files.
Before we go with any code, lets follow the basic rules:
- Every .net data types are property
elements - Every custom objects are many-to-one
elements - Every IList
<> are bag items
The Model
The Test
The IHandler Interface
The Mapping File Builder
The Handlers
The Implementation
