简体   繁体   English

向视图注册演示者

[英]Register a Presenter with a View

If I have a presenter like this - 如果我有这样的主持人-

public class LandingPresenter : ILandingPresenter
{            
    private ILandingView _view { get; set; }
    private IProductService _productService { get; set; }

    public LandingPresenter(ILandingView view, IProductService)
    {
        ....
    }
}

How do I register this Presenter with Autofac considering the dependent view will not be registered (but IProductService will) 考虑到将不注册从属视图,我如何在Autofac中注册此Presenter(但将注册IProductService)

    builder.RegisterType<LandingPresenter>().As<ILandingPresenter>(); ????

Why not register the views in the container as well, put Autofac to work! 为什么也不要在容器中注册视图,将Autofac投入使用! Then you can hook up presenters and views automagically by using constructor injection on the presenters and property injection on the views. 然后,您可以通过在演示者上使用构造函数注入并在视图上使用属性注入来自动挂钩演示者和视图。 You just have to register the views with property-wiring: 您只需要通过属性连接来注册视图:

builder.RegisterAssemblyTypes(ThisAssembly).
    Where(x => x.Name.EndsWith("View")).
    PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies).
    AsImplementedInterfaces();

Presenter: 主持人:

public class LandingPresenter : ILandingPresenter
{            
    private ILandingView _view;
    private IProductService _productService { get; set; }

    public LandingPresenter(ILandingView view, IProductService _productService)
    {
        ....
    }
}

View: 视图:

public class LandingView : UserControl, ILandingView
{
    // Constructor

    public LandingView(... other dependencies here ...)
    {
    }

    // This property will be set by Autofac
    public ILandingPresenter Presenter { get; set; }
}

And if you want to go view-first then you should be able to reverse it so the presenters take the view as property instead. 而且,如果您要先查看,那么您应该可以将其反转,以便演示者将视图作为属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM