we have a class with a dependency to the HttpContext
. We've implemented it like this:
我们有一个依赖于HttpContext的类。我们已经实现了这样:
public SiteVariation() : this(new HttpContextWrapper(HttpContext.Current))
{
}
public SiteVariation(HttpContextBase context)
{}
Now what i want to do is to instantiate the SiteVariation
class via Unity
, so we can create one constructor. But i do not know how to configure this new HttpContextWrapper(HttpContext.Current))
in Unity in the config way.
现在我想要做的是通过Unity实例化SiteVariation类,所以我们可以创建一个构造函数。但我不知道如何在配置方式中在Unity中配置这个新的HttpContextWrapper(HttpContext.Current)。
ps this is the config way we use
ps这是我们使用的配置方式
<type type="Web.SaveRequest.ISaveRequestHelper, Common" mapTo="Web.SaveRequest.SaveRequestHelper, Common" />
2 个解决方案
#1
11
I wouldn't take a dependency on HttpContextBase
directly. I would instead create a wrapper around it, and use the bits you need:
我不会直接依赖HttpContextBase。我会在它周围创建一个包装器,并使用你需要的位:
public interface IHttpContextBaseWrapper
{
HttpRequestBase Request {get;}
HttpResponseBase Response {get;}
//and anything else you need
}
then the implementation:
那么实施:
public class HttpContextBaseWrapper : IHttpContextBaseWrapper
{
public HttpRequestBase Request {get{return HttpContext.Current.Request;}}
public HttpResponseBase Response {get{return HttpContext.Current.Response;}}
//and anything else you need
}
This way, your class now just relies on a wrapper, and doesn't need the actual HttpContext to function. Makes it much easier to inject, and much easier to test:
这样,你的类现在只依赖于一个包装器,并且不需要实际的HttpContext来运行。使注入更容易,并且更容易测试:
public SiteVariation(IHttpContextBaseWrapper context)
{
}
var container = new UnityContainer();
container.RegisterType<IHttpContextBaseWrapper ,HttpContextBaseWrapper>();
#2
27
Microsoft has already built great wrappers and abstractions around HttpContext
, HttpRequest
and HttpResponse
that is included in .NET so I would definitely use those directly rather than wrapping it myself.
微软已经围绕HttpContext,HttpRequest和HttpResponse建立了很好的包装和抽象,因此我肯定会直接使用它们而不是自己包装它们。
You can configure Unity for HttpContextBase
by using InjectionFactory
, like this:
您可以使用InjectionFactory为HttpContextBase配置Unity,如下所示:
var container = new UnityContainer();
container.RegisterType<HttpContextBase>(new InjectionFactory(_ =>
new HttpContextWrapper(HttpContext.Current)));
Additionally, if you need HttpRequestBase
(which I tend to use the most) and HttpResponseBase
, you can register them like this:
另外,如果您需要HttpRequestBase(我倾向于使用最多)和HttpResponseBase,您可以像这样注册它们:
container.RegisterType<HttpRequestBase>(new InjectionFactory(_ =>
new HttpRequestWrapper(HttpContext.Current.Request)));
container.RegisterType<HttpResponseBase>(new InjectionFactory(_ =>
new HttpResponseWrapper(HttpContext.Current.Response)));
You can easily mock HttpContextBase
, HttpRequestBase
and HttpResponseBase
in unit tests without custom wrappers.
您可以在没有自定义包装器的单元测试中轻松模拟HttpContextBase,HttpRequestBase和HttpResponseBase。
#1
11
I wouldn't take a dependency on HttpContextBase
directly. I would instead create a wrapper around it, and use the bits you need:
我不会直接依赖HttpContextBase。我会在它周围创建一个包装器,并使用你需要的位:
public interface IHttpContextBaseWrapper
{
HttpRequestBase Request {get;}
HttpResponseBase Response {get;}
//and anything else you need
}
then the implementation:
那么实施:
public class HttpContextBaseWrapper : IHttpContextBaseWrapper
{
public HttpRequestBase Request {get{return HttpContext.Current.Request;}}
public HttpResponseBase Response {get{return HttpContext.Current.Response;}}
//and anything else you need
}
This way, your class now just relies on a wrapper, and doesn't need the actual HttpContext to function. Makes it much easier to inject, and much easier to test:
这样,你的类现在只依赖于一个包装器,并且不需要实际的HttpContext来运行。使注入更容易,并且更容易测试:
public SiteVariation(IHttpContextBaseWrapper context)
{
}
var container = new UnityContainer();
container.RegisterType<IHttpContextBaseWrapper ,HttpContextBaseWrapper>();
#2
27
Microsoft has already built great wrappers and abstractions around HttpContext
, HttpRequest
and HttpResponse
that is included in .NET so I would definitely use those directly rather than wrapping it myself.
微软已经围绕HttpContext,HttpRequest和HttpResponse建立了很好的包装和抽象,因此我肯定会直接使用它们而不是自己包装它们。
You can configure Unity for HttpContextBase
by using InjectionFactory
, like this:
您可以使用InjectionFactory为HttpContextBase配置Unity,如下所示:
var container = new UnityContainer();
container.RegisterType<HttpContextBase>(new InjectionFactory(_ =>
new HttpContextWrapper(HttpContext.Current)));
Additionally, if you need HttpRequestBase
(which I tend to use the most) and HttpResponseBase
, you can register them like this:
另外,如果您需要HttpRequestBase(我倾向于使用最多)和HttpResponseBase,您可以像这样注册它们:
container.RegisterType<HttpRequestBase>(new InjectionFactory(_ =>
new HttpRequestWrapper(HttpContext.Current.Request)));
container.RegisterType<HttpResponseBase>(new InjectionFactory(_ =>
new HttpResponseWrapper(HttpContext.Current.Response)));
You can easily mock HttpContextBase
, HttpRequestBase
and HttpResponseBase
in unit tests without custom wrappers.
您可以在没有自定义包装器的单元测试中轻松模拟HttpContextBase,HttpRequestBase和HttpResponseBase。