There are three different ways to achieve Dependency Injection, which are namely:
- Constructor Injection
- Method Injection
- Property Injection
Introduction
Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.
Property Injection: In the property injection (aka the Setter Injection), the injector supplies the dependency through a public property of the client class.
Method Injection: In this type of injection, the client class implements an interface that declares the method(s) to supply the dependency and the injector uses this interface to supply the dependency to the client class.
Dependency Injection is the way to achieve loose coupling. Our previous post is referenced here to explain it in detail. DI follows the architecture principle of Dependency Inversion or you can say Inversion of control as explained here.
As per official msdn:
Dependency Inversion Principle is a key part of building loosely coupled applications. In Dependency Inversion, implementation details can be written to depend on and implement higher-level abstractions, rather than the other way around. The resulting applications are more testable, modular, and maintainable.
DIP says:
- High-level modules should not depend on low-level modules. Both should depend on the abstraction.
- Abstractions should not depend on details. Details should depend on abstractions.
Now let’s explain the three different ways to achieve Dependency Injection.
Dependency Injection – Constructor Injection
Now following from my previous post last example where we explained the Constructor injection:
public class BusinessLayer
{
IEntity _dbContext;
public BusinessLayer(IEntity dbContext) {
_dbContext = dbContext;
}
public string GetCustomerName(int id) {
return _dbContext.GetDetailsByID(id);
}
}
public interface IEntity
{
string GetDetailsByID(int id);
}
public class DBLayer:IEntity
{
public string GetDetailsByID(int id) {
return "John Doe";
//To do connect to DBs
}
}
public class ServiceLayer
{
BusinessLayer _BL;
public ServiceLayer() {
_BL = new BusinessLayer(new DBLayer());
}
public string GetCustomerName(int id) {
//Writing same name in service and business layer reduces confusion
return _BL.GetCustomerName(id);
}
}
Dependency Injection – Property Injection
Property Injection: As the name suggest, here the dependency is injected via property.
Below the property _dbContext is made and the constructor is removed. The service layer will assign the new reference to the property as well. So before calling the method GetCustomerName we have a proper reference made.
public class BusinessLayer
{
public IEntity _dbContext {get; set;}
public string GetCustomerName(int id) {
return _dbContext.GetDetailsByID(id);
}
}
public interface IEntity
{
string GetDetailsByID(int id);
}
public class DBLayer:IEntity
{
public string GetDetailsByID(int id) {
return "John Doe";
//To do connect to DBs
}
}
public class ServiceLayer
{
BusinessLayer _BL;
public ServiceLayer() {
_BL = new BusinessLayer();
_BL._dbContext = new DBLayer();
}
public string GetCustomerName(int id) {
//Writing same name in service and business layer reduces confusion
return _BL.GetCustomerName(id);
}
}
Dependency Injection – Method Injection
Whereas Method injection is all about injecting the dependency via Method. Here we have to create an interface to set up the contract between the business layer and make it a mandatory method to implement.
Let’s call our interface IDbLayerDependency, which has a method AddDependency. This method will only set up the reference to the db context.
In our service layer, we will box it to the type of IDbLayerDependency to call AddDependency Method.
interface IDbLayerDependency
{
void AddDependency(IEntity dbContext);
}
public class BusinessLayer:IDbLayerDependency
{
IEntity _dbContext;
AddDependency(IEntity dbContext){
_dbContext = dbContext;
}
public string GetCustomerName(int id) {
return _dbContext.GetDetailsByID(id);
}
}
public interface IEntity
{
string GetDetailsByID(int id);
}
public class DBLayer:IEntity
{
public string GetDetailsByID(int id) {
return "John Doe";
//To do connect to DBs
}
}
public class ServiceLayer
{
BusinessLayer _BL;
public ServiceLayer() {
_BL = new BusinessLayer();
((IDbLayerDependency)_BL).AddDependency(new DBLayer());
}
public string GetCustomerName(int id) {
//Writing same name in service and business layer reduces confusion
return _BL.GetCustomerName(id);
}
}
We have learned three ways to inject dependencies, which makes our code easier to understand, Testable, and modular.
Happy Coding. 馃檪
Check out more content below:
Pingback: Thought Gem - Technology - Dependency Injection in C#
Everything is very open with a precise clarification of the challenges. It was definitely informative. Your site is useful. Thanks for sharing!