为什么像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;
}
}