Site Loading

EDIT: I have decided to update this post with the current features of Nhibernate and include features that have become available since I first wrote this post:

My company has recently decided to update the Data Access of our application and I have started looking into ways to design and structure the new Business Logic and data access.  Currently we are looking at using NHibernate, so this is my guide to setting up NHibernate.  I have attached a working solution to this post.  Please feel free to download it and have a look:  Project Link.

Basically NHibernate is a way of managing information from an application to a relational database.  It is part of Hibernate.Core developed for the .Net platform.  The way NHibernate works is by mapping the database to the objects with the use of  mapping classes.

The first thing to set up is the Config file.  This either goes in the web.config or the app.config.  To do this you add your connection string details:

<add name="DB" connectionString="server=localhost;user id=<USER>;password=<PASSWORD>;database=test"/>


The next step is defining the business object.  The is a fairly self explanatory process and could be automated using code generation tools unless you have a lot of patience.  The idea here is to match the fields in the table(s) with the properties you want to be available in the object.  I generally suggest using a partial classes.  The reason for this is that if there is any class logic you want to include in the object and you need to re generate the class for any reason, you won’t lose the changes you have made.

Once all the objects have been created, the mapping classes will need to be created.  The format of the mapping classes are as follows:

public class PersonMap : ClassMap<Person>
    public PersonMap()
        Id(x => x.Id);
        Map(x => x.Address1);
        Map(x => x.Address2);
        Map(x => x.FirstName);
        Map(x => x.LastName);

The mapping links the Database table(s) to the object.  It does this by specifying the class to use as well as the table.  Obviously then the properties are matched with the columns.  You can have multiple tables in an object, so you are not restrained to one class per table.

Once the mappings have been done the last step is to create the data access object.  Using NHibernate here is really simple and show the incredible flexibility and the power of NHibernate.

To access the database, a session object will need to be created.  This creates a session for use with the database.  It will be stored in the application cache or context if the application is a Web application.  To start this I have created a session manager which creates the session object as well as configures how the application connects to the database:

SessionFactory = Fluently.Configure()
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))

The rest is really easy.  To save and object all you do is call the save method on the session object and pass the object.  The same applies for the delete and update.  The loading of data is incredibly flexible and by default is lazy loaded.  The easiest method of loading data would be getting the object by id, or using the criteria object in NHibernate or using linq queries as per the example below.  Examples of this are:

public Person Get(int id)
    using (ISession session = new SessionManager(_connectionString).Open())
         return session.QueryOver<Person>().Where(x => x.Id == id).List().FirstOrDefault();

public Person Save(Person model)
    using (ISession session = new SessionManager(_connectionString).Open())
        if (model != null)

    return model;

So using NHibernate shows that you are able to add a flexible simple data access solution to you project.  I am amazed by the ability to be able to change or add to the database without many detrimental effects on the project.  NHibernate makes coding simple and effective.  I would love to hear comments from people who have used NHibernate in projects, and how it went.