NHibernate - good complete working Helper class for managing SessionFactory/Session

前端 未结 9 728
余生分开走
余生分开走 2020-12-07 14:37

can anyone provide/refer a proper OO type helper class for managing a singleton of the SessionFactory and then also for managing Sessions?

9条回答
  •  被撕碎了的回忆
    2020-12-07 15:32

    Sure, this is what I used when I was getting started with NHibernate:

    Session Factory

    public class BaseDataAccess
    {
    
      protected ISession m_session;
    
      public BaseDataAccess()
      {
        m_session = NHibernateHttpModule.CurrentSession;
      }
    
      public static ISession OpenSession()
      {
        Configuration config;
        ISessionFactory factory;
        ISession session;
        config = new Configuration();
    
        if (config == null)
        {
          throw new ArgumentNullException(nameof(config));
        }
        if (factory == null)
        {
          throw new ArgumentNullException(nameof(factory);
        }
        if (session == null)
        {
          throw new ArgumentNullException(nameof(session));
        }
    
        config.AddAssembly("My.Assembly.Here");
        factory = config.BuildSessionFactory();
        session = factory.OpenSession();
        
        return session;
      }
    } 
    

    Let me know if that helps.

提交回复
热议问题