Implementation of Dependency Injection Pattern in C#
Nadim Attar
Expert Web Developer | Asp.net Core | React.js | Xamarin | Delivering Innovative Solutions @ First Screen
Dependency Injection (DI) is a method in software design that helps us create code that's not too closely connected. It's like separating different parts of your code, so they don't rely too much on each other. This helps you handle changes and make your code easier to maintain.
In Dependency Injection, we use a builder object to set up objects and give them the things they need to work. It's like giving an object what it needs to function from outside, instead of having it figure things out on its own.
In this article, I will explain three ways to implement DI:
1- Constructor Injection
1- This is a widely used way to implement DI.
2- Dependency Injection is done by supplying the DEPENDENCY through the class’s constructor when creating the instance of that class.
3- The injected component can be used anywhere within the class.
4- Recommended to use when the injected dependency, you are using across the class methods.
5- It addresses the most common scenario where a class requires one or more dependencies.
public interface IProduct{
void Buy();
}
public class Product1: IProduct {
public void Buy() {
Console.WriteLine("Product1 Called");
}
}
public class Product2: IProduct {
public void Buy() {
Console.WriteLine("Product2 Called");
}
}
public class ProductController{
private IProduct _product;
public ProductController(IProduct product) {
this._product = product;
}
public BuyMethod() {
this._product.Buy();
}
}
class Program
{
static void Main(string[] args)
{
//creating object
Product1 p1 = new Product1();
//passing dependency
ProductController PC= new ProductController(p1);
//TO DO:
PC.BuyMethod();
Product2 p2 = new Product2();
//passing dependency
PC = new ProductController(p2);
//TO DO:
PC.BuyMethod();
}
}
The Injection happens in the constructor, bypassing the Product that implements the IProduct Interface. The dependencies are assembled by a "Builder" and the Builder responsibilities are as follows:
1- Knowing the types of each IProduct
2- According to the request, feed the abstract IProduct to the ProductController
领英推荐
2- Property/Setter Injection
1- Recommended using when a class has optional dependencies, or where the implementations may need to be swapped.
2- Different logger implementations could be used in this way.
3- Does not require the creation of a new object or modifying the existing one. Without changing the object state, it could work.
public interface IProduct {
void Buy();
}
public class Product1 : IProduct{
public void Buy() {
Console.WriteLine("Product1 Called");
}
}
public class Product2 : IProduct {
public void Buy() {
Console.WriteLine("Product2 Called");
}
}
public class ProductController{
private IProduct _product;
public IProduct Product {
set { this._product = value; }
}
public BuyMethod() {
this._product.Buy();
}
}
class Program
{
static void Main(string[] args)
{
//creating object
Product1 p1 = new Product1();
ProductController PC = new ProductController();
PC.Product = p1; //passing dependency
//TO DO:
PC.BuyMethod();
Product2 p2 = new Product2();
PC.Product = p2; //passing dependency
//TO DO:
PC.BuyMethod();
}
}
3- Method Injection
1- Inject the dependency into a single method and generally for the use of that method.
2- It could be useful, where the whole class does not need the dependency, only one method having that dependency.
3- This is the way is rarely used.
public interface IProduct {
void Buy();
}
public class Product1 : IProduct {
public void Buy() {
Console.WriteLine("Product1 Called");
}
}
public class Product2 : IProduct {
public void Buy() {
Console.WriteLine("Product2 Called");
}
}
class Program
{
static void Main(string[] args)
{
//creating object
Product1 p1 = new Product1();
ProductController PC = new ProductController();
//TO DO:
PC.BuyMethod(p1);
Product2 p2 = new Product2();
PC.BuyMethod(p2);
}
}
Advantages of Dependency Injection
1- Reduces class coupling
2- Increases code reusability
3- Improves code maintainability
4- Make unit testing possible