Dependency Injection | Service Locator

Inversion of Control

Problem

You have classes that have dependencies on services or components whose concrete type is specified at design time. In this example, ClassA has dependencies on ServiceA and ServiceB.
Dependencies.png
Figure 1
ClassA has dependencies on ServiceA and ServiceB

This situation has the following problems:
  • To replace or update the dependencies, you need to change your classes’ source code.
  • The concrete implementations of the dependencies have to be available at compile time.
  • Your classes are difficult to test in isolation because they have a direct reference to the dependencies. This means that these dependencies cannot be replaced with stubs or mocks.
  • Your classes contain repetitive code for creating, locating, and managing their dependencies.

Forces

  • You want to decouple your classes from their dependencies so that the dependencies can be replaced or updated with minimal or no changes to your classes’ source code.
  • You want to write classes that depend on services whose concrete implementations are not known at compile time.
  • You want to test your classes in isolation, without using the dependencies.
  • You want to decouple your classes from being responsible for locating and managing the lifetime of dependencies.

Solution

Delegate the function of selecting a concrete implementation type for the classes’ dependencies to an external component or source.

Implementation Details

The Inversion of Control pattern can be implemented in several ways. The Dependency Injection pattern and the Service Locator pattern are specialized versions of this pattern that delineate different implementations. Figure 2 illustrates the conceptual view of both patterns.
ServiceLocator-DependencyInjection.png
Figure 2
Conceptual view of the Service Locator and Dependency Injection patterns

For more information about these patterns, see Dependency Injection and Service Locator.

Liabilities

  • You need to implement a mechanism that provides the dependencies that are required by the object that is being initialized.
  • There is added complexity to the source code, which makes it harder to understand.

Related Patterns

  • Service Locator. The Service Locator pattern is a specialization of the Inversion of Control pattern. The Service Locator pattern introduces a locator object that objects use to resolve dependencies.
  • Dependency Injection. The Dependency Injection pattern is a specialization of the Inversion of Control pattern. The Dependency Injection pattern uses a builder object to initialize objects and provide the required dependencies to the object.
Dependency Injection | Service Locator

Last edited Jan 17, 2008 at 3:12 PM by siacomuzzi, version 5

Comments

No comments yet.