Sunday, April 24, 2011

NHibernate Tutorial, Part 1 – Introduction

Hi everyone,

I’ve been working with NHibernate for almost two years now, and it’s been a pretty fun ride. Since I’ve learned a lot along the way I thought I should provide an NHibernate tutorial, specifically working with spatial entities since I don’t think there’s enough material out there on that topic.

Now, there’s a LOT of knowledge and good NHibernate tutorials there so I’ll try to give a link to another article where I see fit, starting with:

  1. A screencast tutorial from summer of NHibernate, for a more visual tutorial. I can’t say I’ve seen it since I’m more of a reading kind of guy, but I got the impression it’s good.
  2. NHibernate 3 Cookbook – a great book that that introduces real life NHibernate usages and tools.
  3. Oren Eini’s (AKA Ayende Rahien) blog – One of NHibernates contributors and the writer of a great software design blog.

I’ll also combine Domain Driven Design principles along with this tutorial, as we develop our own application. Let’s go.

Some History…

Let’s look at a simple example:

domain

Ignore the base class IntEntity for now, we’ll get to it later – just keep in mind that each of the above entities also has an Id property and a version property, both of type int.

Now, a single Store has many Branches, and a branch has a reference to it’s parent store. Also, each store has a reference to a Person who’s it’s contact, and each Branch has a person who’s it’s manager. A simple model in OO, but a bit more complex in a relational database:

erd

A property becomes a foreign key and we have a One-to-Many relationships – it’s up to us to make the conversion back and forth to objects. Now, we want our application to persist these entities while keeping the entities oblivious of the underlying mechanism.

If you’ve ever worked with ADO.Net or any other DAO framework, you know it means a lot of implementation (even if we auto-generate some code) – each entity has it’s own SQL queries for CRUD operations that requires manual implementation and maintenance, not to talk about inheritance modeling. Also, it doesn’t’ support refactoring since it’s based on pure SQL strings.

Oh, and did I mention we want the same entities to be persisted on Oracle10 and Sql Server 2008, and tested against Sqlite?

NHibernate to the rescue!

NHibernate is a mature, open source OR/M framework, which basically means it allows us to model our entities naturally as objects, and define how they should be be to the underlying RDBMS in a more declarative way. It was originally ported from Java’s Hibernate thought it has a life of it’s own for some time now.

Microsoft also provides an OR/M framework called Entity Framework, it’s not as mature but it’s growing fast and it has a great designer support, if that’s your cup of tea. Still, NHibernate is more mature and supports more features.

To start working with NHibernate, we need to map our model, configure our database connection and settings and actually use them in our application.

We’ll continue on the next part - Modeling Our Domain.

1 comment:

  1. Hi Mattan, Its a good idea, as there doesn't seem to be much on spatial entities and Nhibernate.

    Did you do manage to do anything on the spatial entities in the end?

    Cheers, Mickey

    ReplyDelete