我正在向内部使用HttpContext.Current.Session的类添加一些测试,我们正在移植到ASP.NET MVC.我的班级看起来像这样:
class Foo { public void foo() { HttpContext.Current.Session["foo"] = "foo"; } }
我想改变它是这样的:
class Foo { IHttpSessionState session; public Foo() : this(HttpContext.Current.Session) {} public Foo(IHttpSessionState session) { m_session = session; } public void foo() { m_session["foo"] = "foo"; } }
问题在于默认构造函数.我无法传递旧类,因为它们没有实现新的ASP.NET MVC接口.
反正有没有在默认构造函数中获取实现IHttpSessionState的实例?
谢谢
试试这个,它使用了MVC框架使用的IHttpSessionState包装器.
class Foo { // member variable set by constructor IHttpSessionState m_session; // if no session is passed it attempts to create a new session state wrapper from the current session public Foo() : this(new HttpSessionStateWrapper(HttpContext.Current.Session)) {} public Foo(IHttpSessionState session) { m_session = session; } public void foo() { m_session["foo"] = "foo"; } }