static void

NHibernate

Simplistic "get started" code for NHibernate (version 1.2 and 2).

  1. Download the latest version from NHForge.org
  2. In your data access layer, reference the NHibernate.dll, plus the proxy generator (eg Castle). Optional extras include Fluent mapping and Lambda Extensions.
  3. Create the configuration (contains connection string).
  4. Create the domain entities. Entities are mostly "POCO"s but you probably should inherit from a base class for identity.
  5. Create the mapping to the database tables (or use Fluent mapping)
  6. In Application Start create the session factory (don't create it for every session or operation).Simplest is to hold it in a static or singleton object such as a SessionManager.
  7. Open an ISession from the Session Factory and use Load/Get/SaveOrUpdate etc- then Flush/Commit to database. In asp, typically create a session with a begin request, but you could store it in Session to span requests ("conversations"). Ideally create repositories (data access objects) for each domain entity, using the SessionManager.

Example Domain

Here we just use Category and Product from Northwind.

Here are the same entities in ActiveRecord- the mapping is done by property attributes.

NHibernate helpers

NHibernate helpers - optional

Intermediate