[英]Register a Presenter with a View
如果我有这样的主持人-
public class LandingPresenter : ILandingPresenter
{
private ILandingView _view { get; set; }
private IProductService _productService { get; set; }
public LandingPresenter(ILandingView view, IProductService)
{
....
}
}
考虑到将不注册从属视图,我如何在Autofac中注册此Presenter(但将注册IProductService)
builder.RegisterType<LandingPresenter>().As<ILandingPresenter>(); ????
为什么也不要在容器中注册视图,将Autofac投入使用! 然后,您可以通过在演示者上使用构造函数注入并在视图上使用属性注入来自动挂钩演示者和视图。 您只需要通过属性连接来注册视图:
builder.RegisterAssemblyTypes(ThisAssembly).
Where(x => x.Name.EndsWith("View")).
PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies).
AsImplementedInterfaces();
主持人:
public class LandingPresenter : ILandingPresenter
{
private ILandingView _view;
private IProductService _productService { get; set; }
public LandingPresenter(ILandingView view, IProductService _productService)
{
....
}
}
视图:
public class LandingView : UserControl, ILandingView
{
// Constructor
public LandingView(... other dependencies here ...)
{
}
// This property will be set by Autofac
public ILandingPresenter Presenter { get; set; }
}
而且,如果您要先查看,那么您应该可以将其反转,以便演示者将视图作为属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.