Getting started with dependency-injection


In object-oriented programming, objects often depend on other objects in order to do things.

Dependency Injection (DI) is giving an object the things that it depends on so that it doesn’t have to worry about getting them itself. That is, the dependencies are injected into the object. This is most often done with constructor injection or property injection.

Dependency injection is a form of Inversion of Control (IoC). IoC is a broader term that describes a pattern of software design.

In traditional procedural programming, the flow of control follows logically in steps. The control is in the hands of the object or function performing operations. Step-by-step the program performs a series of operations that it controls explicitly.

Instead of the object or function detailing every step, the flow of control can be inverted by making the operations be performed by more generic and abstract objects - usually a framework that is broader in scope.

What is a basic example of dependency injection?

Here is a class (Dog) creating its own dependency (Food):

class Dog {
    public Dog() {
        var food = new Food();;

Here is the same class being injected with its dependency using constructor injection:

class Dog {
    public Dog(Food food) {;

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow