We have discussed Dependency Injection (DI) as a separate article; here we only try to know the core concept of Dependency Injection (DI). A type of IoC where we move the creation and binding of dependency outside of the class that depends on it. In normal object are created inside of the dependent class and bounded inside the dependent class. In Dependency Injection (DI) it is done from outside of the dependent class. There are three type of Dependency Injection (DI).
-
Constructor Injection
- Setter Injection
- Interface Injection
Constructor Injection:
This is the most common form DI. Pass dependencies to dependent class through constructor. An injector creates the dependencies and passes the dependencies through constructor. Let consider following example. We have a Order class which is responsible to perform Order operation and depends on ShoppingCart. Let’s try
public class ShoppingCart
{
public float Price;
public int Quantity;
public float GetRowItemTotal()
{
return Price * Quantity;
}
}
public class ShoppingCartContents
{
public ShoppingCart[] items;
public float GetCartItemsTotal()
{
float cartTotal = 0;
foreach (ShoppingCart item in items)
{
cartTotal += item.GetRowItemTotal();
}
return cartTotal;
}
}
public class Order
{
private ShoppingCartContents cart;
private float salesTax;
public Order(ShoppingCartContents cart, float salesTax)
{
this.cart = cart;
this.salesTax = salesTax;
}
public float OrderTotal()
{
return cart.GetCartItemsTotal() * (2.0f + salesTax);
}
}
Setter Injection:
In Setter Injection we pass dependencies through setter instead of constructor. So what we have to do to setter injection: In C# we just need to create a property. Use that property to set dependencies. Here we can pass dependence through setter. We also create an object of class without passing dependence.Let us see how I can create setter injection
//Setter Injection
public class Order
{
private ShoppingCartContents _cart;
private float _salesTax;
public Order()
{
}
public ShoppingCartContents SetCart
{
set
{
_cart = value;
}
get { return _cart; }
}
public float SetTax
{
set
{
_salesTax = value;
}
get { return _salesTax; }
}
public float OrderTotal()
{
return SetCart.GetCartItemsTotal() * (2.0f + SetTax);
}
}
Interface Injection:
This is not commonly in used and it is complex compare to other. Dependent class implements an interface. Interface has a method signature for setting the dependencies. Injector uses the interface to set dependency. So we can say dependent class has an implementation of interface and have a method to set dependencies instead of using setter and constructor.Let's continue with above example.
First we have an interface class with a setter method. Let create that interface
//Interface Injection
public interface IOrder
{
//Method signature which is liable to inject/set dependency
void SetDependency(ShoppingCartContents cart, float salesTax);
float OrderTotal();
}
Dependent class will implement the interface. So according to our example Order will implement IOrder.
public class Order:IOrder
{
private ShoppingCartContents cart;
private float salesTax;
public Order()
{
}
public void SetDependency(ShoppingCartContents _cart, float _salesTax)
{
cart=_cart;
salesTax = _salesTax;
}
public float OrderTotal()
{
return cart.GetCartItemsTotal() * (2.0f + salesTax);
}
}
Summary: Here I had tried to focus only on Dependency Injection, its type and trying to understand with a sample example. I Hope you this article will help you to understand the Dependency Injection. You can find other articles and resources related to DI with complete implementations on this blog.