Dependency Injection (DI) helps you to produce code that is loosely coupled by separating the code's behavior from its dependencies, which leads to more readable code that is easy to test, extend, and maintain. Code is more readable because you follow the single responsibility principle. This also leads to much smaller code. Smaller code is easier to maintain and test, and because we rely upon abstractions instead of on implementations, we can extend the code more easily according to our needs.
The following are the types of DI that you can implement:
- Constructor injection
- Property/setter injection
- Method injection
Poor man's DI is composed without a container. However, the recommended and best practice is to use a DI container. In simple terms, a DI container is a registration framework that instantiates dependencies and injects them when requested.
We are now going to write...