麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

《NHibernate One Session Per Request 簡單實現》勘誤

2019-11-17 03:49:50
字體:
來源:轉載
供稿:網友
使用 NHibernate 進行 Web 開發的朋友大多都知道 session-Per-Request 模式,但網上真正能夠正確使用的例子不多,網上包括園子里好多文章犯了同一個錯誤,而這個錯誤確一直在散播...

先來看園子里 Flyear 的一篇文章 《NHibernate One Session Per Request 簡單實現》。

首先對 NHibernate 進行配置沒有錯:

    <PRoperty name='current_session_context_class'>web</property>
錯誤在類 NHinbernateSessionFactory(類名都寫錯了) 中,NHinbernateSessionFactory.GetCurrentSession 不應包含對 HttpContext 的操作,GetCurrentSession 其實本應很簡單,請參見(類名改成 NHibernateHelper,簡短):

    public sealed class NHibernateHelper
    {
        public static readonly ISessionFactory SessionFactory;
        static NHibernateHelper()
        {
            SessionFactory = new Configuration()
                .Configure()
                .AddAssembly(/**/)
                .BuildSessionFactory();
        }
        public static ISession GetCurrentSession()
        {
            return SessionFactory.GetCurrentSession();
        }
    }
將 current_session_context_class 配置為 web,NHibernate 在初始化時會生成 NHibernate.Context.WebSessionContext 類的實例,WebSessionContext 類源碼如下:

    [Serializable]
    public class WebSessionContext : MapBasedSessionContext
    {
        // Fields
        private const string SessionFactoryMapKey = "NHibernate.Context.WebSessionContext.SessionFactoryMapKey";

        // Methods
        public WebSessionContext(ISessionFactoryImplementor factory) : base(factory)
        {  }

        protected override IDictionary GetMap()
        {
            return (HttpContext.Current.Items[SessionFactoryMapKey] as IDictionary);
        }

        protected override void SetMap(IDictionary value)
        {
            HttpContext.Current.Items[SessionFactoryMapKey] = value;
        }
    }
WebSessionContext 實現了 Session-Per-Request 模式,它封裝了 HttpContext ,因此我們不需要在我們的輔助類(NHibernateSessionFactory 或是 NHibernateHelper)中再對 HttpContext 進行操作。

我們只需要從 WebSessionContext 的實例中獲取 Session 即可。從WebSessionContext 類中獲取當前 ISession 相當簡單,因為 WebSessionContext 實現了 ICurrentSessionContext 接口:

    public interface ICurrentSessionContext
    {
        ISession CurrentSession();
    }


NHibernate.Context 命名空間中的類和接口

(說明:current_session_context_class 還可以配置為 Managed_web、Call、thread_static,分別對應類 ManagedWebSessionContext、CallSessionContext、ThreadStaticSessionContext)

在實際使用中我們并不需要直接調用 WebSessionContext 的 CurrentSession() 方法,因為 ISessionFactory 提供了一個更簡單的方法讓我們能一步獲取到 Session:

    public interface ISessionFactory : IDisposable
    {
        ISession GetCurrentSession();
        //......
    }


下面探討一下 ISessionFactory.GetCurrentSession 方法的具體實現:

Configuration.BuildSessionFactory 方法實際返回的是 SessionFactoryImpl 類的實例,讓我們簡單看一下 SessionFactoryImpl 的部分代碼吧:

1     public sealed class SessionFactoryImpl
2         : ISessionFactoryImplementor, IMapping, ISessionFactory, IDisposable, IObjectReference
3     {
4         private readonly ICurrentSessionContext currentSessionContext;
5
6         public ISession GetCurrentSession()
7         {
8             if (this.currentSessionContext == null)
9             {
10                 throw new HibernateException(
11                     "No CurrentSessionContext configured (set the property current_session_context_class)!");
12             }
13             return this.currentSessionContext.CurrentSession();
14         }
15
16         public ICurrentSessionContext CurrentSessionContext
17         {
18             get { return this.currentSessionContext; }
19         }
20
21         private ICurrentSessionContext BuildCurrentSessionContext()
22         {
23             string name = PropertiesHelper.GetString("current_session_context_class", this.properties, null);
24             string str2 = name;
25             if (str2 != null)
26             {
27                 if (str2 == "call") return new CallSessionContext(this);
28                 if (str2 == "thread_static") return new ThreadStaticSessionContext(this);
29                 if (str2 == "web") return new WebSessionContext(this);
30                 if (str2 == "managed_web") return new ManagedWebSessionContext(this);
31             }
32             else
33                 return null;
34             try
35             {
36                 Type type = ReflectHelper.ClassForName(name);
37                 return (ICurrentSessionContext)Environment.BytecodeProvider.ObjectsFactory
38                     .CreateInstance(type, new object[] { this });
39             }
40             catch (Exception exception)
41             {
42                 log.Error("Unable to construct current session context [" + name + "]", exception);
43                 return null;
44             }
45         }
46     //......
47     }

SessionFactoryImpl 在實例化時會調用 BuildCurrentSessionContext() 方法(行21)為 currentSessionContext 字段賦值,具體值有配置文件中的 current_session_context_class 決定。

ISessionFactory 的 GetCurrentSession()  調用的是 ICurrentSessionContext 的 CurrentSession()(行6) 方法。

上面的代碼相當簡單,想必大家都已經明明白白了。



《NHibernate One Session Per Request 簡單實現》 一文中還有兩處不太恰當的地方:

1. 并不是每一次請求都需要一個 Session 來訪問數據庫。文中 Global.asax 的代碼給所有的請求在開始的時候都進行WebSessionContext.Bind(),會照成很多 Session 的浪費,雖然 NHibernate 的 Session 是輕量級的,較為合理的做法是在“真正需要”時綁定。

2. 因為 WebSessionContext.Unbind 方法需要一個 ISessionFactory 接口的實例,迫使用我們的輔助類(NHibernateSessionFactory 或是 NHibernateHelper)公開 SessionFactory。

第一個問題比較容易解決,留在回復中和大家交流吧。

第二個問題我會在下一篇隨筆中解答。



本人學習 NHibernate 時間也不長,屬于新手,如有錯誤,歡迎指正!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美一区在线观看视频 | 毛片免费视频播放 | 史上最强炼体老祖动漫在线观看 | 国产精品免费看 | 日本特级a一片免费观看 | 欧美一级免费在线观看 | 久久久国产精品电影 | 国产免费久久久久 | 亚洲午夜1000理论片aa | 黄色99视频| 中文字幕h | 成年性羞羞视频免费观看无限 | av影院在线| 久久久一区二区三区四区 | 成年性羞羞视频免费观看无限 | 久久久久久久免费精品 | 久久99精品久久久久久久久久久久 | 日韩在线播放一区二区 | 久久99亚洲精品久久99果 | 久久99精品久久久久久秒播蜜臀 | 久草在线手机视频 | 国产精品视频一区二区三区四区五区 | 中文字幕一区2区 | 有兽焉免费动画 | 黄色大片www | 视频在线亚洲 | 久久国产精品电影 | 美女网站黄在线观看 | 成年人精品视频 | 久草视频免费 | 激情久久一区二区 | 国产一级性生活视频 | 日本黄色免费观看视频 | 欧美18videos性处按摩 | 综合网天天色 | 久久久免费电影 | 精精国产xxxx视频在线野外 | 欧美乱淫| 日韩黄色影视 | 成人富二代短视频 | 999久久久|