Dependency Injection

Dependency injection makes it easy to create loosely coupled components, which typically means that components consume functionality defined by interfaces without having any first-hand knowledge of which implementation classes are being used. It also results in components that are easier to isolate for unit testing.

Inversion of Control is a design concept that enables the creation of dependent objects to be inverted.

The Problem is tight coupling. Example, following is an Employee class which contains an instance of the Address class. Now, if the Address class changes, the Employee class also need to be recompiled as it contains a reference to the Address class.

Tight coupling example
// Tight coupling
// if the Address class changes, we have to recompile the Employee class

public class Employee
{
    private Address address;
    //...
}

public class Address
{
    public String Address1 { get; set; }
    public String Address2 { get; set; }
    public String Phone { get; set; }
    public String Zip { get; set; }
    public String City { get; set; }
    public String Country { get; set; }
}

The solution to this problem is IOC. We need to invert the control – just shift the creation of the instance of the Address class to some other class. Let’s say we have a Factory class that creates instances and returns them.

Factory class
// factory class
public class ObjectFactory
{
    public static Address GetAddress()
    {
        //...
        return new Address();
    }
}

The main principles driving IOC are that the classes aggregating other classes should not depend on direct implementation of the aggregated classes. Rather, they should depend on abstraction.

An abstraction can be injected using any of the following processes:
  • Constructor Injection
  • Service Locator Injection
  • Interface Injection

using constructor for injection
// using constructor for injection
public class Employee
{
    private Address _address;

    public Employee(Address address)
    {
        this._address = address;
    }

    //...
}

using service locator for injection
// using service locator
public class ContactsService
{
    private static IContact address = null;

    public static IContact GetAddress()
    {
        // code to locate the right Address object and return it
        return address;
    }
}
public class Employee
{
    private IContact address = ContactsService.GetAddressObject();
    //...
}

using an interface for injection
// using an Interface
public interface IContact
{
    //...
}

public class Employee
{
    private IContact _address;

    //...

    public void SetAddress(IContact address)
    {
        this._address = address;
    }
}

public class Address : IContact
{
    public String Address1 { get; set; }
    public String Address2 { get; set; }
    public String Phone { get; set; }
    public String Zip { get; set; }
    public String City { get; set; }
    public String Country { get; set; }
}



Ginger CMS
the future of cms, a simple and intuitive content management system ...

ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ...

CFTurbine
cf prototyping engine, generates boilerplate code and views ...

Search Engine LITE
create your own custom search engine for your web site ...

JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ...

Validation Library
complete validation library for your web forms ...