Dependency Injection and Service Location

Instead of the home brew Service Locator implementation we used earlier on project F, this time around we're going to leverage a full dependency injection framework, Ninject.

Generally, you'll be able to ignore this - just include the required classes as parameters to your presenter's constructor, and you'll receive the object you need.

In the rare instances where you need more, obtain an implementation of IServiceLocator through your constructor. This interface acts as a common point of entry for multiple dependency injection frameworks and allows you to leverage the framework without creating an explicit dependency.

public class SamplePresenter {
    public SamplePresenter( IServiceLocator serviceLocator) {
        mServiceLocator = serviceLocator;
    }
 
    public void AnAction()
    {
        var object = mServiceLocator.GetInstance<IMyService>();
        // Do something with object
    }
 
    private IServiceLocator mServiceLocator;
}

References