Start a Project

Dependency Injection

Dependency Injection is a process where by object define their dependencies that are, the other objects they work with, by the constructor argument, arguments to factory method or property that set on the object instance after it is constructed or returned by the factory class, this process is called DI (Dependency Injection).

Why we use Dependency Injection?

The main idea of DI is to decouple the tight coupling for the dependent objects from each other. so that it is usually testable and modifiable.

The problem occurring when in future we have changed some objects instance from an object then code modifiability is too difficult because of we will change all the instances of that object from the application. For Ex.-

there are two Type services that send messages from the application so if we use without DI such as –

In future, if we want to delete or change the TypeA message to another TypeC message then we manually change all the TypeA object from the Application, so it will the very hard process if complex application.

So, How can solve?

We create MessageService Interface that has send message property and all the Services implement MessageService Interface.  and inject MessageService implementation class dependency from MessageApplication class via the setter method.

 

 

 

 

Exit mobile version