Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you use dependency injection?

I've been using StructureMap recently and have enjoyed the experience thoroughly. However, I can see how one can easily get carried away with interfacing everything out and end up with classes that take in a boatload of interfaces into their constructors. Even though that really isn't a huge problem when you're using a dependency injection framework, it still feels that there are certain properties that really don't need to be interfaced out just for the sake of interfacing them.

Where do you draw the line on what to interface out vs just adding a property to the class?

like image 637
Kevin Pang Avatar asked Aug 26 '08 15:08

Kevin Pang


People also ask

Why do we use dependency injection?

Dependency injection is a technique used in software engineering to develop loosely coupled systems. It is a technique that allows an object to receive other objects that it depends on by some other program than it explicitly calling the dependent object.

When should you use DI?

A DI box is primarily used to allow you to run a long cable from an instrument such as an electric guitar or bass guitar without adding noise or losing signal quality. High impedance signals tend to be much more prone to noise and keeping cable lengths under 8 meters (25 ft) is recommended.

Why we should not use dependency injection?

Basically, dependency injection makes some (usually but not always valid) assumptions about the nature of your objects. If those are wrong, DI may not be the best solution: First, most basically, DI assumes that tight coupling of object implementations is ALWAYS bad.

Which choice is benefit of using dependency injection?

A basic benefit of dependency injection is decreased coupling between classes and their dependencies. By removing a client's knowledge of how its dependencies are implemented, programs become more reusable, testable and maintainable.


1 Answers

The main problem with dependency injection is that, while it gives the appearance of a loosely coupled architecture, it really doesn't.

What you're really doing is moving that coupling from the compile time to the runtime, but still if class A needs some interface B to work, an instance of a class which implements interface B needs still to be provided.

Dependency injection should only be used for the parts of the application that need to be changed dynamically without recompiling the base code.

Uses that I've seen useful for an Inversion of Control pattern:

  • A plugin architecture. So by making the right entry points you can define the contract for the service that must be provided.
  • Workflow-like architecture. Where you can connect several components dynamically connecting the output of a component to the input of another one.
  • Per-client application. Let's say you have various clients which pays for a set of "features" of your project. By using dependency injection you can easily provide just the core components and some "added" components which provide just the features the client have paid.
  • Translation. Although this is not usually done for translation purposes, you can "inject" different language files as needed by the application. That includes RTL or LTR user interfaces as needed.
like image 93
Jorge Córdoba Avatar answered Dec 20 '22 09:12

Jorge Córdoba