The Dependency Injection is a pattern that enforces a class to have it's required dependencies.This can be acheived by decouling the usage of an object from it's creation.This design pattern follows dependency inversion and single responsibility SOLID principles.


We can use interfaces to break the dependencies between higher and lower level classes


If we introduce interfaces to break the dependencies between higher and lower level classes then both classes depend on the interface and not on eachother.The interface decouples the usage of the lower level class but not on its instantiation.

Four main roles in dependency injection pattern:

Role 1: The service you want to use.

Role 2: The client that uses the service.

Role 3: The interface that is used by the client and implemented by the service.

Role 4: The injector which creates a service instance and injects it into the client.

There are three types of dependency_injection:
The constructor injection
The field Injection (This one exists but is not very practical to use as it might introduce different problems)
The setter(This one is not used as it might also be problematic at runtime)

As a General guideline: We are supposed to rely on dependency injection when we start introducing complexity of our code instead of simplifying it.