The 'Permanent' SessionFactory, ASP.NET MVC and nHibernate

I've been building an application with Fluent nHibernate/ASP.NET MVC - and I've dug around and figured out that it's considered most appropriate practice to keep a 'permanent' SessionFactory open, and then use sessions for each request to the database. Okay, this sounds good...

I'm quite confused on how to accomplish this, though. Everything I find assumes an entire structured framework that uses some kind of IoC container system ...and that's just too advanced for what I have so far. Are there any more simple examples of how to implement this kind of design?

I've taken a look at Where can I find a good NHibernate and ASP.NET MVC Reference Application

And even read the book "ASP.NET MVC in Action", but it's example is just far more complicated than what I am trying to achieve. I thought a singleton model would work in the Application_Start of the 'global.asax' but that didn't yield the results I had hoped for. It would keep disposing of my factory and never recreating it.


You could expose the ISessionFactory as singleton:

public sealed class FactoryManager
    private static readonly ISessionFactory _instance = CreateSessionFactory();

    static FactoryManager()
    { }

    public static ISessionFactory Instance
        get { return _instance; }

    private static ISessionFactory CreateSessionFactory()
        // TODO: configure fluentnhibernate and create a session factory

Now you could use FactoryManager.Instance in your code:

using (var session = FactoryManager.Instance.OpenSession())
using (var tx = session.BeginTransaction())
    // TODO: use the session here

Need Your Help

Search in C# bringing back multiple rows - datagrid view

c# sql datagridview duplicates rows

Ok so I have a function to search the DB for a record based on a number, then display it in the datagrid, this used to work fine but for some reason when I copied the code over sometime ago it now ...

data from json file to postgres table

python json postgresql

I have a json file from which i want to put data in my postgres table.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.