Question

I'm using Ninject to handle my dependencies.

My ApplicationPresenter is responsible to dispatch the user calls to features throughout the application, hence the need to inject multiple factories at once, thus the container itself. It handles an MDI GUI.

  1. Is there a better approach?
  2. Is it okay to make it so?

ApplicationPresenter

public class ApplicationPresenter 
    : Presenter<IApplicationView>
    , IApplicationPresenter {
    public ApplicationPresenter(IApplicationView view, IKernel dependencies) 
        : base(view) {
        Dependencies = dependencies;
        View.Connect += OnConnect;
        View.ManageInquiries += OnManageInquiries;
    }

    private static readonly IKernel Dependencies;
}

The ApplicationPresenter is responsible for orchestrating the system's feature in the MDI window such as:

  1. User authentication
  2. Managing inquiries...

I have an AuthenticationPresenter and an InquiryManagementPresenter which shall both be called from the ApplicationPresenter like so:

User clicks on...

public class ApplicationPresenter : ... { 
    // See ctor for event subscription...

    private void OnConnect() {
        AuthenticationPresenter authentication = 
            Dependencies.Get<IAuthenticationPresenter>();
        authentication.ShowView();
    }

    private void OnManageInquiries() {
        InquiriesManagementPresenter inquiriesManagement = 
            Dependencies.Get<IInquiriesManagementPresenter>();
        inquiriesManagement.ShowView();
    }
}

And within the InquiriesManagementPresenter, I have dependencies on other presenters as:

  • NewInquiryPresenter
  • EditInquiryPresenter
  • CancelInquiryPresenter
  • ApproveInquiryPresenter...

Ninject modules

public class ApplicationModule : NinjectModule {
    public void Load() {
        Bind<IApplicationPresenter>().To<ApplicationPresenter>().InSingletonScope();
        Bind<IApplicationView>().To<ApplicationForm>();
    }
}

public class AuthenticationModule : NinjectModule {
    public void Load() {
        Bind<IMembershipService>().To<MembershipService>();
        Bind<IAuthenticationPresenter>().To<AuthenticationPresenter>().InSingletonScope();
        Bind<IAuthenticationView>().To<AuthenticationForm>();
        Bind<ICredentials>().To<Credentials>().InSingletonScope();
        Bind<IUser>().To<User>().InSingletonScope();
        Bind<IDatabaseInstance>().To<DatabaseInstance>().InSingletonScope();
        Bind<IDatabaseUser>().To<DatabaseUser>().InSingletonScope();
    }
}

public class InquiryManagementModule : NinjectModule {
    public void Load() {
        Bind<INewInquiryPresenter>().To<NewInquiryPresenter>().InSingletonScope();
        Bind<INewInquiryView>().To<NewInquiryForm>();
        Bind<IEditInquiryPresenter>().To<EditInquiryPresenter>().InSingletonScope();
        Bind<IEditInquiryView>().To<EditInquiryForm>();
        Bind<IInquiryRepository>().To<InquiryRepository>();
        // ...
    }
}

So, I find it simpler to pass the Ninject's IKernel from from a presenter to another for those that require multiple functionality like the InquiryManagementPresenter or the ApplicationPresenter.

So still, is it this okay or is there another much better way to achieve this?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top