a blog for those who code

Thursday, 26 January 2017

What is Dependency Injection and how to implement it in C#

In this post we will be discussing about Dependency Injection Pattern and how to implement Dependency Injection Pattern in C#. In our previous post we have discussed about List of Dependency Injection Container for .Net and in this post we will further discuss about implementing it in C#.

As you might know, Dependency Injection is a design pattern that demonstrates how to create loosely coupled class i.e. to reduce the coupling between classes and move the binding of abstraction and concrete implementation out of the dependent class. It helps us to better manage future changes and other complexity in our software.


The simplest example of Dependency Injection is to use Interface rather than the actual Class because if the implementation of the dependent class changes you need not have to worry about changing the logic in the calling class.

So as of now we came to know two things which Dependency Injection might need to come into affect, and they are :

1. High-Level modules should not be dependent on Low-Level Modules but should be dependent on Abstractions.
2. Abstraction should not be dependent on details but it should be other way round.

Dependency Injection can be done in three ways.

1. Constructor Injection
2. Method Injection
3. Property Injection

Constructor Injection - To do this kind of Dependency Injection we have to pass the dependency through the class's constructor.

Class CallingClass
{
  IDependentClass _idc = null;
  // Pass as constructor variable
  public CallingClass(IDependentClass idc)
  {
    this._idc = idc;
  }
}

In the above code, the constructor will take the class object and bind it to the interface handle.

Method Injection - To do this kind of dependency Injection we have to pass the class object on each invocation of the method of the dependent class which is actually invoking the action.

Class CallingClass
{
  IDependentClass _idc = null;
  // Pass as method variable
  public void myMethod(IDependentClass idc)
  {
    this._idc = idc;
  }
}

Property Injection - To do this kind of Dependency Injection we have to pass the class object via a setter property that was exposed by the dependent class.

Class CallingClass
{
  IDependentClass _idc = null;
  // Pass as method variable
  public IDependentClass DCProperty
  {
    get
    {
      return _idc;
    }
    set
    {
      _idc = value;
    } 
  }
}

The main advantage of Dependency Injection is that it enables us to better manage future changes and complexity in our software thus helps us to make our code maintainable. The three approaches (Constructor Injection, Method Injection and Property Injection) are OK if we have only one level of dependency, but think about big software where we have chained and nested dependencies which makes it quite complicated to implement the dependency injection. That is where we have to use DI Containers. These containers will help us to map the dependencies easily when we have chained or nested dependencies.

Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License