簡體   English   中英

向視圖注冊演示者

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM