如何在Unity中实现这个(HttpContext)依赖?

发布于 2024-12-13 06:25:16 字数 539 浏览 1 评论 0原文

我们有一个依赖于 HttpContext 的类。 我们是这样实现的:

public SiteVariation() : this(new HttpContextWrapper(HttpContext.Current))
{
}
public SiteVariation(HttpContextBase context)
{}

现在我想做的是通过 Unity 实例化 SiteVariation 类,这样我们就可以创建一个构造函数。 但我不知道如何在Unity中以配置方式配置这个新的HttpContextWrapper(HttpContext.Current))

附注 这是我们使用的配置方式

<type type="Web.SaveRequest.ISaveRequestHelper, Common" mapTo="Web.SaveRequest.SaveRequestHelper, Common" />

we have a class with a dependency to the HttpContext.
We've implemented it like this:

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.

ps
this is the config way we use

<type type="Web.SaveRequest.ISaveRequestHelper, Common" mapTo="Web.SaveRequest.SaveRequestHelper, Common" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-12-20 06:25:16

Microsoft 已经围绕 .NET 中包含的 HttpContextHttpRequestHttpResponse 构建了出色的包装器和抽象,因此我肯定会直接使用它们,而不是直接使用它们我自己包裹它。

您可以使用 InjectionFactoryHttpContextBase 配置 Unity,如下所示:

var container = new UnityContainer(); 

container.RegisterType<HttpContextBase>(new InjectionFactory(_ => 
    new HttpContextWrapper(HttpContext.Current)));

此外,如果您需要 HttpRequestBase (我最常使用它)并且HttpResponseBase,您可以像这样注册它们:

container.RegisterType<HttpRequestBase>(new InjectionFactory(_ => 
    new HttpRequestWrapper(HttpContext.Current.Request)));

container.RegisterType<HttpResponseBase>(new InjectionFactory(_ => 
    new HttpResponseWrapper(HttpContext.Current.Response)));

您可以在单元测试中轻松模拟 HttpContextBaseHttpRequestBaseHttpResponseBase,而无需定制包装。

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.

You can configure Unity for HttpContextBase by using InjectionFactory, like this:

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:

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.

祁梦 2024-12-20 06:25:16

我不会直接依赖 HttpContextBase 。相反,我会在它周围创建一个包装器,并使用您需要的位:

public interface IHttpContextBaseWrapper
{
   HttpRequestBase Request {get;}
   HttpResponseBase Response {get;}
   //and anything else you need
}

然后是实现:

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
}

这样,您的类现在只依赖于包装器,并且不需要实际的 HttpContext 来运行。使得注入更容易,测试也更容易:

public SiteVariation(IHttpContextBaseWrapper context)
{

}

var container = new UnityContainer();
container.RegisterType<IHttpContextBaseWrapper ,HttpContextBaseWrapper>();

I wouldn't take a dependency on HttpContextBase directly. I would instead create a wrapper around it, and use the bits you need:

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:

public SiteVariation(IHttpContextBaseWrapper context)
{

}

var container = new UnityContainer();
container.RegisterType<IHttpContextBaseWrapper ,HttpContextBaseWrapper>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文