为什么Controller.Request和Controller.Session等属性不是虚拟的?
我敢打赌,如果情况确实如此,我会盯着我并道歉。
这是我所拥有的,我正在努力改进的地方。我的ControllerBase
允许我注入mock / stubs / fakes(无论如何),我知道这是一个弱设计,可能导致细微的错误(没有任何因为依赖关系使用访问器)。
public class ControllerBase : Controller
{
private readonly ModelStateDictionary modelStateDictionary;
private readonly HttpRequestBase request;
private readonly HttpSessionStateBase session;
public ControllerBase()
{
session = null;
request = null;
modelStateDictionary = null;
}
public ControllerBase(HttpSessionStateBase ssb, HttpRequestBase rq, ModelStateDictionary msd)
{
session = ssb;
request = rq;
modelStateDictionary = msd;
}
protected HttpSessionStateBase GetSession()
{
return session ?? Session;
}
protected HttpRequestBase GetRequest()
{
return request ?? Request;
}
protected ModelStateDictionary GetModelState()
{
return modelStateDictionary ?? ModelState;
}
}