理论上人们会告诉你这是一种糟糕的商业行为。在实践中,我们只需要业务层中始终可用的会话级别的数据。:-(
我们最终将不同的存储引擎统一在一个小界面下。
public interface ISessionStorage
{
SomeSessionData Data {get;set;}
...
.. and most of the data we need stored at "session" level
}
//and a singleton to access it
public static ISessionStorage ISessionStorage;
这个接口几乎可以在我们代码的任何地方使用。
然后我们有一个 Session 和/或一个单例实现
public WebSessionStorage
{
public SomeSessionData Data
{
get { return HttpContext.Current.Session["somekey"] as SomeSessionData;}
set { HttpContext.Current.Session["somekey"] = value;}
}
public WebFormsSessionStorage
{
private static SomeSessionData _SomeSessionData; //this was before automatic get;set;
public SomeSessionData
{
get{ return _SomeSessionData;}
set{ _SomeSessionData=value; }
}
}
在启动应用程序时,该网站将执行
Framework.Storage.SessionStorage = new WebSessionStorage();
在 Global.asax 中,FormsApp 会做
Framework.Storage.SessionStorage = new WebFormsSessionStorage();