<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      NHibernate One Session Per Request簡單實現

       

      哈哈,先廢話一下.

      研究NHibernate應用算起來也有兩個禮拜了, 自己也想總結一些自己的用法寫在博客當中,但是一直都沒有時間, 回家冷也就不想寫了,想知道今天為啥開始寫了么? 哈哈, 住在現在的地方已經快半年了, 房間一直缺把椅子,也沒有時間去買(哈哈,一聽就知道是借口), 今天跟同事聊天無意間提起, 說送我一把椅子,哈哈, 現在知道為啥開始寫了么? 不知道的去面壁.

       

      (切入正題)

          只是打算寫一些用法,具體的思路大家一起慢慢交流探討吧,因為我也是剛學, 今天快下班的時候才測試成功, 先寫出來,有問題大家在慢慢討論.

         期間主要參考了博客園NHibernate小組的兩篇討論:

         1. http://home.cnblogs.com/group/topic/34139.html(這兒應該不算轉載吧)

         2. http://home.cnblogs.com/group/topic/34273.html

       

          要實現One Session Per Request有以下幾點需要注意:

          1. 在hibernate.cfg.xml中加入一下property配置參數:

        <property name='current_session_context_class'>web</property>

       

           此句是制定session context的實現類, NHibernate中文文檔中的說明是: “hibernate.current_session_context_class配置參數定義了應該采用哪個NHibernate.Context.ICurrentSessionContext 實現。一般而言,此參數的值指明了要使用的實現類的全名,但那三種內置的實現可以使用簡寫,即"managed_web", "call","thread_static", and "web", 引自 NHibernate中文文檔 –>  2.3. 上下文相關的(Contextual)Session節”

       

       2. 創建NHinbernateSessionFactory類, 我是在Dao層創建的, 這個不一定,看自己怎么認為.

       

      NHibernateSessionFactory
          public class NHinbernateSessionFactory
          {
              
      public static readonly string CurrentSessionKey = "NHibernate.Context.WebSessionContext.SessionFactoryMapKey";

              
      /// <summary>
              
      /// 為方便獲取,增加一個CurrentSession屬性
              
      /// </summary>
              public static ISession CurrentSession
              { 
      get { return GetCurrentSession(); } }

              
      public static ISessionFactory _sessionFactory = CreateSessionFactory();

              
      public static ISessionFactory SessionFactory
              {
                  
      get
                  {
                      
      if (_sessionFactory == null)
                          _sessionFactory 
      = CreateSessionFactory();
                      
      return _sessionFactory;
                  }
              }

              
      protected static ISessionFactory CreateSessionFactory()
              {
                  
      return new Configuration()
                      .Configure()
                      .BuildSessionFactory();
              }
              
      //獲取當前Session
              public static ISession GetCurrentSession()
              {
                  var ht 
      = HttpContext.Current.Items[CurrentSessionKey] as System.Collections.Hashtable;
                  ISession currentSession 
      = ht[SessionFactory] as ISession;
                  
      if (currentSession == null)
                  {
                      currentSession 
      = SessionFactory.OpenSession();
                      //HttpContext.Current.Items[CurrentSessionKey] 
      = currentSession; //此處錯誤
                      //更正如下:
                      WebSessionContext.Bind(session);//還是用NHibernate自帶的方法將Session綁定到HttpContext.Current上.
                  }
                  
      return currentSession;
              }

              
      //關閉Session     
              public static void CloseSession()
              {
                  //var ht 
      = HttpContext.Current.Items[CurrentSessionKey] as System.Collections.Hashtable;
                  //ISession currentSession 
      = ht[SessionFactory] as ISession;
                  var session = WebSessionContext.Unbind(SessionFactory); //從HttpContext.Current中卸載Session,并返回當前Session
                  
      if (currentSession == null)
                  {
                      
      // No current session   
                      return;
                  }
                  currentSession.Close();
                  HttpContext.Current.Items.Remove(CurrentSessionKey);
              }
          }

       


       3.  在Global.ascx文件中給Global.asax加一個構造函數(照搬LYJ的,也可以直接在BeginRequest/EndRequst方法中寫,也可以用單獨的HttpModule來實現), 直接貼代碼:

      Global.asax
      public Global()
              {
                  BeginRequest 
      += (sender, args) =>
                  {
                      var session 
      = Data.NHinbernateSessionFactory.SessionFactory.OpenSession();
                      NHibernate.Context.WebSessionContext.Bind(session);
                      session.BeginTransaction();

                  };

                  EndRequest 
      += (sender, args) =>
                  {
                      ISession session 
      = NHibernate.Context.WebSessionContext.Unbind(
                          Data.NHinbernateSessionFactory.SessionFactory);
                      
      if (session != null)
                      {
                          
      if (session.Transaction != null &&
                              session.Transaction.IsActive)
                          {
                              session.Transaction.Commit();
                          }
                          session.Close();
                      }
                  };
              }

       

       4. 在Dao層的調用代碼:

      Dao層調用代碼
      public static ISession Session { get { return NHinbernateSessionFactory.CurrentSession; } }

              
      public IList<Domain.Entities.Customer> GetAllCustomer()
              {
                  
      return Session.CreateCriteria<Domain.Entities.Customer>()
                        .List
      <Domain.Entities.Customer>();
              }

       

      至此結束, 接下來貼張測試結果的圖:

       

      posted @ 2009-12-03 22:11  DukeCheng  閱讀(3240)  評論(30)    收藏  舉報
      主站蜘蛛池模板: 久久91精品牛牛| 影音先锋亚洲成aⅴ人在| 国产精品九九久久精品女同| 大胸美女被吃奶爽死视频| 亚洲av天堂综合网久久| 亚洲国产中文字幕精品| 一本色道久久东京热| 国产精品无遮挡猛进猛出| 国语自产精品视频在线看| 国产精品一区二区日韩精品| 97se综合| 国产精品va无码一区二区| 日日碰狠狠添天天爽| 日韩精品中文字幕一线不卡| 亚洲偷自拍国综合| 宅男噜噜噜66在线观看| 精品日韩色国产在线观看| 久久久精品国产精品久久| 久久羞羞色院精品全部免费| 国产第一页屁屁影院| 国产综合色一区二区三区| 国产精品二区中文字幕| 亚洲最大日韩精品一区| 日本无码欧美一区精品久久| 久久精品国产亚洲av忘忧草18| 国产精品综合色区av| 亚洲高清国产拍精品网络战| 中文字幕人妻精品在线| 伊人成人在线视频免费| 国产成人午夜福利在线播放| 国产亚洲一区二区三区av| 久久一日本道色综合久久| 国产熟女一区二区三区四区| 强奷漂亮人妻系列老师| 成人无码潮喷在线观看| 国产精品不卡一区二区在线| 黑人欧美一级在线视频| 又黄又爽又色的少妇毛片| 依依成人精品视频在线观看| 国产高清自产拍av在线| 国产高清在线不卡一区|