dependency-injection Getting started with dependency-injection

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

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();

        this.eat(food);
    }
}
 

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

class Dog {
    public Dog(Food food) {
        this.eat(food);
    }
}
 


Got any dependency-injection Question?