我有一个 Presenter,它在其构造函数中将 Service 和 View Contract 作为参数:
public FooPresenter : IFooPresenter {
private IFooView view;
private readonly IFooService service;
public FooPresenter(IFooView view, IFooService service) {
this.view = view;
this.service = service;
}
}
我用 Autofac 解决了我的服务:
private ContainerProvider BuildDependencies() {
var builder = new ContainerBuilder();
builder.Register<FooService>().As<IFooService>().FactoryScoped();
return new ContainerProvider(builder.Build());
}
在我的 ASPX 页面(查看实现)中:
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 的构造函数需要特定的页面,而不是容器创建新页面。
我可以为容器提供视图的特定实例(当前页面)以实现此分辨率吗?这样做有意义吗,还是我应该以另一种方式这样做?