Dependency Injection (DI) is a software design pattern that allow us to develop loosely coupled code. DI is a great way to reduce tight coupling between software components. DI also enables us to better manage future changes and other complexity in our software. The purpose of DI is to make code maintainable.
The Dependency Injection pattern uses a builder object to initialize objects and provide the required dependencies to the object means it allows you to "inject" a dependency from outside the class.
For example, Suppose your
The Dependency Injection pattern uses a builder object to initialize objects and provide the required dependencies to the object means it allows you to "inject" a dependency from outside the class.
For example, Suppose your
Client
class needs to use a Service
class component, then the best you can do is to make your Client
class aware of an IService
interface rather than a Service
class. In this way, you can change the implementation of the Service
class at any time (and for how many times you want) without breaking the host code.Key points about DI
- Reduces class coupling
- Increases code reusing
- Improves code maintainability
- Improves application testing
No comments:
Post a Comment