Is this right to inject the container/kernel to the main application presenter? [duplicate]

旧巷老猫 提交于 2019-12-02 12:28:13

You should never pass the DI Container around, because then you're using it as a Service Locator, which is an anti-pattern.

If your ApplicationPresenter requires an AuthenticationPresenter and an InquiriesManagementPresenter, then inject those dependencies:

public class ApplicationPresenter : Presenter<IApplicationView>, IApplicationPresenter
{
    private readonly static AuthenticationPresenter authenticationPresenter;
    private readonly static InquiriesManagementPresenter inquiriesManagementPresenter;

    public ApplicationPresenter(
        IApplicationView view,
        AuthenticationPresenter authenticationPresenter,
        InquiriesManagementPresenter inquiriesManagementPresenter) 
        : base(view)
    {
        this.authenticationPresenter = authenticationPresenter;
        this.inquiriesManagementPresenter = inquiriesManagementPresenter;
        View.Connect += OnConnect;
        View.ManageInquiries += OnManageInquiries;
    }
}

If those presenters have dependencies of their own, it's totally fine: you just build up the entire graph up front, but the AplicationPresenter doesn't have to see any of the sub-graphs.

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