views:

134

answers:

2

I have a asp.net application with Nihbernate setup, now I want to convert it to Windows form application.

Here is the code which have been setup in the Global.asax.cs. Can anyone give me some code sample how to do this in Windows form?

  protected void Application_BeginRequest(object sender, EventArgs e)
    {
        ManagedWebSessionContext.Bind(HttpContext.Current, SessionManager.SessionFactory.OpenSession());
    }

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionManager.SessionFactory);
        if (session != null)
        {
            try
            {
                if (session.Transaction != null && session.Transaction.IsActive)
                {
                    session.Transaction.Rollback();
                }
                else
                {
                    session.Flush();
                }
            }
            finally
            {

                session.Close();
            }
        }
    }
+1  A: 

Well, there are several methods for accessing ISessionFactory in statefull application (and a desktop application is that kind of application), among them:

Singleton You could build the session factory once during the startup of your program and access it through a static singleton class. This would force the application to use only one instance of the session factory.

example:

 public sealed class NHibernateHelper
 {
    private static ISessionFactory SessionFactory;
    private static readonly Configuration NhibernateConfig;
    // ....
    static NHibernateHelper()
    {
        NhibernateConfig = new Configuration().Configure();
        SessionFactory = NhibernateConfig.BuildSessionFactory();
    }

    public static ISessionFactory GetSessionFactory()
    {
        return SessionFactory;
    }
    // ....
 }

... and access the session factory through GetSessionFactory method all over the application.

Context Object and/or Dependency Injection

You could build the session factory from configuration and pass it through a context object all over the application.

example:

  1. during startup:

    // here you configure NHibernate.
    ISessionFactory _sessionFactory = BuildMySessionFactory();
    // ...
    ObjectFactory.Initialize(x =>
    {
                   x.For<IPatientRepository>()
                     .Use<StandardPatientRepository>()
                     .Ctor<ISessionFactory>().Is(_sessionFactory);
    
    
    // ... initialize the rest of your repositories...
    });
    
  2. then:

    public class StandardPatientRepository : IPatientRepository
    {
        private readonly ISessionFactory _sessionFactory;
        public StandardPatientRepository(ISessionFactory sessionFactory)
        {
            if (sessionFactory == null)
                throw new ArgumentNullException("sessionFactory");
    
    
    
       _sessionFactory = sessionFactory;
    }
    public virtual Patient Get(Guid id)
    {
        using (IStatelessSession session = 
                   _sessionFactory.OpenStatelessSession())
        {
            return session.Get&lt;Patient&gt;(id);
        }
    }
    // the rest of data-access methods.
    
    }

then in your classes that will make use of the data (ie. use the repositories) you will use:

Patient = ObjectFactory.GetInstance<IPatientRepository>().Get(patient);

In my opinion the second method is better as I think that singleton in most cases is an anti-pattern. The second approach will give you more control over your data layer, you will know who and when is accessing it.

Karim
Hi Karim thank you. Could please send me a small working desktop application as a sample?
Daoming Yang
what's your e-mail?
Karim
+1  A: 

Here is a well done and extensive sample application using NHibernate in a desktop application:

Building a Desktop To-Do Application with NHibernate

Managing the NHibernate session in a desktop application tends to be a lot more involved than managing the NHibernate session in a web application.

Michael Maddox