Introducing the NHibernate QuickStart (NHQS, Part 1)

If you’ve worked with me over the past year or you’ve seen one of my alt group meetings, chances are I’ve mentioned, demonstrated, or maybe stuffed down your throat, the NHQS thingamabob. With the improvements in NHibernate following .NET 4.0 and the experiences I learned in a few projects in the previous release I’ve decided to rewrite NHQS for NHibernate 3.xx. I like the improvements in NHibernate and with those and a slightly renewed focus on my goal for NHQS I felt it might be worthwhile to introduce it on its own into the wild. This post will introduce the world to NHQS and provide a guideline for its usage.

As usual the project is only as effective as is the goal it intends to serve so let’s get that out of the way.

A lot of developers want to use NHiibernate, or Fluent NHibernate, or some flavor therein, but don’t want to learn the layers and layers of how-and-why it works just to get up and running. NHQS should mitigate this confusion and ease the transition into usage of NHibernate. A secondary goal of NHQS is to make it easy for a developer to have access to any and all persistent storage mechanisms supported by NHibernate in a fluent and domain-centric manner. At the highest level, a developer facilitating object persistence via NHQS should neither need to know nor be exposed to the underlying persistence mechanism.

To accomplish that goal we’re going to walk through the steps of setting up and using NHQS to access multiple databases via the almost-too-simple-to-be-legit fluent/generic interfaces exposed almost solely via extension methods. I’m sure I’ll get burned at the stake for failing to follow someone’sNHibernate Best Practice 101 list, so I’ll apologize up front. With that, here’s the synopsis of the blog series, which I aim to complete by the end of this week.

  1. Introduction (this post)
  2. Session Factory Creation and Containment
  3. Session Fluency Extensions
  4. Transactions and Multiple Database Support

NHQS in 30 Seconds

There are a few steps one must follow to get their persistence mechanism accessible via NHQS. Here they are:

  1. Implement the ISessionFactoryCreator interface via a class of your own (we’ll cover this in a moment)
  2. Add code to the startup of the calling application (or in your TestFixtureSetup method) that calls the Create method of that class to create an NHibernate ISessionFactory object instance
  3. Take the resultant ISessionFactoryand store it using the SessionFactoryContainer class
  4. Write DDD-like code to get objects from databases and perform CRUD operations via an arsenal of extension methods attached to areas of NHibernate

The second post in this series will begin the process of using NHQS in a real-world situation by simplifying the art of creating ISessionFactoriesand storing them in a container, so check back soon.

If you’d like to skip the blogs and look at the source, feel free, I keep the NHQS source in Github.