Tuesday, September 27, 2011

(Yet another) nHibernate sessionmanager for ASP.NET (MVC)

Working with a nHibernate session in an asp.net web application is a subject to many a blog post, forum question or overflowing stack. Having already made a contibution before, I am nevertheless going to add another go at it combining all I’ve met and trying to find a way to meet the objections to the different approaches.
The problem

Accessing a database via nHibernate takes four steps.

    Create a sessionfactory.
    Open a session.
    Access data
    Close the session

Creating a sessionfactory is a very costly process, don’t do this to often. Opening a session is fast but results in an open connection to the database. Database connections are a costly external resource. Despite session pooling connections should be closed as soon as possible.
Strategies

Trying to balance to cost of creating a sessionfactory and the cost of an open database connection I’ve found several ways to manage the session.

    Do it yourself.  Create the factory whenever the app needs data and fiddle on from there optimized to the occasion. Needless to say that’s not going to work unless you have a very simple app.

Read more: CodeBetter
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://codebetter.com/petervanooijen/2011/09/26/yet-another-nhibernate-sessionmanager-for-asp-net-mvc/

Posted via email from Jasper-Net