Dependency Injection
Dependency Injection mainly reduces the tight coupling between the classes. Dependency Injection moves the abstraction binding out of the class or higher level modules. By Dependency Injection, we can achieve the process of removing the dependency of low level modules from higher level modules. Dependency Injection can be achieved in three ways, which are shown below.
- Dependency Injection via Constructor Injection.
- Dependency Injection via Method Injection.
- Dependency Injection via Property Injection.
Dependency Injection via Constructor Injection
Problem
Lets have three low level classes or modules.
- //first Low level class
- class Sum
- {
- public void func(int a, int b)
- {
- //return a + b;
- Console.WriteLine(a + b);
- }
- }
- //second Low level class
- class Subtract
- {
- public void func(int a, int b)
- {
- Console.WriteLine(a - b);
- }
- }
- //third Low level class
- class Multiple
- {
- public void func(int a, int b)
- {
- Console.WriteLine(a * b);
- }
- }
Higher Level Module
- class HighlevelModule
- {
- //DependencyInterface Action = null;
- Sum s = null;
- public void funcWrite(int a, int b)
- {
- s.func(a, b); //here hig level module depends on low level module
- }
- // for calling sub or mul we have to create instances and dependency is there
- }
What problem can we have?
This design is against the DIP Dependency Inversion Principle. Class HighlevelModule depends on class Sum, which is a low level class. We can call “Subtract” and “mul” classes also like this, but we will use only one object at a time. Using this, we have to use all the objects at the same time, which we don’t want to use. For this, we have used the approach given below by Dependency Injection to achieve this.
- //Interface for dependency Injection
- interface IDependencyInterface
- {
- void func(int a, int b);
- }
- //first Low level class
- class Sum :IDependencyInterface
- {
- public void func(int a, int b)
- {
- //return a + b;
- Console.WriteLine(a + b);
- }
- }
- //second Low level class
- class Subtract : IDependencyInterface
- {
- public void func(int a, int b)
- {
- Console.WriteLine(a - b);
- }
- }
- //third Low level class
- class Multiple : IDependencyInterface
- {
- public void func(int a, int b)
- {
- Console.WriteLine(a * b);
- }
- }
- //high Level module
- class HighlevelModule
- {
- IDependencyInterface action = null;
- // Sum s = null;
- //Constructor
- //Dependency Injection via constructor
- public HighlevelModule(IDependencyInterface act)
- {
- this.action = act;
- }
- public void Actionfunc(int a, int b)
- {
- action.func(a, b);
- }
- }
We will call the method, which we require, as shown below.
- static void Main(string[] args)
- {
- Sum s = new Sum();
- HighlevelModule high = new HighlevelModule(s);
- s.func(6, 5);
- Console.Read();
- }
Hence, there is no dependency now. Higher level module is not dependent on low level ones. We have done the Constructor Injection, while we are injecting the dependency in constructor only.

