SEARCH:   GO
{Blog entries}
Feb 1

Written by: Javier Callico
2/1/2010  RssIcon

This post from Uncle Bob generated lots of talking on the developer community regarding Inversion of Control (IoC) and Dependency Injection (DI), which is always good for the sake of  learning and general awarness. Almost every developer with a blog posted about their take on this subject. Two people which I respect a great deal and suscribe to their blogs also posted their reactions, please read Ayende and Davy's blogs for a better understanding of their positions.

What I get from all this, in short, is that Uncle Bob accepts the advantages of using dependency injection but prefers to encapsulate the access to the IoC container on factories. Davy seems to accept this approach on another post but chooses to also inject the factories into the clases.

After reading all these and other posts I asked myself the following question: What would be the best way to refactor legacy code which uses Factories and Repositories to take advantage of DI?

I don't think that replacing all the references to factories and repositories on the classes to direct references to the IoC container will be the right approach. IMO, these are two fundamental components of Domain Driven Design, which I'm a big proponent of, and their use should be encouraged. Also, injecting all factories, like Davy suggests, will be a little overkill specially for legacy code which already supports a method for factories to be extended and modified (by using inheritance).

So my choice is to modify the factories and repositories to use an IoC container, but I'm also going to abstract the IoC container so it can be easily replaced given the many excellent options available out there.

Let`s get down to the code and start by introducing two classes: SampleClass, SampleFactory. All over the application SampleClass is created by using a method located on SampleFactory:

 

 

Tags: .NET , DI
Categories:

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel