Short explanation of DI and IoC

The acronyms DI and IoC stand for Dependency Injection and Inversion of Control. The first one is a development pattern for object orientated programming, whereas the other one is a characteristic of frameworks which are used to do Dependency Injection.

Dependency Injection faces the following problem:

public class CustomerManger
{
    public void CreateNewCustomer(String firstname, String lastName, String address)
    {
        Customer newCustomer = new Customer()
        {
            FirstName = firstname,
            LastName = lastName,
            Address = address
        };
 
        using (CustomerSqlDataLayer db = new CustomerSqlDataLayer())
        {
            db.Create(newCustomer);
        }
    }
}

On the surface, this code looks perfectly fine, but the class CustomerManger is tightly coupled to the class CustomerSqlDataLayer. If a customer wants an SQLite or Oracle DataLayer, you’ll need to add an if clause. Furthermore and even worse the CustomerManager can only be tested with the DataLayer and the Database.

Basically, the idea behind Dependency Injection is to inject dependencies like the CustomerSqlDataLayer. In code this means to add a contract with an interface, to inject via the constructor a class which implements this interface and work against this interface every time the dependency is needed.

public class CustomerManger
{
    private ICustomerDataService dataService;
 
    public CustomerManger(ICustomerDataService dataService)
    {
        this.dataService = dataService;
    }
                
    public void CreateNewCustomer(String firstname, String lastName, String address)
    {
        Customer newCustomer = new Customer()
        {
            FirstName = firstname,
            LastName = lastName,
            Address = address
        };
 
        dataService.Create(newCustomer);
    }
}

Now these two classes are working together without the tight coupling, instead they’re loosely coupled.

Inversion of Control frameworks or containers are there to do Dependency Injection. Famous containers are for instance Unity, Autofac or Castle Windsor.

Martin Fowler has written an even better explanation of DI and IoC and Scott Hanselman wrote a blog post about IoC containers for .NET

http://www.martinfowler.com/articles/injection.html
http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx

Advertisements
This entry was posted in C#, Development and tagged , , . Bookmark the permalink.

One Response to Short explanation of DI and IoC

  1. Pingback: Unity Application Block 3.0 QuickStart | .Kai Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s