Friday, July 3, 2009

Dependency Injection

Inspired of rise of Dependency Injection (DI), I try to explain this concept in the simple way in this post and hope you understand it. DI now become very popular pattern in software development, both Java and .Net, you can find a lot of articles talking about, by putting the word "dependency injection" on google textbox, you will see. This pattern is invented by Martin Flower and here is his orginal article:

So, let's first talk dependency between two classes. As my thinking, we just have two kind of dependency:

1. Loose dependency. A class has dependency to interface of other class. In this below example, CustomerService have dependency to ICustomerRepository.

2. Tight dependency. Class A is created inside class B, or class B has creation dependency to class A:

So, the purpose is to turn from tight dependency to loose dependency between two classes, we need to follow the rule: the instant of a class need creating outside of other class. It is also called INVERSION OF CONTROL (IoC).

Some of you might concern what is the difference between IoC and DI? You can imagine that IoC is a principle and DI is just a methodology of this principle. How to do IoC? the answer is using DI, that's simple.

There are 4 types of DI for you to use as below:

1. Constructor DI, like above example in loose dependency
2. Property DI.
3. Interface DI.
4. Service Locator.

We often use constructor DI and property DI.
The pros of DI that everyone knows: LOOSE COUPLING between two objects. So why LOOSE COUPLING:

1. Easy to unit test for using mock object. If you are not familiar with mocking framework, I recommend take a look at Rhino, Moq or NMock2. For me I prefer Rhino Mock. Maybe I will write more about of unit test and mocking framework later. But you can know that the magic of unit test and mocking framework will improve your code design, the more your code is cover, the better your design are.

2. Easy for trouble shooting.

3. Maintainability will be improved as well, follow principle OOP (Open Close principle), your application will be more sustainable.

Talking about DI container or IoC container. This is is an abstraction responsible for object management, instantiation and configuration. Let's imagine that when you create an object, container will automatically create injects all dependent objects of this object into its constructor if you use constructor injection and so on. Technically, DI container is something like type pool or object pool that takes responsibility for creating objects and injecting dependency (DI). This is the difference of DI and DI container. With DI container, we also must notice about the lifetime of an object in container (or lifetime management), in general we have 3 lifetime managers:

1. Singleton. Hope you know what singleton is.

2. Per thread. Object is created when thread is created and is died when thread is died. More detail, object is contained in data slot of the thread.

3. Per request. For web application, object will live during the time of an HTTP request.

Be careful when using lifetime manager, depend on which context you should use which lifetime manager appropriately. Typical example that our team got: DON'T use data context as singleton, it will be a nightmare for you.

There are a lot of DI tools for you to find as below:

1. Unity from Microsoft, in Enterprise Library.
2. StructureMap from Jeremy Miller (my idol).
3. Spring.Net.
4. NInject.
5. Windsor Catlse.
6. Who knows more?

If you think that, ugh, why are there a lot of DI tools right now? you might be confused when opting an DI tool, maybe in the future you can switch to another DI tools, use Windsor castle in lieu of Unity for example. You just want to have a common interface for all DI tools, there are also a good solution for this kind of problem: Common Service Locator

Right now, DI is used a lot. Example for MVC when you customize controller factory (in mvc) to inject services into controllers (example in here will be better). As a chain of dependencies, DI container will continue inject repository (repository pattern) into services, then container injects Unit of work (DataContext, ObjectContext) into repository, my thought, or bla bla bla. Below is example how to injection dependency object into controllers:

Hope my little words above will make you clear about DI and DI Container

@My colleagues I know most of you are masters of this kind of concepts, correct me if I am wrong in here, your comments are always welcome.


  1. Dude, there are many English techinical articles out there which are definitely better than ours. Why do you persist in writting your Blog in English?

    Back to your post, I think we should choose one, love and get married with her until there is another reason to learn a new library :D. They are quite the same when created to solve the 1 problem.

  2. You know me don't you, why I have to write articles in Vietnamese? Yeah, I knows a lot of English technical articles are better than ours, but my purpose is to satisfy my passion about writing by English and technology as well

  3. Agreed with Cuong. Even though it'd be better for a multilingual blog and aim to everyone. Anyway, a good shot, dude!

  4. Dude, write something about Entity Framework :D

  5. Thien Le: Good artical, @Cuong: give some articals about DI tools to make it clearer!

  6. Ever want to be a hero in bed, try softabs!! She'll love you for it. More info at

    Don't wait, click now !!!!!!