Friday, September 12, 2014

Dependency Injection—an Inversion of Control technique



Dependency Injection—an Inversion of Control technique

Dependency injection is a software design pattern that implements inversion of control. An injection is the passing of a dependency (a service) to a dependent object (a client). The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.

Advantages

  • Clients are more independent and easier to unit test in isolation using stubs or mock. This ease of testing is often the first benefit noticed when using dependency injection.
  • Dependency injection allows a client to remove all knowledge of a concrete implementation that it needs to use.
  • Dependency injection can be used to externalize a system's configuration details into configuration files allowing the system to be reconfigured without recompilation.
  • Dependency injection allows concurrent or independent development. Plugins are often developed by third party shops that never even talk to the developers who created the product that uses the plugins.

Disadvantages

  • Dependency injection diminishes encapsulation by requiring users of a system to know how it works and not merely what it does.
  • Dependency injection increases coupling by requiring the user of a subsystem to provide for the needs of that subsystem.
  • Dependency injection can make code difficult to trace (read) because it separates behavior from construction.
  • Dependency injection typically requires more lines of code to accomplish the same behavior legacy code would.

No comments:

Post a Comment