In this post, I want to share a quick answer to the most two favorite question of the interviewers:
- what can you tell me about the dependency injection (DI) concept introduce by spring years ago?
- what is inversion of control (IoC)?
It is not my goal to share an elaborate definition of DI or IoC, but to provide a two rows definition of each other.
Dependency Injection (DI)
Dependency of Injection is a software design pattern, introduced by Spring Framework years ago, and implements Inversion on Control (IoC). In other words it can be said that dependency injection is the way Spring chooses to provide its integration.
You have to know there are three types of DI (aka, three ways for a client to accept a dependency injection):
- interface injection
- setter injection
- constructor injection
The dependency injection exists with the goal to decouple objects so no client code has to be change when an object it depends changes.
The advantages of dependency injection:
- it is easy to switch the implementation
- it is easy to test your application
- allows AOP and discoverability.
Inversion of Control (IoC)
IoC is a design pattern by which the control of object is transferred to a container or framework.
Keep in mind
A dependency is an object that can be used.
An injection is the process of passing dependency to a dependent object that would use it.
DI and IoC are all about removing dependencies from your code.