Understanding Dependency Injection

Before getting into Dependency injection, we need to understand about IOC and DIP.

Both IOC and DIP are principles or you can think it as rules that are set to achieve loosely coupled design between two classes. These two classes can be thought of as class A called as "PARENT" and class B called as "CHILD". It can be thought of as two project or two software components.

These two classes should not depend of each other. I mean to say "PARENT" class should not depend on "CHILD" class to achieve something. Here depend means : inside "PARENT" class we should not create the object of "CHILD" class.

Ex:



Here in the above image you can see the "PARENT" class is creating "CHILD" class object to achieve something. Hence we can say that "PARENT" class is "DEPENDENT" on "CHILD" class or we can also that "CHILD" class is a dependency class of "PARENT" class.

So to make these two classes independent of each other, we use below two principles 

  1.     IOC - Inversion of Control
  2.     DIP - Dependency Inversion Principle
Inversion of Control - This principle says that we need to create the dependent object here in our case CHILD object outside of these two classes i.e. we need to create separate class ex: class C and inside class C, we should create the object of CHILD. In simple terms give the power/control of creating CHILD object to third party.




Dependency Inversion Principle - This principle says that high-level components here in our "PARENT" class should not dependent on low level components here in our case "CHILD" class. Rather parent class depending on child class to achieve something directly (directly means : creating child object), 

  1. Let child class functionalities be declared inside an interface and later let child class inherit this interface and define all the functionalities of an interface.
  2. Rest changes will be same
      


In the above code, there is a problem. what is the problem?
    - Problem is that if there is one more child class that has some other implementation of getting message from database and if I want use that implementation then the current implementation has an issue.

 I mean the problem is in Third-party class because it is returning fixed object i.e. child object. Now, if we want use new class implementation then we need to change Third-party class, Parent class.




So to solve the above problem, I mean to completely create the object [here 
Third-party class] from outside of the class or project, we use Dependency Injection pattern.
     
One of the technique of IOC and DIP is called Dependency Injection pattern. In this pattern, we will inject the dependency object using three ways 
  1. class constructor, 
  2. method or 
  3. property
Dependency Injection pattern :  Continue to above code
  1. Let parent class use this interface inside its constructor or in its method or in its property.
  2. In the Main or client class, create the object of parent class and inside constructor of parent class create child object

 all the three points said are in the above image.

Adding new class implementation is easy and required code change only in user class that's it. No other areas required the changes. This is the power of Dependency Injection pattern. 
if you in the below image, I have only changed the code in user class.
                        


IOC Container - Continuing to the above code, this is fine if the project is small and dependency object creation is less in number. But what if project is big and there are more number of dependency object creation. Then creating these objects manually is complex, time consuming and error prone. Hence IOC container will come into picture. 

All the dependency object creation is automatically for us from IOC containers. There are many containers in the market.
    Autofac,
    Unity,
    Ninject

But here in this we are taking unity container. We need to follow three steps in order build the object creation

    Register - Register the interface and its class 
    Resolve - To create object of service class or parent class that has an constructor injection in it
    Dispose - To tell what is the lifespan of dependency object

Install using "unity" nuget pacakge manager. Once it is intalled use it your project as shown below


Dependency object life span - These are three types in ASP.Net Core MVC.

  1. Singleton - For multiple request, the values or the object will be same. For next request, values/object will be same
  2. Scoped - For every single request-response cycle, the new object will be created. 
  3. Transition - For every time you call the service, new object is created. Ex: There is service called as "A". Now Method1 calls this service "A" then Method1 will get new object. At the same time, if Method2 calls this service "A" then Method2 will get new object.



Comments

Popular posts from this blog

Understanding Collection and Object Initializer

How to execute Javascript in VS code