依赖注入演示 [英] Dependency Injection for Presenter
问题描述
我有一个演示,需要一个服务和查看合同在其构造函数的参数:
I have a Presenter that takes a Service and a View Contract as parameters in its constructor:
public FooPresenter : IFooPresenter {
private IFooView view;
private readonly IFooService service;
public FooPresenter(IFooView view, IFooService service) {
this.view = view;
this.service = service;
}
}
我解决我的服务与Autofac:
I resolve my service with Autofac:
private ContainerProvider BuildDependencies() {
var builder = new ContainerBuilder();
builder.Register<FooService>().As<IFooService>().FactoryScoped();
return new ContainerProvider(builder.Build());
}
在我的ASPX页面(View实现):
In my ASPX page (View implementation):
public partial class Foo : Page, IFooView {
private FooPresenter presenter;
public Foo() {
// this is straightforward but not really ideal
// (IoCResolve is a holder for how I hit the container in global.asax)
this.presenter = new FooPresenter(this, IoCResolve<IFooService>());
// I would rather have an interface IFooPresenter so I can do
this.presenter = IoCResolve<IFooPresenter>();
// this allows me to add more services as needed without having to
// come back and manually update this constructor call here
}
}
的问题是FooPresenter的构造预期特定页面,而不是容器创建一个新的。
The issue is FooPresenter's constructor expects the specific Page, not for the container to create a new one.
我可以提供的观点,当前页面的特定实例,到容器只是这一决议?这是否有意义的事,或者我应该这样做另一种方式?
Can I supply a specific instance of the view, the current page, to the container for just this resolution? Does that make sense to do, or should I do this another way?
推荐答案
要解决通过什么我喜欢叫的方式数据解决Autofac依赖参数时是使用的生成的工厂。
The way to solve passing what I like to call data parameters when resolving dependencies in Autofac is by using generated factories.
(更新:的这个问题讨论同样的问题,我的文章。展示了如何避免大量的工厂代表)
(Update: this question discusses the same problem and my article shows how you can avoid large amounts of factory delegates).
解决您的问题将是这个样子:
The solution to your problem will look something like this:
首先声明一个工厂委托thath 仅的接受数据参数:
First, declare a factory delegate thath only accepts the data parameters:
public delegate IFooPresenter FooPresenterFactory(IFooView view);
您演示云不变:
public FooPresenter : IFooPresenter {
private IFooView view;
private readonly IFooService service;
public FooPresenter(IFooView view, IFooService service) {
this.view = view;
this.service = service;
}
}
下一步Autofac容器的设置:
Next the Autofac container setup:
var builder = new ContainerBuilder();
builder.Register<FooService>().As<IFooService>().FactoryScoped();
builder.Register<FooPresenter>().As<IFooPresenter>().FactoryScoped();
builder.RegisterGeneratedFactory<FooPresenterFactory>();
现在在你的页面,您可以在两行代码解决演示首先,把工厂,然后调用工厂做的分辨率为您提供:
Now in your page you can in two lines of code resolve the presenter by first getting the factory and then calling the factory to do the resolution for you:
public partial class Foo : Page, IFooView {
private FooPresenter presenter;
public Foo() {
var factory = IoCResolve<FooPresenterFactory>();
this.presenter = factory(this);
}
}
这篇关于依赖注入演示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!