Register a Presenter with a View

北慕城南 提交于 2019-12-10 11:27:03

问题


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)

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

回答1:


Why not register the views in the container as well, put Autofac to work! 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.



来源:https://stackoverflow.com/questions/13078640/register-a-presenter-with-a-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!