<< back to blog

Zero-effort mapping for NHibernate?

nhibernate ~ .net

08 Aug, 2008

If you're working with NHibernate, you may realize you have several options for defining your mappings.

Sometimes I don't want to write XML. Neither do I want to use attributes. In fact, I don't even want to do any kind of mapping. This is mainly because:

  • I'm lazy
  • XML is verbose, boring to write, ugly and hard to debug
  • Attributes often represent a separate set of concerns to those of the class they decorate. This makes them a distraction.
  • Mappings often smell of duplication. Much of the time my mappings repeat much of what my classes already say.

Writing mappings is tedious. A computer should be able to do it for me. I'd rather just write code and have my mapper figure out what to do with it. There are exceptions, of course, but most of the time this is how I feel.

I started playing with some code to create inferred mappings. I got a tiny example working.

Essentially, I can write a class like this:

public class Customer
    public long Id { get; private set; }
    public string Title { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public List<Order> Orders{ get; set; }

Then, my mapper class can infer the NHibernate XML mapping based on conventions such as:

  • Entities always have an Id property which represents their persistent identifier.
  • Lists such as List always get mapped as an NHibernate list.

The beauty of this approach is that 80%-100% of your mappings can be done hands-free, automatically at run-time. You just write domain objects making sure you following some sensible conventions. Of course, the conventions should be customizable so style & rules can be altered to suit the project/team.

In my short spike, I bastardised a copy of the great work already done by Pierre Henri Kuate (et al) in the NHibernate.Mapping.Attributes library.

My idea was that this library should continue to work as usual, but if no mappings attributes are found, it infers them based on sensible defaults. So, you can define zero attributes on your classes and still get mappings for free.

Sadly, I can't take this any further right now. I just wanted to test the basic approach (and dig around some NHContrib code!).

Once I've finished with NHibernate in Action, I may give this project another look. Does it have legs? Can you shoot it down in flames?

If it's a runner, I'd suggest it's done as an extension to NHibernate.Mapping.Attributes. Perhaps called NHibernate.Mapping.Inferred or similar.

You may also like...
.NET Gem Fun
Automating .NET development (and NHibernate) with IronRuby + Rake
Get a Rails-Stylee Interactive Shell For Your NHibernate Backed Domain Model
In Memory SQLite Testing with NHibernate
NHibernate Trick
NHibernate: Calling Update Unnecessarily
.NET Config for Multiple Developers
NHibernate in Action 2nd Edition - Your Opinion
Shorthand Repository Accessors
NHibernate for Ruby?

kick it on DotNetKicks.com
blog comments powered by Disqus