假设业务层是一个单独的 DLL,我永远不会添加对对象的引用System.Web,因此我永远不会Session直接使用该对象。这将导致业务层和客户端(Web 或 Winforms)的公开接口的不同设计。
也就是说,作为一种快速解决方法,我建议在您的业务层中编写一个包装类,以隐藏Session您的代码中的对象。您的代码调用将是这样的:
((UserSession) DualContext.Current["UserSession"]).User.Info
并且包装器实现将是这样的(未完成和测试):
public class DualContext
{
private Dictionary<string, object> winFormsSession = new Dictionary<string, object>();
private static readonly DualContext instance = new DualContext();
public static DualContext Current
{
get { return instance; }
}
public object this[string key]
{
get
{
if (HttpContext.Current != null)
return HttpContext.Current.Session[key];
else
return winFormsSession[key];
}
set
{
if (HttpContext.Current != null)
HttpContext.Current.Session[key] = value;
else
winFormsSession[key] = value;
}
}
}