Reader Level:
Articles

Dependency Injection Pattern

By Jignesh Trivedi on January 11, 2012
There are three basic type of Dependency Injection: Construction Injection, Setter Injection and Interface based Injection.
  • 2
  • 0
  • 39667

When designing an object oriented application, a major concern of design is "Design must be loosely coupled", which means that objects should have only those dependencies which are required doing their task and it must be fewer. Object dependencies should be an interface not on "concrete" objects. Loose coupling offers us greater reusability, maintainability and testability.

There are three basic type of Dependency Injection

  1. Construction Injection
  2. Setter Injection
  3. Interface based Injection.

Constructor Injection

The basic idea of constructor-injection is that the object has no defaults or single constructor; instead specified values are required at the time of creation to instantiate the object. In other words Constructor injection uses parameters to inject dependencies.

Advantages

  • Construction Injection makes a strong dependency contract
  • Construction Injection supports testing, because dependencies can be passed in the constructor.
  • A dependency may be made immutable by making the dependency reference final by means that it prevents circular dependency.

Disadvantages

  • It requires up front wiring of the entire dependency graph.

The class that needs the Dependency must be exposing a public constructor that takes dependent class as constructor argument. In most cases, this should be the only one available constructor but if more than one Dependency is required then additional constructor arguments can be used.

Example

In this example "BusinessHelper" class's constructor takes an argument. Dependency inject from constructor argument.

public class CommonClass
{
    //Implement common property and method.
}

public class FirstObject : CommonClass
{
    public string GetData()
    {
        using (var helper = new BusinessHelper(this))
        {
            return helper.GetName();
        }
    }
}
public class BusinessHelper : IDisposable
{
    public BusinessHelper(CommonClass clsCommon) {}
    public string GetName()
    {
        return "Jignesh Test";
    }

    public void Dispose()
    {
        // Dispose your object that out of scoped
    }
}

Setter Injection

Setter Injection does not require the constructor to be change but dependencies are passed through public properties that are exposed. Setter Injection allows us costly resources or services to be created as late as possible and only when required.

Setter Injection should be used carefully in place of Constructor Injection, because it:

  1. In Setter Injection does not have clear idea which dependencies are required when?

  2. It is very difficult to trace down when exceptions occurs. Which means it can save on modifying a lot of legacy code when introducing a new method.

Advantages

  • It does not require up front wiring of entire dependency graph.

Disadvantage

  • It is very difficult to identify which dependencies are required.

Example

In this example "BusinessHelper" class's constructor does not accept any arguments. Dependency injection from the setting property is called "clsCommon".

public class CommonClass
{
    //Implement common property and method.
}

public class FirstObject : CommonClass
{
    public string GetData()
    {
        using (var helper = new BusinessHelper())
        {
            helper.clsCommon = this;
            return helper.GetName();
        }
    }
}
public class BusinessHelper : IDisposable
{
    public CommonClass clsCommon { get; set; }
    public BusinessHelper() { }
    public string GetName()
    {
        return "Jignesh Test";
    }

    public void Dispose()
    {
        // Dispose your object that out of scoped
    }
}

Interface Based Injection

Interface based Injection is implemented using common interfaces that other classes need to implement to inject dependencies. This type of injected from both the way either constructor injection or setter injection.

Benefits and drawbacks depend on which dependency injection is used with interface based injection.

Advantages of Dependency Injection pattern

The main advantage of DI is, it makes our code more reusable, maintainable, testable and readable.

Reference

http://www.martinfowler.com/articles/injection.html
http://msdn.microsoft.com/en-us/magazine/cc163739.aspx

COMMENT USING

Trending up