Back to: Design Patterns in C# With Real-Time Examples
Property and Method Dependency Injection in C#
In this article, I am going to discuss how to implement Property and Method Dependency Injection in C# with examples. Please read our previous article before proceeding to this article where we discussed Constructor Dependency Injection in C# with an example. We are also going to work with the same example that we created in our previous article. As part of this article, we are going to discuss the following pointers in detail.
- What is Property Dependency Injection in C#?
- Example using Property Dependency Injection.
- When to use Property Injection over Constructor Injection and vice versa?
- What is Method Dependency Injection in C#?
- Example using Method Dependency Injection.
- What are the advantages of using Dependency Injection?
What is Property Dependency Injection in C#?
In Property Dependency Injection, we need to supply the dependency object through a public property of the client class. Let us see an example to understand how we can implement the Property or you can say setter dependency injection in C#. Modify the EmployeeBL class as shown below.
namespace DependencyInjectionExample { public class EmployeeBL { private IEmployeeDAL employeeDAL; public IEmployeeDAL employeeDataObject { set { this.employeeDAL = value; } get { if (employeeDataObject == null) { throw new Exception("Employee is not initialized"); } else { return employeeDAL; } } } public List<Employee> GetAllEmployees() { return employeeDAL.SelectAllEmployees(); } } }
As you can see in the above example, we are injecting the dependency object through a public property of the EmployeeBL class. As we are setting the object through the setter property, we can call this Setter Dependency Injection in C#. Here we need to use the property EmployeeDataObject in order to access the instance of IEmployeeDAL.
Change the Main method of the Program class as shown below to inject the object through a property
namespace DependencyInjectionExample { class Program { static void Main(string[] args) { EmployeeBL employeeBL = new EmployeeBL(); employeeBL.employeeDataObject = new EmployeeDAL(); List<Employee> ListEmployee = employeeBL.GetAllEmployees(); foreach(Employee emp in ListEmployee) { Console.WriteLine("ID = {0}, Name = {1}, Department = {2}", emp.ID, emp.Name, emp.Department); } Console.ReadKey(); } } }
Now run the application and you will see the output as expected as shown below.
The Property or Setter Dependency Injection in C# does not require the constructor to be changed. Here the dependency objects are going to be passed through the public properties of the client class. We need to use the Setter or Property Dependency Injection when we want to create the dependency object as late as possible or we can say when it is required.
When to use Property Dependency Injection over Constructor Injection and vice versa?
The Constructor Dependency Injection in C# is the standard for dependency injection. It ensures that all the dependency objects are initialized before we are going to invoke any methods or properties of the dependency object, as a result, it avoids the null reference exceptions.
The Setter/Property Dependency Injection in C# is rarely used in real-time applications. For example, if I have a class that has several methods but those methods do not depend on any other objects. Now I need to create a new method within the same class but that new method now depends on another object. If we use the constructor dependency injection here, then we need to change all the existing constructor calls where we created this class object. This can be a very difficult task if the project is a big one. Hence, in such scenarios, the Setter or Property Dependency Injection can be a good choice.
What is Method Dependency Injection in C#?
In Method Dependency Injection, we need to supply the dependency object through a public method of the client class. Let us see an example to understand how we can implement the Method dependency injection in C#. Modify the EmployeeBL class as shown below
namespace DependencyInjectionExample { public class EmployeeBL { public IEmployeeDAL employeeDAL; public List<Employee> GetAllEmployees(IEmployeeDAL _employeeDAL) { employeeDAL = _employeeDAL; return employeeDAL.SelectAllEmployees(); } } }
Modify the Main method of the Program class as shown below
namespace DependencyInjectionExample { class Program { static void Main(string[] args) { //Create object of EmployeeBL class EmployeeBL employeeBL = new EmployeeBL(); //Call to GetAllEmployees method with proper object. List<Employee> ListEmployee = employeeBL.GetAllEmployees(new EmployeeDAL()); foreach (Employee emp in ListEmployee) { Console.WriteLine("ID = {0}, Name = {1}, Department = {2}", emp.ID, emp.Name, emp.Department); } Console.ReadKey(); } } }
Now run the application and see the output as expected as shown below
Note: We need to use the Method Dependency Injection in C# when the entire class does not depend on the dependency object but a single method of that class depends on the dependency object.
What are the advantages of using Dependency Injection in C#?
- The Dependency Injection Design Pattern allows us to develop loosely coupled software components.
- Using Dependency Injection, it is very easy to swap with a different implementation of a component, as long as the new component implements the interface type.
Dependency Injection Container:
There are a lot of Dependency Injection Containers are available in the market which implement the dependency injection design pattern. Some of the commonly used Dependency Injection Containers are as follows.
- Unity
- Castle Windsor
- StructureMap
- Spring.NET
In the next article, I am going to discuss how to implement Dependency Injection in ASP.NET MVC using the Unity Container. Here, in this article, I try to explain the Property and Method Dependency Injection in C# steps by step with an example. I hope you enjoy this article.
very nice article.
Thanks for the great tutorial
Very Good
Thanks !!
Very good
Thank !!
Many Thanks….
thanks a lot
such a very very nice explanation . first time i saw step by step explain all the things. and its a very easy language .
Very nice and clear article
That was awesome thanks a lot