Sep 13, 2018


The dependency injection pattern is based on the idea of inverting the control. Instead of creating hard dependencies and creating new objects either with the new keyword or lookups, you inject the needed resource into the destination object. This approach has many benefits:
  • The client does need not to be aware of the different implementations of the injected resources, making design changes easier.
  • Unit testing using mock objects is much easier to implement.
  • Configuration can be externalized, reducing the impact of changes.
  • A loosely coupled architecture allows pluggable structures.
The basic idea behind DI is to change the place where objects are created and to use an injector to inject the specific implementations to the destination objects at the right moment. This may sound like an implementation of the factory pattern, but the whole concept is much more than simple object creation. Inversion of Control (IoC) changes the whole wiring between objects and lets the injector do the work (most of the time magically). Instead of calling a factory to provide an implementation to the caller, the injector works proactively to determine when a destination object needs the target object and performs the injection in the appropriate way.