Introducing NHibernate

Hibernate is a popular Object Relational Mapper in the Java world. Now there is a port for .NET called NHibernate. Like Hibernate, NHibernate maps .NET objects to the database. It is licensed under the LGPL. In other words, it is free. This is becoming a popular method for .NET web developers to interface with a database.

NHibernate works just like normal desktop application persistence technology. As such, there is a tendency to have one global session for your whole application. However .NET experts recommend that you keep session lifetimes down at the transaction level. This will cut down on memory leaks as well as other problems.

Note that the NHibernate session is not exactly the same as a database connection. This session is managed by the NHibernate framework. Actual database connections may come and go with one NHibernate session.

Here is a peculiar fact with NHibernate. You have to make your class members virtual. That applies to all members. You should also be aware that the time for NHibernate to start up is long. Yes I know. That is no fun. So kick the NHibernate stuff off to a separate worker thread. Just recognize that NHibernate is not thread safe in the sense that two threads cannot access the same NHibernate session at the same time.

That’s all for now. Next time I want to revisit ASP.NET 4.0.