Introduction
Dependency Injection is a design pattern used to fulfill the requirement of a class to achieve its goal or responsibility. (i.e. if a ClassX is using multiple other class objects inside, such as ClassA, ClassB, ClassC.) So, if we want to use ClassX, we need to have an instance of ClassA, ClassB, ClassC inside ClassX before initiating ClassX. ClassX has a dependency of ClassA, ClassB, ClassC. To resolve this dependency, there are multiple methods. In general, resolving (injecting) the dependency of a class is called Dependency injection.
We will try resolving dependency injection with 3 ways:
- Using Injection Constructor level
- Using MEF injection
- Using Unity injection
We will see these methods using examples.
Let’s have a ShowRoom class. This ShowRoom class has a dependency on Bike, a Car class dependency at the method GetBikeDemo and it uses GetCarDemo to perform its action. Below is the code of the Car and Bike classes:
- using static System.Console;
- namespace Sample.DependencyInjection.Business
- {
- public class Car
- {
- public void DriveDemo()
- {
- WriteLine("Car drive demo done");
- }
- }
- }
- using static System.Console;
- namespace Sample.DependencyInjection.Business
- {
- public class Bike
- {
- public void DriveDemo()
- {
- WriteLine("Bike drive demo done");
- }
- }
- }
Injection at the Constructor level
In this case, we will inject the dependency class objects at constructor so that whoever accessing the main class, while initializing they will come to know, they need to create an instance of some other class(es) to use the main class.
In the below example, the ShowRoom class has a dependency of Bike, a Car class dependency at the method GetBikeDemo, and a GetCarDemo respectively. We can see that at the ShowRoom constructor we are passing Bike and Car objects to perform GetBikeDemo and GetCarDemo method action. As we are passing dependency objects in the constructer level, it is called a constructor level injection.
Below is the code of the ShowRoom class:
- namespace Sample.DependencyInjection.Business
- {
- public class ShowRoom
- {
- private Car car;
- private Bike bike;
- /// <summary>
- /// This is the constructor level injection
- /// </summary>
- /// <param name="car"></param>
- /// <param name="bike"></param>
- public ShowRoom(Car car, Bike bike)
- {
- this.car = car;
- this.bike = bike;
- }
- public void GetCarDemo()=> car.DriveDemo();
- public void GetBikeDemo()=> bike.DriveDemo();
- }
- }
Unfortunately, in the future, if we have the requirement of adding a method GetBicycleDemo, we need to create class Bicycle off course. We need to pass the Bicycle class object in the constructor. Wherever we are creating an instance of ShowRoom class, we need to add the Bicycle object as a dependency.
We will use regression testing at all places we create an instance of ShowRoom class. This is not the best practice to do. In the same way, if we pass objects at the method level (i.e. as parameters of methods we call it Method level injection.)
Dependency injection can be also achieved by having getter setter property, and if initiate the class at setter level, it is called a Setter level injection. (i.e. after creating an instance of the main class and before accessing the dependent method we need to set the property.) Again, in both cases, we will have the drawback which we already discussed above.
To avoid this above problem, we have 2 solutions, 1 using the MEF framework and another using Unity Framework.
Now we will see dependency injection using the MEF framework.
MEF is a managed extensibility framework. It's a light-weight framework to build a plugin solution. At the base level, MEF will use reflection.
In MEF the class or interface, we want to add it as a dependency. Therefore, we need to mention the class/interface with an Export decorator. In our example, Car and Bike were dependency classes. I will mention the Export decorator to the classes as shown below for both Car and Bike.


faizal mohamedPosted Sep 20, 2024, 3:42 AM
Awesome explanation
Naveen HPosted Mar 20, 2020, 8:00 AM
Nice Article..
Karthik RPosted Mar 17, 2020, 11:44 AM
Best article about the mef vs unity thank you Nagaraj