Monday, October 28, 2019

Dependency Injection C# Example| Brief Introduction To Dependency Injection | Constructor Dependency Injection

Hello & As-salam u alikum ! , In this article we will learn about the most important and the confusing topic for the new developers and the topic is " Dependency Injection In C# "

What is Dependency Injection ? 

 It is a software design pattern that allows us to develop such application code which contain loosely coupled code.As a developer we only focus on developing features that result the correct desired output, but many of us do not think about future. If we need to make changes in future  in our application code, our application can get crashed if we did not follow any design pattern at the time of developing  application code . see the below example to understand with a humor.

The above picture represents your application when you didn't follow any design pattern while developing application . any small change can crash or destroyed your product so we need to follow design patterns to make our code loosely coupled.

Dependency Injection is one of the concept in design pattern which can  reduce the hard coded dependencies among your classes by injecting the dependencies at run time.

Types Of Dependency Injection :

  1. Constructor Injection
  2. Property Injection
  3. Method  Injection

In this article we will discuss the 1st type only, later we will discuss the remaining two.

Constructor Injection :

This is the most commonly used type of DI in Object Oriented Programming. It has normally one parametrized Constructor by removing the default constructor. In Constructor Injection we are forced to provide an specified value  in constructor at the time of object creation.

Code Implementation: 

For Implementing the Constructor injection 1st we need to make an interface ,which will contain  base methods which we implement in the implemented classes , In my case I have only one method print()
In the next step I have made a class to implement the interface . IText interface is implemented in model1 class. 

 similarly, I ll develop another class which will implement the same method with different output.
Now I need to make a class for constructor Injection and see the below code.

In the above code , I have make a class in which constructor I am providing Interface as parameter and in method base method is implemented.

from the above code ,every one will understand that at the time of object creation we just need to change the class for different implementation. model1 and model2 is implementing the IText Interface and both classes has different implementation of method "Print()" ,In future if we will ask to change the implementation ,so we will just change the class references by changing all code. that's easy and all for today. 


I hope you like this article, If really understood then please share this article for supporting my blog. and subscribe to my youtube channel. 
  

0 Comments:

Post a Comment

Do not Add Spam links in the Comment Box

Subscribe to Post Comments [Atom]

<< Home