Dependency Injection(DI)
- What is Dependency Injection(DI)
- Type of DI
- Constructor DI
- Method DI
- Property DI
- Pain point without DI
- Example and Demo
- What is DI container?
- Example and Demo of DI
- why to use DI?
What is Dependency Injection?
- Dependency Injection (DI) is a software design pattern that allows us to develop loosely coupled code.
- It is a process of supplying the required resources to the code which requires these resources.
- DI allow developers to build loosely coupled components as we make our components depended upon the Interfaces rather than depending on the actual implementation.
- It is instead of using direct & hard-coded instances we inject dependencies to the components & make them aware that they will need injected dependency to serve the purpose.
- DI also enables us to better mange future changes and other complexity in our software.
- 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 call.
Types of Dependency Injection(DI):
Example without DI:
Here we have two services ServiceA and ServiceB both having execute method.
logic can be anything.
Service A and service B getting called by Consumer Class.
Now for that, we generally do. We will create in the constructor so that we can use it across the class and then you have a execute() just ignore execute logic might be some execute logic which you want to implement for consumer class.
Actually Consumer class trying to consume the two services A and services B and this is basically your client. So Consumer is basically your client class and now that client class you want to use it any programmer at anywhere in your website then that time you need to create Consumer class.
Here you need to create Consumer class object and you will call that execute method now you are not this class where you are implementing this or you are consuming the consumer class there you were not aware that what is its dependency. If you see the consumer classes depend on service A and Service B so that is the dependency but you are not passing any dependency here if there is any issue you will not be doing here. So this is how you implement your code without DI.
Now let's say how it can be changed in to DI where you use the DI
Constructor DI:
Now it will be same service A but if you see we have a inherited one interface IService here also service B we are inherited interface IService and you will create a IService interface and it will have a execute method which will be the actual implementation will be written in to the service classes so this is how you will change the service class and now how the client class will be change.
Here your consumer class instead you are pass creating service A or serviceB object you will pass the interface.
Basically you inserting the dependency that you are passing IService here and then you write your executor logic in the execute method. Now if you want to consume this class like you want to use the consumer class or you want to use this consumer class is basically your client class so this is service A and Service B are Services and ConsumerDIC is your client class. and now that client you want to use it in your application I want to use the execute method so what im doing it creating a ServiceA object so Im injecting that dependency or passing that object to client like ConsumerDIC so this is basically injecting the dependency. Dependency you are passing this value here this object here that is basically you are injecting your dependency to this ConsumerDIC Class then you are executing the execute method now you know that for this here where you implementing it you know that consumerDIC is depend on ServiceA so whenever your writing a test also you will understand that this is depend on services so this how you can use DI in simple way. -- this Constructor DI why because the dependency Injection you are passing here..
Method DI:
Method DI having same service method but In client class there is no constructor for adding dependencies so Method DI having addition method in client class for adding the dependencies..
Property DI:
Property DI having same service class but In client class we are setting the property value and assigning Service A obj to method then it will set as a property value.
These are three different ways we can do it.
What is DI Container?
Comments
Post a Comment