BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Dependency Injection

Dependency injection is a technique used in object-oriented programming languages. It promotes loose coupling of components by passing dependencies to an object, rather than having an object instantiate its own dependencies.

Example Setter Injection

We can now modify the code from the interface injection example to use setter injection instead. To do so, remove the IPaymentCalculatorInjecter interface and modify the PaymentTerms class as follows. The other types do not need to be changed. The new version of the class includes a write-only property that receives the dependency and stores it in the private field.

public class PaymentTerms
{
    IPaymentCalculator _calculator;

    public IPaymentCalculator Calculator
    {
        set { _calculator = value; }
    }

    public decimal Price { get; set; }
    public decimal Deposit { get; set; }
    public int Years { get; set; }

    public decimal GetMonthlyPayment()
    {
        return _calculator.GetMonthlyPayment(Price, Deposit, Years);
    }
}

Testing the Setter Injection

To test the setter injection, try executing the following code. This is identical to the previous test code except that the injection is handled by the property instead of a known method.

PaymentTerms pt = new PaymentTerms();
pt.Calculator = new PaymentCalculator();
pt.Price = 10000;
pt.Years = 4;
pt.Deposit = 2500;
Console.WriteLine(pt.GetMonthlyPayment());  // Outputs "239.58"

Implementing Constructor Injection

The final type of dependency injection that we will consider is constructor injection. As the name suggests, this uses parameters of a constructor of the dependent class to provide any dependencies. This guarantees that the dependencies will have been provided before any members are executed but also means that sometimes dependencies may be injected when they are not needed.

Constructor Injection

The above diagram shows an implementation of constructor injection. The items in the diagram are described below:

  • Dependent. This class represents the dependent class. The dependency is injected using the constructor and is stored in a private field.
  • IDependency. This interface describes the members provided by all dependencies that implement it.
  • Dependency. This class represents a dependency that may be injected into a dependent object.

The following shows the basic code for constructor injection implemented using C#.

public interface IDependency
{
    void SomeMethod();
}


public class Dependency : IDependency
{
    public void SomeMethod()
    {
        Console.WriteLine("Dependency.SomeMethod() called");
    }
}


public class Dependent
{
    IDependency _dependency;

    public Dependent(IDependency dependency)
    {
        _dependency = dependency;
    }

    public void CallDependency()
    {
        _dependency.SomeMethod();
    }
}

Example Constructor Injection

We will now modify the code from the setter injection example to use constructor injection. Modify the PaymentTerms class as follows, leaving the other types unchanged:

public class PaymentTerms
{
    IPaymentCalculator _calculator;

    public PaymentTerms(IPaymentCalculator calculator)
    {
        _calculator = calculator;
    }

    public decimal Price { get; set; }
    public decimal Deposit { get; set; }
    public int Years { get; set; }

    public decimal GetMonthlyPayment()
    {
        return _calculator.GetMonthlyPayment(Price, Deposit, Years);
    }
}

Testing the Constructor Injection

The test code is similar to the previous two examples. This time the payment calculator is provided as an argument of the constructor.

PaymentTerms pt = new PaymentTerms(new PaymentCalculator());
pt.Price = 10000;
pt.Years = 4;
pt.Deposit = 2500;
Console.WriteLine(pt.GetMonthlyPayment());  // Outputs "239.58"
12 September 2010