NHibernate first attemp in blog system

As mention in the previous post, i started my application with first try in NHibernate. Wow it was sweat try. Eventually i got it works.

After nearly 4 hours fighting with NHibernate and my application, i had these in general:

general structure
general structure

Let says a little bit about them before continuing:

  • AnhDuc.Blog: Web site application
  • AnhDuc.Blog.Business: Project contains all business logics for the site
  • AnhDuc.Common: Project contains common things which can be used widely or in other projects.

Since i was using NHibernate then there was not DataAccess project.

Time to talk about using NHibernate. I have read some article and NHibernate In Action book to accomplish the usage. Basically here are steps:

Required Dlls:

Download NHibernate dlls and other required dlls. You can easy get them by using your friend: Google, thus i do not mention here.


First, you need these in webconfig. All you need to do is copy this part and paste in your config then change the connection string to the right one: 

< hibernate-configuration xmlns=urn:nhibernate-configuration-2.2 >
< session-factory>
<property name=connection.driver_class>NHibernate.Driver.SqlClientDriver</property>
<property name=connection.connection_string>Server=(local);initial catalog=anhduc_blog;Integrated Security=SSPI</property>
<property name=adonet.batch_size>10</property>
<property name=show_sql>false</property>
<property name=dialect>NHibernate.Dialect.MsSql2000Dialect</property>
<property name=use_outer_join>true</property>
<property name=command_timeout>60</property>
<property name=query.substitutions>true 1, false 0, yes ‘Y’, no ‘N’</property>
<property name=proxyfactory.factory_class>NHibernate.ByteCode.LinFu.ProxyFactoryFactory,


<mapping assembly=AnhDuc.Blog.Business />



Then i came with code to initialize the configuration: NHibernateSessionManager class.

public sealed class NHibernateSessionManager
private static readonly ISessionFactory _sessionFactory;
private const String CurrentSessionKey = “anhduc.blog.nhibernate.currentsession”;
static NHibernateSessionManager()
Configuration cfg = new Configuration();
//cfg.AddAssembly((typeof (BlogItem)).Assembly);

_sessionFactory = cfg.BuildSessionFactory();

public static ISession GetCurrentSession()
HttpContext context = HttpContext.Current;
ISession session = context.Items[CurrentSessionKey] as ISession;
session = _sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = session;
return session;

public static void CloseCurrentSession()
HttpContext context = HttpContext.Current;
ISession session = context.Items[CurrentSessionKey] as ISession;
if (session == null)

public static void CloseSessionFactory()
if (_sessionFactory != null)

public static void CommitTransaction()

public static void AbortTransaction()

It contains some methods to deal with transaction.

The configuration is done, and ready to use. Come to your business class.

Business classes:

They are all under AnhDuc.Blog,Business project. Each class has its own mapping file. All mapping files are put under hbm folder. I am not going to explain in detail about mapping here. However, it is the most important thing when you work with NHibernate.

Be aware of inheritance when designing your domain. In many cases, inheritance is not encouraged.

Recommended book: NHibernate In Action June 2008

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: