获得BaseController /集的HttpContext会话方法VS惩戒HttpContextBase创建get / set方法 [英] Get/Set HttpContext Session Methods in BaseController vs Mocking HttpContextBase to create Get/Set methods
问题描述
我创建的get / set HttpContext的会话方法对BaseController类也戏弄HttpContextBase和创建get / set方法。
这是使用它的最佳方式。
HomeController的:BaseController
{
VAR值1 = GetDataFromSession(KEY1)
SetDataInSession(KEY2(对象)key2Value); 要么 VAR值2 = SessionWrapper.GetFromSession(KEY3);
GetFromSession.SetDataInSession(KEY4(对象)key4Value);
}
公共类BaseController:控制器
{
公共ŧGetDataFromSession< T>(字符串键)
{
回报(T)HttpContext.Session [关键]
} 公共无效SetDataInSession(字符串键,对象的值)
{
HttpContext.Session [关键] =价值;
}
}
或者
公共类BaseController:控制器
{
公共ISessionWrapper SessionWrapper {搞定;组; } 公共BaseController()
{
SessionWrapper =新HttpContextSessionWrapper();
}
} 公共接口ISessionWrapper
{
ŧGetFromSession< T>(字符串键);
无效SetInSession(字符串键,对象的值);
} 公共类HttpContextSessionWrapper:ISessionWrapper
{
公共ŧGetFromSession< T>(字符串键)
{
回报(T)HttpContext.Current.Session [关键]
} 公共无效SetInSession(字符串键,对象的值)
{
HttpContext.Current.Session [关键] =价值;
}
}
第二个似乎是最好的。虽然我可能会写这两个作为扩展方法的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpsessionstatebase.aspx\">HttpSessionStateBase而不是把它们放入一个基本的控制器。像这样的:
公共静态类SessionExtensions
{
公共静态ŧGetDataFromSession&LT; T&GT;(这HttpSessionStateBase会议,串键)
{
回报(T)会话[关键]
} 公共静态无效SetDataInSession&LT; T&GT;(这HttpSessionStateBase会议,串键,对象的值)
{
会话[关键] =价值;
}
}
,然后控制器或助手,或者一些具有 HttpSessionStateBase
的一个实例,使用它里面的:
公众的ActionResult指数()
{
Session.SetDataInSession(KEY1,值1);
字符串值= Session.GetDataFromSession&LT;串GT(KEY1);
...
}
由框架提供的 HttpSessionStateBase
写作会话包装是在ASP.NET MVC无用已经可能在单元测试中轻松嘲笑的抽象类。
I created Get/Set HttpContext Session Methods in BaseController class and also Mocked HttpContextBase and created Get/Set methods.
Which is the best way to use it.
HomeController : BaseController
{
var value1 = GetDataFromSession("key1")
SetDataInSession("key2",(object)"key2Value");
Or
var value2 = SessionWrapper.GetFromSession("key3");
GetFromSession.SetDataInSession("key4",(object)"key4Value");
}
public class BaseController : Controller
{
public T GetDataFromSession<T>(string key)
{
return (T) HttpContext.Session[key];
}
public void SetDataInSession(string key, object value)
{
HttpContext.Session[key] = value;
}
}
Or
public class BaseController : Controller
{
public ISessionWrapper SessionWrapper { get; set; }
public BaseController()
{
SessionWrapper = new HttpContextSessionWrapper();
}
}
public interface ISessionWrapper
{
T GetFromSession<T>(string key);
void SetInSession(string key, object value);
}
public class HttpContextSessionWrapper : ISessionWrapper
{
public T GetFromSession<T>(string key)
{
return (T) HttpContext.Current.Session[key];
}
public void SetInSession(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
}
The second one seems the best. Although I would probably write those two as extension methods to the HttpSessionStateBase instead of putting them into a base controller. Like this:
public static class SessionExtensions
{
public static T GetDataFromSession<T>(this HttpSessionStateBase session, string key)
{
return (T)session[key];
}
public static void SetDataInSession<T>(this HttpSessionStateBase session, string key, object value)
{
session[key] = value;
}
}
and then inside the controllers, or helpers, or something that has an instance of HttpSessionStateBase
use it:
public ActionResult Index()
{
Session.SetDataInSession("key1", "value1");
string value = Session.GetDataFromSession<string>("key1");
...
}
Writing session wrappers is useless in ASP.NET MVC as the HttpSessionStateBase
provided by the framework is already an abstract class which could be easily mocked in unit tests.
这篇关于获得BaseController /集的HttpContext会话方法VS惩戒HttpContextBase创建get / set方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!