3

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.

Community
  • 1
  • 1
Ciel
  • 17,312
  • 21
  • 104
  • 199

2 Answers2

5

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
    tx.Commit();
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Do I want to always 'Open' and 'Close' The session? I'm sorry if I sound stupid, this is very hard for me to grasp. SessionFactories create Sessions - and Sessions are 'disposable' - but I want Factories to remain Singleton? – Ciel Dec 02 '10 at 17:48
  • A session factory is an expensive resource to create that's why it is recommended to create it only once for the lifetime of the application and reuse it. They are also thread safe. Sessions are created using the session factory every time you need to access the database. They are very fast to create and could be opened and closed on every query. Contrary to session factories, sessions are not thread safe. – Darin Dimitrov Dec 02 '10 at 17:51
  • This is exactly what I want to do. I'm just experiencing problems with factories being recreated often in my test code. In a live scenario, that shouldn't happen though, right? I'm not 'launching' the application often, and so the place where it is made should not be called unless I restart the website. – Ciel Dec 02 '10 at 17:54
  • @Stacey, if you use the singleton pattern, by definition you will get only a single instance of the object for the lifetime of the application and unless you restart the web site it will always be this same instance. If you are testing with Visual Studio you are always recompiling and thus restarting the web site, so the session factories will be recreated. – Darin Dimitrov Dec 02 '10 at 17:57
2

Make a static GetSessionFactory method on your global MvcApplication class. This method initializes a session factory the first time it is called and stores it as a private static variable. Upon subsequent calls, it simply returns the static variable.

This method can also check to see if the object is null or disposed and recreate as necessary, though it shouldn't happen since the variable would be static and thus, stay alive for the duration of the application's lifetime.

Chris
  • 27,596
  • 25
  • 124
  • 225