-
Notifications
You must be signed in to change notification settings - Fork 1
Dependency Injection Pattern
Inversion of Control (IOC) and Dependency Injection (DI) are used to remove dependencies of an application. This makes the system more decoupled and maintainable. Dependency Injection (DI) is a design pattern that demonstrates how to create loosely coupled classes. The Dependency Injection (DI) pattern uses a builder object to initialize objects and provide the required dependencies to the object, meaning that it allows developers to "inject" a dependency from outside the class. There are four ways of achieving the Dependency Injection.
Inversion of Control is a generic term. Rather than having the application call the methods in a framework, the framework calls implementations provided by the application. Dependency Injection is a form of Inversion of control, where implementations are passed into an object through constructors/setters/service lookups, which the object will 'depend' on to behave correctly. Dependency Injection frameworks are designed to make use of DI and can define interfaces to make it easy to pass in the implementations.
Dependency Injection (DI) helps class decoupling. Dependency Injection (DI) and Inversion of Control make it simple for a developer to manage dependencies between objects; that makes it easier fora developer to break coherent functionality off into its own contract. As a result, code become more modularized. It also increases reusability of the code and improves code maintainability and testing.
-
Your code can become harder to understand. Dependency Injection (DI) increases code complexity, usually by increasing the number of classes, which is not always beneficial. Generally, the benefit of decoupling makes each task simpler to read and understand, but increases the complexity of orchestrating the more complex tasks.
-
Dependency Injection (DI) takes a higher learning curve. To understand how a project uses dependency injection, a developer needs to understand both the dependency injection pattern and the specific framework.
-
Using a Dependency Injection framework, clients are dependent on the configuration data. This becomes an extra task for developers when the application does not need so many custom configuration values.
Dependency Injection continues to grow in popularity within the developer community. Inversion of Control (IOC) talks about who is going to initiate the call, whereas Dependency Injection (DI) talks about how one object acquires dependency on other object through abstraction. If you use Dependency Injection (DI), there are a number of styles to choose between. I would suggest you follow constructor injection.