In this type of injection, the dependent object is injected, using the method of the class. In Constructor Injection, the dependent class uses the same concrete class for its all life time and if we have to pass some separate concrete class on each invocation of the method, at this moment, we have to pass the dependency in method only and not in constructor.
In Method Injection, we pass the object of the concrete class into the method of the dependent class.
Here, we pass the object of the class, which we want to use, which may be Sum, Subtract or Multiple into the method of HighlevelModuleM class (method's name is Actionfunc).We have to pass the object of the concrete class and the parameters used.
The example of the Method Injection is given below.
- class MethodInjection
- {
- static void Main(string[] args)
- {
- Sum s = new Sum();
- HighlevelModuleM high = new HighlevelModuleM();
- high.Actionfunc(s, 5, 6);
- //s.Actionfunc
- //s.funct(6, 5);
- Console.Read();
- }
- }
- //high Level module
- class HighlevelModuleM
- {
- IDependencyInterface actionM = null;
- // Sum s = null;
- //Constructor
- //Dependency Injection via constructor
- //public HighlevelModule(IDependencyInterface act)
- //{
- // this.action = act;
- //}
- //Method Injection
- public void Actionfunc(IDependencyInterface act, int a, int b)
- {
- this.actionM = act;
- actionM.funct(a, b);
- }
- }
- interface IDependencyInterface
- {
- void funct(int a, int b);
- }
- class Sum : IDependencyInterface
- {
- public void funct(int a, int b)
- {
- //return a + b;
- Console.WriteLine(a + b);
- }
- }
- //second Low level class
- class Subtract : IDependencyInterface
- {
- public void funct(int a, int b)
- {
- Console.WriteLine(a - b);
- }
- }
- //third Low level class
- class Multiple : IDependencyInterface
- {
- public void funct(int a, int b)
- {
- Console.WriteLine(a * b);
- }
- }
- public void Actionfunc(IDependencyInterface act, int a, int b)
- {
- this.actionM = act;
- actionM.funct(a, b);
- }
- //Calling Or Intializing
- Sum s = new Sum();
- HighlevelModuleM high = new HighlevelModuleM();
- high.Actionfunc(s, 5, 6);

Property Injection
In a case, when both the concrete classes and the high level modules are in different places (modules), how can we achieve the Dependency Injection? This brings us to the part of Property Injection.
In Property Injection, we will have a setter property in the High Level Module/ Dependent class and we will pass the object of the concrete class via this setter property. In this type, the setter property of the dependent class will take the object of the concrete class and pass it to the Interface used by the class.
- class PropertyInjection
- {
- static void Main(string[] args)
- {
- SumP s = new SumP();
- HighlevelModuleP high = new HighlevelModuleP();
- high.ActionP = s;
- high.Actionfunc(3, 4);
- Console.Read();
- }
- }
- //high Level module
- class HighlevelModuleP
- {
- IDependencyInterfaceP actionP = null;
- //Propert Setter where this property will be set by Object of the class
- public IDependencyInterfaceP ActionP
- {
- get
- {
- return actionP;
- }
- set
- {
- actionP = value;
- }
- }
- //Method Injection
- public void Actionfunc(int a, int b)
- {
- actionP.functP(a, b);
- }
- }
- interface IDependencyInterfaceP
- {
- void functP(int a, int b);
- }
- class SumP : IDependencyInterfaceP
- {
- public void functP(int a, int b)
- {
- //return a + b;
- Console.WriteLine(a + b);
- }
- }
- //second Low level class
- class SubtractP : IDependencyInterfaceP
- {
- public void functP(int a, int b)
- {
- Console.WriteLine(a - b);
- }
- }
- //third Low level class
- class MultipleP : IDependencyInterfaceP
- {
- public void functP(int a, int b)
- {
- Console.WriteLine(a * b);
- }
- }
The output of this will be, as shown below.

IMAD AYOUBPosted May 26, 2020, 5:07 PM
Excellent Tutorial. Keep it up. Thanks a million.
Sridhar YelagandhulaPosted Nov 7, 2019, 7:30 AM
Very nice explanation!!!!!!.
Jerald James CapaoPosted Jan 16, 2019, 8:48 AM
Perfect! Thank you so much for this article. It helped me understand more about Dependency Injection in C#. Bless you
Rakesh reddyPosted Dec 2, 2017, 1:38 PM
It was very clear with simple example to get clear picture on Dependency Injection & it Types
Jasbeer SinghPosted Jul 13, 2017, 1:14 AM
Thanks saravana ..
saravana bharathiPosted Jul 4, 2017, 10:28 AM
Long time i was trying to know simple samples which helps to understand dependency injection concept. Really you made it very simple. Thanks for wonderful and very simple way to understand dependency injection... Keep doing such simple and easy understand manner in your upcoming blogs...
Jasbeer SinghPosted Jan 12, 2017, 11:40 AM
Thanks Mahesh for your comment
Mahesh AllePosted Jan 12, 2017, 6:36 AM
It is very simple to understand the dependency injection. Good one...
Jasbeer SinghPosted Dec 28, 2016, 1:18 AM
Thankyou for feedback Nisha..
Nisha VermaPosted Dec 28, 2016, 1:06 AM
Excellent..I have learned a lot
Jasbeer SinghPosted Dec 22, 2016, 12:08 AM
Thankyou neha...........................................
NehaPosted Dec 22, 2016, 12:08 AM
Nice One Jasbeer.....................
Jasbeer SinghPosted Dec 22, 2016, 12:03 AM
Thanks SubhashKumar Yadav
Subhashkumar YadavPosted Dec 22, 2016, 12:02 AM
Nicely Explained................