views:

15312

answers:

22

I've been using Dependency Injection (DI) for a while, injecting either in a constructor, property, or method. I've never felt a need to use an Inversion of Control (IoC) container. However, the more I read, the more pressure I feel from the community to use an IoC container.

I played with .NET containers like StructureMap, NInject, Unity, and Funq. I still fail to see how an IoC container is going to benefit / improve my code.

I'm also afraid to start using a container at work because many of my co-workers will see code which they don't understand. Many of them may be reluctant to learn new technology.

Please, convince me that I need to use an IoC container. I'm going to use these arguments when I talk to my fellow developers at work.

+24  A: 

In my opinion the number one benefit of an IoC is the ability to centralize the configuration of your dependencies.

If you're currently using Dependency injection your code might look like this

public class CustomerPresenter
{
  public CustomerPresenter() : this(new CustomerView(), new CustomerService())
  {}

  public CustomerPresenter(ICustomerView view, ICustomerService service)
  {
    // init view/service fields
  }
  // readonly view/service fields
}

If you used a static IoC class, as opposed to the, IMHO the more confusing, configuration files, you could have something like this:

public class CustomerPresenter
{
  public CustomerPresenter() : this(IoC.Resolve<ICustomerView>(), IoC.Resolve<ICustomerService>())
  {}

  public CustomerPresenter(ICustomerView view, ICustomerService service)
  {
    // init view/service fields
  }
  // readonly view/service fields
}

Then, your Static IoC class would look like this, I'm using Unity here.

public static IoC
{
   private static readonly IUnityContainer _container;
   static IoC()
   {
     InitializeIoC();
   }

   static void InitializeIoC()
   {
      _container = new UnityContainer();
      _container.RegisterType<ICustomerView, CustomerView>();
      _container.RegisterType<ICustomerService, CustomerService>();
      // all other RegisterTypes and RegisterInstances can go here in one file.
      // one place to change dependencies is good.
   }
}
bendewey
Ben, what you're talking about is really Service Location, not Dependency Injection - there is a difference. I always prefer the first over the latter. http://stevenharman.net/blog/archive/2009/09/25/prefer-dependency-injection-to-service-location.aspx
stevenharman
Instead of doing the IoC.Resolve<T> in your default constructor, you should leverage the IOC container's ability to construct the object for you. Get rid of that empty constructor, and let the IOC container build the object for you.var presenter = IoC.Resolve<CustomerPresenter>(); voila! all dependencies already wired up.
Ben Scheirman
@ben, Agreed! but don't forget the fine point to tuck that Service Location up into some corner of your infrastructure, limiting the use of location to as few places as possible.
stevenharman
The down-side of centralization of this kind of configuration is the de-contextualization of knowledge. Joel points out this problem in saying, "code becomes, frankly, a lot harder to read".
Scott Bellware
@sbellware I disagree. While some knowledge becomes de-contextualized, it is mostly likely boilerplate code anyway which is merely detracting from the real work your class and methods are trying to get done.
Jesse C. Slicer
@sbellware, what knowledge? The interface being taken as a primal dependency serves as a contract and should be enough to both convey it's purpose I reserve my brain's stack for the context of the work site I'm currently focused on.
stevenharman
Steve,I know the .NET text and walked that walk for many years. I also have intimate familiarity with other modes and forms, and I understand viscerally that there are real tradeoffs. I know how and when to make those tradeoffs, but the diversity in my work life at least lets me tangibly understand Joel's perspective. Rather than lecture me on tools and tricks that I've been using for longer than most .NET mono-culturists, tell me something I don't know. I reserve my brain's stack for diverse, critical thinking rather than alt.net stasis and orthodoxy.
Scott Bellware
Jesse, "boilerplate" is a perspective. I'm not passing judgments on that perspective, but to say that it's just a perspective.
Scott Bellware
Scott, I get Joel's point; there is a loss of contextual knowledge when the configuration happens, well... outside the context of its use. And I wasn't trying to lecture on tools nor tricks to work around that tradeoff. Rather, I was observing that w/in the context of certain architectural constraints (in this case: .NET, JAVA, or any other language that requires a container) why not lean on the tools to do that type of work, when you can? I should have been more clear about the context under which I was making that statement.
stevenharman
+5  A: 

Because all the dependencies are clearly visible, it promotes creating components which are loosely coupled and at the same time easily accessible and reusable across the application.

bbmud
+18  A: 

Using a container is mostly about changing from an imperative/scripted style of initialization and configuration to a declarative one. This may have a few different beneficial effects:

  • Reducing hairball main-program startup routines.
  • Enabling fairly deep deployment-time reconfiguration capabilities.
  • Making dependency-injectable style the path of least resistance for new work.

Of course, there may be difficulties:

  • Code that requires complex startup/shutdown/lifecycle management may not be easily adapted to a container.
  • You will probably have to navigate any personal, process and team culture issues -- but then, that's why you asked...
  • Some of the toolkits are fast becoming heavyweight themselves, encouraging the sort of deep dependency that many DI containers started off as a backlash against.
Jeffrey Hantin
+10  A: 

I'm with you, Vadim. IoC containers take a simple, elegant, and useful concept, and make it something you have to study for two days with a 200-page manual.

I personally am perplexed at how the IoC community took a beautiful, elegant article by Martin Fowler and turned it into a bunch of complex frameworks typically with 200-300 page manuals.

I try not to be judgemental (HAHA!), but I think that people who use IoC containers are (A) very smart and (B) lacking in empathy for people who aren't as smart as they are. Everything makes perfect sense to them, so they have trouble understanding that many ordinary programmers will find the concepts confusing. It's the curse of knowledge. The people who understand IoC containers have trouble believing that there are people who don't understand it.

The most valuable benefit of using an IoC container is that you can have a configuration switch in one place which lets you change between, say, test mode and production mode. For example, suppose you have two versions of your database access classes... one version which logged aggressively and did a lot of validation, which you used during development, and another version without logging or validation that was screamingly fast for production. It is nice to be able to switch between them in one place. On the other hand, this is a fairly trivial problem easily handled in a simpler way without the complexity of IoC containers.

I believe that if you use IoC containers, your code becomes, frankly, a lot harder to read. The number of places you have to look at to figure out what the code is trying to do goes up by at least one. And somewhere in heaven an angel cries out.

Joel Spolsky
If I may say, it looks like different view of the world for different people. People who use IOC are like ideologists wanting the world to follow their footsteps )
shahkalpesh
Couldn't agree more. Started looking into using a DI framework (StructureMap) which claims to be the oldest IoC container. The complexity was staggering considering what I wanted. Instead I am writing my own very simple Service Locator. Perhaps one of the other IoC containers would have made more sense but I really don't see the point nor do I have the leisure of spending several days figuring out how to use something that will make reading the code more complex.
eesh
Martin Fowler, while a great author, didn't in any way invent IoC.
Nicholas Blumhardt
I think you have your facts mixed up a bit Joel. IoC and containers came first, then came the treatise by Martin, not vice-versa.
Glenn Block
shahkalpesh Which IoC container did you work with?
Glenn Block
eesh most of the containers let you get going very quickly. With autofac, structure map, unity, ninject, etc you should be able to have the container working in about 5 mins. Yes they have advanced features but you don't need those to get off the ground.
Glenn Block
Sam Saffron
I used structure map and Unity before. You can do the basic very simply (a few lines of code, reading their FAQs), but if you try to shoehorn it into too many situations it gets complex fast.
Jess
I am a fan of IoC containers but in no way do I think they are 'easy to get' (the concepts are quite abstract and go to the heart of software development)... Also I think Joel's answer is going to encourage people to dismiss the important ideas behind IoC hence my downvote.
Schneider
Also if anyone is writing testable code, following SRP etc (which I think most people agree is good) your class basically *always* end up with a ton of interfaces dependancies. I ask: without an IoC framework how on earth do you wire up all your classes?
Schneider
Following IoC without a container is, quite frankly, far more complicated and time consuming than "reading a 200 page manual". Not to mention that the containers came before Mr. Fowler's article and are part and parcel with the design pattern. So, if Mr. Spolsky wanted to claim that IoC was overly complicated, we might have to agree to disagree, but since that's not what he said... he's just plain wrong.
wekempf
Joel I used to think the same way as you. Then I literally and seriously spent five minutes to figure out how to get the most basic setup running and was amazed at the 80/20 rule in action. It makes code much cleaner especially in the simple cases.
Justin Bozonier
I've been programming for less than two years and I read the Ninject wiki and got it. I have been using DI in a lot of places since then. Did you see that? Less than two years and reading a few pages on a wiki. I'm not a wizard or anything. I don't consider myself a genius, but it was easy for me to understand. I can't imagine someone who really understands OO not being able to grasp it.Also, what 200-300 page manuals are you referring to? I've never seen those. All of the IoC containers I have used are pretty short and to the point.
J.R. Garcia
So if you don't use IoC containers, it's because you are not smart enough to understand them, but if you do use IoC containers (and love every second of it) it is because you are too smart?
Anderson Imes
@Joel-spolsky I don't understand why you think it makes the code harder to read. The code is actually simpler to understand. All your dependencies just get injected through the constructors. No tracing of dependencies is necessary. It takes about 2-3 lines of code to setup depending on how many items you need to create.
Micah
I don't know why you guys are questioning Joel. He knows what is going on in our heads, the dude is like Santa Claus or something. He knows we can't understand IoC and more than we can put our pants on in the morning without help. Thank you Joel for reminding me how dumb I am, I was getting way too full of myself.
Kevin Sheffield
Joel - I assume you haven't looked at IoC Containers for a while because a watershed has most certainly passed in the past couple of years. They're now trivial to configure mainly due to conventions. DI is good; make a good practice easier to implement without having to pepper your classes with poormans DI ctors; reducing lines of code and; centralising your dependency resolution in one succinct place in your code base.
Ed Blackburn
Joel - Your claim that the main benefit of IoC is swapping out implementations for testing, shows that you don't understand IoC. IoC is about separation of concerns and freeing you from having to re-write all your instantiation chains when you want to add/remove a dependency. Not to mention numerous other benefits
Arne Claassen
You are very wrong.
ScottKoon
I really don't understand how I was convinced to come to the StackOverflow DevDays
CodeClimber
I need to make an I heart VBA t-shirt for devdays
Wow... Geez talk about the backlash. I'm thinking this is some sort of experiment on how not to go against the crowds. Honestly, I haven't been developing full time for too long (almost 3 years now) and I have used 3 different .Net IoC containers. I don't claim to understand everything that they do. But I do know that they have simplified my development.In the same way, I use SQL Server. But I won't claim to understand all the intricacies of a modern RDBMS system.
Min
kevindente RT @aaronjensen: RT @mhinze: can we get some downvotes? RT @chadmyers: More Walmart Wisdom from Joel Spolsky: http://bit.ly/2KRFvw Wow Joel, I guess you should just keep your mouth shut, people are actually tweeting to downvote you now. Nice.
tbone
In Joel's defense, DI and IoC may be very difficult to grasp in Z80 Assembly or when using templates with COM in multi-threaded code.
ScottKoon
Joel - you should totally delete this answer to get the Peer Pressure badge!
Graeme Perrow
Welcome to 1988.. A programmer who can't grok IoC is like a programmer who can grok OOP (read: not a programmer)
Ryan Emerle
+1 well written and thought out. One thing I think a lot of people don't realize is that adding in a framework or such to "magically" handle something automatically adds complexity and limitations to a system. Sometimes it's worth it, but often something gets overlooked and entropy is released into the code trying to fix a problem enforced by limitations. It may save time up front, but people have to realize that may cost 100 fold down the line trying to fix some obscure problem only a handful of people truly have enough knowledge to solve.
Kevin
@Ryan Emerle: It seems to me that there is a difference between grokking IoC and believing that an IoC framework should be commonly employed.
Steven Huwig
I totally agree with Joel.. seems like an awful lot of navel grazing here when a simpler solution is staring you right in the face. People tend to forget the KISS principle and this is one case where it sorely needs to be applied.
Craig
IOC is fine when done correctly, but it's really horrible when someone messes it up, and eventually someone will mess it up.It also can obfuscate exactly what's happening with a bit of code. IMHO it's always better if you can look at a bit of code and get what's going on in a few seconds without digging through the rest of the source.
overstood
I'm tempted to +1 this just for aggressively expressing the minority opinion, but I will refrain-I think you're partially correct in that IOC containers can make simple things complex, and make the code harder to trace (because so many of the code paths become indirect), but there are other benefits than what you mention, allowing for for a convenient way of scaffolding application resources. You'd have to handle that somewhere. I also don't think they're too complex, but then again I work somewhere where all the IOC is coded backwards(!) so maybe you shouldn't take my word for that.
Steve B.
You made a great point for small applications. However, the complexity cost of using an IoC container is surmountable in medium to large projects and pays for itself in value.
Robert Venables
There are a few commenters that are supporting Joel's argument, citing KISS. The thing these people don't realise is that using and IoC container is the KISS way. KISS stands for "keep it simple, stupid", not "start off simple, end up with a mess, stupid" (SOSEUWMS). An IoC container helps you KEEP your application simple as it grows over time.I'm sure Joel is a fan of SOSEUWMS, because it helps keep bug trackers in high demand.
Paul Batum
It's not that I have trouble believing that there are people who don't understand IoC, it's that I have trouble believing there are people who won't try to understand. Truly, it's not that hard. I think the biggest problem is the name sounds intimidating.
KevDog
I am not sure if you have seriously given a try to StructureMap. Please have a look at one of the Rob Connery's Video, you can start using it in 10 mins. No Kidding.!!!
Ajay
Like in so many situations, your attempt to protect developers from powerful ideas just ends up patronizing them and devaluing their abilities. In order to make your point stick, you *must* provide an example of an IoC container with a 200 page manual - I simply don't believe there is one! Which makes your contrarian rhetoric that much less palatable. In order to practice IoC/DI, one must learn the problems being solved by these techniques. With that understanding, you can use any tooling, or none. Without it, it doesn't matter what tooling you use.
Bryan Watts
Lol @ all the nerd rage
Gurdas Nijor
@KevDog: I totally agree about the name problem. Dependency Injection Container totally sounds like something invented by a Dalek.
Bill Karwin
Yeah...what he said! Actually, scratch that...Joel, I'm starting to suspect that you get all excited at the prospect of throwing a wrench in the works. As some others have said, I'm coming to Dev Days London, but I sure as hell hope I don't have to listen to you talk about programming - hopefully you'll realise you need to leave that part up to people like Jon Skeet who actually know what they're talking about...
RobertTheGrey
Our team use Guice and are extremely happy. They have none of the problems Joel describes and they are getting caching and code-simplification benefits all over the place.
Douglas Squirrel
I use an IOC container and I am not smart.
Miyagi Coder
Your buffoonery is on full display here joel. Bravo good skir. Bravo.
Pierreten
+1 for a sanity check to reduce code complexity
alchemical
I am guessing Joel is talking about something like Spring and not Guice. Guice is very simple and doesn't require more than a 15 minute introduction. Spring on the other hand is a more complicated (yet more versatile) beast.
ponzao
Wow, most controversial post... just because Joel dared suggest that IoC is not the be-all-end-all of development grail. Honestly, enough with the religious wars already! Even IF (big if) IoC is the best thing since sliced butter, even any half-decent programmer with more than a couple weeks real-world experience outside of academia could not believe it is the best solution in all - or even most - situations.
AviD
On the other hand, I consider myself to be **quite** smart, and while I did spend the time to research and experience enough to grok IoC - I still hate it. With a *passion*. But that might be just because I spend most of my time actually *reading* code, not just producing write-only code.... which is where IoC really shines, write a lil bits of code, then combine them and recombine them differently at config time. On the other hand, code readability takes a HUGE hit with IoC.
AviD
If you are dealing with wiring up hundreds+ of components and want a centralised way of managing the dependencies, an IoC container makes life much easier. I was sceptical about using a container because I didn't see the benefits. I'm now convinced that they're useful bits of kit. We swap out huge sections of our object graph construction via configuration and re-use our modules in different contexts (this is something we do extensively, it's not just a speculative pipe dream). The trade-off is unfamiliarity with a new concept and sacrificing some compile time safety.
Mark Simpson
+10  A: 

I'm a fan of declarative programming (look at how many SQL questions I answer), but the IoC containers I've looked at seem too arcane for their own good.

...or perhaps the developers of IoC containers are incapable of writing clear documentation.

...or else both are true to one degree or another.

I don't think the concept of an IoC container is bad. But the implementation has to be both powerful (that is, flexible) enough to be useful in a wide variety of applications, yet simple and easily understood.

It's probably six of one and half a dozen of the other. A real application (not a toy or demo) is bound to be complex, accounting for many corner cases and exceptions-to-the-rules. Either you encapsulate that complexity in imperative code, or else in declarative code. But you have to represent it somewhere.

Bill Karwin
I like your observation about the unavoidable presence of complexity.The difference between container-based and manual dependency wiring is that using a container, you can focus on each component individually, and thus manage increasing complexity in a fairly linear way.Systems with manual dependency wiring are more challenging to evolve, since the act of configuring one component is hard to isolate from the configuring of everything else.
Nicholas Blumhardt
I like AspectJ. It's not Java, but it's not XML either. It sort of *IS* Java, in that it feels and looks like Java by being an extension of it. I'd rather keep my aspects out of my POJOs and in my business logic. I'd almost like to keep my IoCs out as well. There's entirely too much XML in wiring up tools, IMHO.If I have to have configuration files, let them be BeanShell scripts. /Java, apply your .Net workalikes here -> .
Chris Kaminski
+20  A: 

IoC Containers are also good for loading deeply nested class dependencies. For example if you had the following code using Depedency Injection.

public void GetPresenter()
{
    var presenter = new CustomerPresenter(new CustomerService(new CustomerRepository(new DB())));
}

class CustomerPresenter
{
    private readonly ICustomerService service;
    public CustomerPresenter(ICustomerService service)
    {
        this.service = service;
    }
}

class CustomerService
{
    private readonly IRespoistory<Customer> repository;
    public CustomerService(IRespoistory<Customer> repository)
    {
        this.repository = repository;
    }
}

class CustomerRepository : IRespoistory<Customer>
{
    private readonly DB db;
    public CustomerRepository(DB db)
    {
        this.db = db;
    }
}

class DB { }

If you had all of these dependencies loaded into and IoC container you could Resolve the CustomerService and the all the child dependencies will automatically get resolved.

For example:

public static IoC
{
   private IUnityContainer _container;
   static IoC()
   {
       InitializeIoC();
   }

   static void InitializeIoC()
   {
      _container = new UnityContainer();
      _container.RegisterType<ICustomerService, CustomerService>();
      _container.RegisterType<IRepository<Customer>, CustomerRepository>();
   }

   static T Resolve<T>()
   {
      return _container.Resolve<T>();
   }
}

public void GetPresenter()
{
   var presenter = IoC.Resolve<CustomerPresenter>();
   // presenter is loaded and all of its nested child dependencies 
   // are automatically injected
   // -
   // Also, note that only the Interfaces need to be registered
   // the concrete types like DB and CustomerPresenter will automatically 
   // resolve.
}
bendewey
+9  A: 

I think most of the value of an IoC is garnered by using DI. Since you are already doing that, the rest of the benefit is incremental.

The value you get will depend on the type of application you are working on:

  • For multi-tenant, the IoC container can take care of some of the infrastructure code for loading different client resources. When you need a component that is client specific, use a custom selector to do handle the logic and don't worry about it from your client code. You can certainly build this yourself but here's an example of how an IoC can help.

  • With many points of extensibility, the IoC can be used to load components from configuration. This is a common thing to build but tools are provided by the container.

  • If you want to use AOP for some cross-cutting concerns, the IoC provides hooks to intercept method invocations. This is less commonly done ad-hoc on projects but the IoC makes it easier.

I've written functionality like this before but if I need any of these features now I would rather use a pre-built and tested tool if it fits my architecture.

As mentioned by others, you can also centrally configure which classes you want to use. Although this can be a good thing, it comes at a cost of misdirection and complication. The core components for most applications aren't replaced much so the trade-off is a little harder to make.

I use an IoC container and appreciate the functionality but have to admit that I've noticed the trade-off: My code becomes more clear at the class level and less clear at the application level (i.e. visualizing control flow).

Steven Lyons
+2  A: 

In the .NET world AOP isn't too popular, so for DI a framework is your only real option, whether you write one yourself or use another framework.

If you used AOP you can inject when you compile your application, which is more common in Java.

There are many benefits to DI, such as reduced coupling so unit testing is easier, but how will you implement it? Do you want to use reflection to do it yourself?

James Black
The new MEF framework allows just this for .NET and I must say the code is cleaner and easier to understand, makes it a lot easier to understand the DI concept.
TT
@TT: MEF is *not* a dependency injection container and has nothing to do with AOP.
Mauricio Scheffer
+6  A: 

It sounds to me like you already built your own IoC container (using the various patterns which were described by Fowler) and are asking why someone else's implementation is better than yours.

So, you have a bunch of code that already works. And are wondering why you would want to replace it with someone else's implementation.

Pros for considering a 3rd party IoC container

  • You get bugs fixed for free
  • The library design may be better than yours
  • People may be already familiar with the particular library
  • The library may be faster than yours
  • It may have some features you wish you implemented but never had time to (Do you have a service locater?)

Cons

  • You get bugs introduced, for free :)
  • The library design may be worse than yours
  • You have to learn a new API
  • Too many features you will never use
  • Its usually harder to debug code you did not write
  • Migrating from a previous IoC container may be tedious

So, weigh your pros against your cons and make a decision.

Sam Saffron
I agree with you Sam - DI is a type of IoC, so if the original poster is doing DI, they're already doing IoC so ther real question is why roll your own.
Jamie Love
+11  A: 

Presumably no one is forcing you to use a DI container framework. You're already using DI to decouple your classes and improve testability, so you're getting many of the benefits. In short, you're favoring simplicity, which is generally a good thing.

If your system reaches a level of complexity where manual DI becomes a chore (that is, increases maintenance), weigh that against the team learning curve of a DI container framework.

If you need more control over dependency lifetime management (that is, if you feel the need to implement the Singleton pattern), look at DI containers.

If you use a DI container, use only the features you need. Skip the XML configuration file and configure it in code if that is sufficient. Stick to constructor injection. The basics of Unity or StructureMap can be condensed down to a couple of pages.

TrueWill
Very good response. The only thing I'd differ in opinion on is whether or not manual injection EVER can be considered less complex or not a chore.
wekempf
+3  A: 

As you continue to decouple your classes and invert your dependencies, the classes continue to stay small and the "dependency graph" continues to grow in size. (This isn't bad.) Using basic features of an IoC container makes wiring up all these objects trivial, but doing it manually can get very burdensome. For example, what if I want to create a new instance of "Foo" but it needs a "Bar". And a "Bar" needs an "A", "B", and "C". And each of those need 3 other things, etc etc. (yes, I can't come up with good fake names :) ).

Using an IoC container to build your object graph for you reduces complexity a ton and pushes it out into one-time configuration. I simply say "create me a 'Foo'" and it figures out what's needed to build one.

Some people use the IoC containers for much more infrastructure, which is fine for advanced scenarios but in those cases I agree it can obfuscate and make code hard to read and debug for new devs.

Aaron Lerch
why do we continue to appease the lowest common denominator rather than work to pull them up?
stevenharman
I didn't say anything about appeasing. I just said the advanced scenarios can make things more obfuscated for new devs - this is a fact. I didn't say "so don't do it". But even then (time for devil's advocate) there are often other ways to accomplish the same thing that make a codebase more explicit and approachable. Hiding complexity in your infrastructure tool custom configuration just because you can isn't the right reason, and it pulls nobody up.
Aaron Lerch
+245  A: 

Wow, can't believe that Joel would favor this:

var svc = new ShippingService(new ProductLocator(), 
   new PricingService(), new InventoryService(), 
   new TrackingRepository(new ConfigProvider()), 
   new Logger(new EmailLogger(new ConfigProvider())));

over this:

var svc = IoC.Resolve<IShippingService>();

Many folks don't realize that your dependencies chain and become nested, and it quickly becomes unwieldy to wire them up manually. Even with factories, the duplication of your code is just not worth it.

IoC containers can be complex, yes. But for this simple case I've shown it's incredibly easy.


Edit: okay let's justify this even more. Let's say you have some entities or model objects that you want to bind to a smart UI. This smart UI (we'll call it Shindows Morms) wants you to implement INotifyPropertyChanged so that it can do change tracking & update the UI accordingly.

"OK, that doesn't sound so hard" so you start writing.

You start with this:

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime CustomerSince { get; set; }
    public string Status { get; set; }
}

..and end up with this:

public class UglyCustomer : INotifyPropertyChanged
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            string oldValue = _firstName;
            _firstName = value;
            if(oldValue != value)
                OnPropertyChanged("FirstName");
        }
    }

    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set
        {
            string oldValue = value;
            _lastName = value;
            if(oldValue != value)
                OnPropertyChanged("LastName");
        }
    }

    private DateTime _customerSince;
    public DateTime CustomerSince
    {
        get { return _customerSince; }
        set
        {
            DateTime oldValue = value;
            _customerSince = value;
            if(oldValue != value)
                OnPropertyChanged("CustomerSince");
        }
    }

    private string _status;
    public string Status
    {
        get { return _status; }
        set
        {
            string oldValue = value;
            _status = value;
            if(oldValue != value)
                OnPropertyChanged("Status");
        }
    }

    protected virtual void OnPropertyChanged(string property)
    {
        if(PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

That's disgusting plumbing code, and I maintain that if you're writing code like that by hand you're stealing from your client. There are better, smarter way of working.

Ever hear that term, work smarter, not harder?

Well imagine some smart guy on your team came up and said: "Here's an easier way"

If you make your properties virtual (calm down, it's not that big of a deal) then we can weave in that property behavior automatically. (This is called AOP, but don't worry about the name, focus on what it's going to do for you)

Depending on which IoC tool you're using, you could do something that looks like this:

var bindingFriendlyInstance = IoC.Resolve<Customer>(new NotifyPropertyChangedWrapper());

Poof! All of that manual INotifyPropertyChanged BS is now automatically generated for you, on every virtual property setter of the object in question.

Is this magic? YES! If you can trust the fact that this code does its job, then you can safely skip all of that property wrapping mumbo-jumbo. You've got business problems to solve.

Some other interesting uses of an IoC tool to do AOP:

  • Declarative & nested database transactions
  • Declarative & nested Unit of work
  • Logging
  • Pre/Post conditions (Design by Contract)
Ben Scheirman
Yes. This. A thousand times this.
Anderson Imes
I a thousand times yes your yes.
jfar
Excellent addition. I often find myself doing that kind of plumbing for an object. I will now open my mind for IoC and see what it can be accounted for.
Pierre-Alain Vigeant
Also, which IoC tool are you referring too?
Pierre-Alain Vigeant
Oops, you forgot to make all the properties virtual, and it didn't work. Is the time you needed to spend debugging this stealing from your client too? (Apologies if you're not using DynamicProxy. :P)
Thom
Actually we're using IoC. It wasn't as hard as thought to teach everyone about IoC.
Vadim
Truly the only way of writing maintainable code.
Ryan Emerle
You sacrifice a LOT of clarity by using the INotifyPropertyChanged wrapper. If it takes you more than a few minutes to write that plumbing to begin with then you're in the wrong business. Less is not more when it comes to the verbosity of your code!
overstood
@overstood - I completely disagree. First, where does clarity matter in this example? Nearest the consumer. The consumer is explicitly asking for INotifyPropertyChanged.Just because we *can* create this type of code with micro or macro code generation, doesn't mean it's a good idea to write it.That INotifyPropertyChanged gunk is just rote, mundane, plumbing and actually detracts from the readability of the class in question.
Ben Scheirman
Dude! I wish I had more than one up-vote per Answer.
RKitson
I'm interested in what tool knows how to inject code automatically to work as the NotifyPropertyChangedWrapper(), though your example is crafted to be particularly poor - if you'd use Observable wrappers around each property then you could make the code significantly cleaner without using an IoC container (or whatever NotifyPropertyChangedWrapper()) is.
Jamie Love
Jamie - the real work is generally done by something like DynamicProxy that lets you easily intercept calls to (virtual or interface) members on your objects. This has little to do with IoC - in fact, in the example above, we're talking about an entity anyway, so you'd never be resolving that from your container. You'd have to muck about with your ORM, which is entirely separate. I don't want to sound anti-IoC - I'm using Castle and a ton of custom facilities in my current project, but the above example is a poor one (which doesn't work as written, or as described).
Thom
To be fair, my comment regarded the first half of your answer before the edit. You went a bit out there with the NotifyPropertyChanged wrapper (this is not a common case, though admittedly cool).
Anderson Imes
@Jamie, @ThomI don't think Ben's example above was intended to be a complete implementation by any means. That said, I'd be curious to see your implementation of "Observable wrappers around each property" that doesn't look like the typical implementation of INotifyPropertyChanged.
Ryan Riley
Marked down because you're confusing the Dependency Injection pattern and the Service Locator pattern. The former is meant to be used instead of the latter. E.g. an object (or indeed the entire system) should never call Resolve(...) to get an instance of IShippingService. The objects that need an IShippingService should receive a reference to the instance they should use when they are constructed, and some higher layer is responsible for connecting the two objects together.
Nat
@Nat: I'm not confusing the two. I'm illustrating the power of an IoC tool. Eventually you have to ask the container for an object, and ideally this happens way at the forefront, and subsequent injections happen for you.
Ben Scheirman
@Nat: See my comment on this exact topic on the next answer down.
Ben Scheirman
This must be one of the coolest answers that I have read on SO. Well done Ben, wahhahaha, great explanation
PieterG
Whatever this is (IoC, DynamicProxy, AOP or *black magic*) can someone link me to a concrete article/specific documentation in framework that provides more details for achieving this?
fostandy
A: 

Dependency Injection in an ASP.NET project can be accomplished with a few lines of code. I suppose there is some advantage to using a container when you have an app that uses multiple front ends and needs unit tests.

etechpartner
can you give an example? How is DI different in ASP.NET as compared to any other application type?
taoufik
+3  A: 

Don't use stuff because of pressure.

Dan Finch
A: 

Personally, I use IoC as some sort of structure map of my application (Yeah, I also prefer StructureMap ;) ). It makes it easy to substitute my ussual interface implementations with Moq implementations during tests. Creating a test setup can be as easy as making a new init-call to my IoC-framework, substituting whichever class is my test-boundary with a mock.

This is probably not what IoC is there for, but it's what I find myself using it for the most..

cwap
+2  A: 

I just so happen to be in the process of yanking out home grown DI code and replacing it with an IOC. I have probably removed well over 200 lines of code and replaced it with about 10. Yes, I had to do a little bit of learning on how to use the container (Winsor), but I'm an engineer working on internet technologies in the 21st century so I'm used to that. I probably spent about 20 minutes looking over the how tos. This was well worth my time.

Matt Wrock
+6  A: 

Whenever you use the "new" keyword, you are creating a concrete class dependency and a little alarm bell should go off in your head. It becomes harder to test this object in isolation. The solution is to program to interfaces and inject the dependency so that the object can be unit tested with anything that implements that interface (eg. mocks).

The trouble is you have to construct objects somewhere. A Factory pattern is one way to shift the coupling out of your POXOs (Plain Old "insert your OO language here" Objects). If you and your co-workers are all writing code like this then an IoC container is the next "Incremental Improvement" you can make to your codebase. It'll shift all that nasty Factory boilerplate code out of your clean objects and business logic. They'll get it and love it. Heck, give a company talk on why you love it and get everyone enthused.

If your co-workers aren't doing DI yet, then I'd suggest you focus on that first. Spread the word on how to write clean code that is easily testable. Clean DI code is the hard part, once you're there, shifting the object wiring logic from Factory classes to an IoC container should be relatively trivial.

floater81
+4  A: 

Dittos about Unity. Get too big, and you can hear the creaking in the rafters.

It never surprises me when folks start to spout off about how clean IoC code looks are the same sorts of folks who at one time spoke about how templates in C++ were the elegant way to go back in the 90's, yet nowadays will decry them as arcane. Bah !

M Lopez
+7  A: 

The biggest benefit of using IoC containers for me (personally, I use Ninject) has been to eliminate the passing around of settings and other sorts of global state objects.

I don't program for the web, mine is a console application and in many places deep in the object tree I need to have access to the settings or metadata specified by the user that are created on a completely separate branch of the object tree. With IoC I simply tell Ninject to treat the Settings as a singleton (because there is always only one instance of them), request the Settings or Dictionary in the constructor and presto ... they magically appear when I need them!

Without using an IoC container I would have to pass the settings and/or metadata down through 2, 3, ..., n objects before it was actually used by the object that needed it.

There are many other benefits to DI/IoC containers as other people have detailed here and moving from the idea of creating objects to requesting objects can be mind-bending, but using DI was very helpful for me and my team so maybe you can add it to your arsenal!

Jeffrey Cameron
This is a huge plus. Can't believe no one else has mentioned it prior. Without the need of passing or storing tempoary objects makes code much cleaner.
Finglas
+2  A: 

IoC frameworks are excellent if you want to...

  • ...throw away type safety. Many (all?) IoC frameworks forces you to execute the code if you want to be certain everything is hooked up correctly. "Hey! Hope I got everything set up so my initializations of these 100 classes won't fail in production, throwing null-pointer exceptions!"

  • ...litter your code with globals (IoC frameworks are all about changing global states).

  • ...write crappy code with unclear dependencies that's hard to refactor since you'll never know what depends on what.

The problem with IoC is that the people who uses them used to write code like this

public class Foo {
    public Bar Apa {get;set;}
    Foo() {
        Apa = new Bar();
    }
}

which is obviously flawed since the dependency between Foo and Bar is hard-wired. Then they realized it would be better to write code like

public class Foo {
    public IBar Apa {get;set;}
    Foo() {
        Apa = IoC<IBar>();
    }
}

which is also flawed, but less obviously so. In Haskell the type of Foo() would be IO Foo but you really don't want the IO-part and is should be a warning sign that something is wrong with your design if you got it.

To get rid of it (the IO-part), get all advantages of IoC-frameworks and none of it's drawbacks you could instead use an abstract factory.

The correct solution would be something like

data Foo = Foo { apa :: Bar }

or maybe

data Foo = forall b. (IBar b) => Foo { apa :: b }

and inject (but I wouldn't call it inject) Bar.

Also: see this video with Erik Meijer (inventor of LINQ) where he says that DI is for people who don't know math (and I couldn't agree more): http://www.youtube.com/watch?v=8Mttjyf-8P4

Unlike Mr. Spolsky I don't believe that people who use IoC-frameworks are very smart - I simply believe they don't know math.

Down-vote me all you can (if you feel like it) but this is my opinion.

finnsson
throw away type safety - except your example is still type safe, that's what the interface is. You're not passing around types of object, but the interface type. And type safety doesn't do away with null reference exceptions anyway, you can happy pass null references as type safe parameters - that's not a type safety issue at all.
blowdart
You throw away type safety since you don't know if the type is wired up or not in the IoC-container (the type of IoC<IBar> is not `IBar`, it is in fact `IO IBar`).. And yes, in the Haskell-example I can't get a null-reference.
finnsson
Type safety not really about an object being wired up, at least not in Java/C#, it's simply about the object being of the right type. And ConcreteClass myClass = null is still of the right type. If you want to enforce not null then code contracts come into play.
blowdart
Sure, null-pointers can exist in c#/java without IoC but you wouldn't write `foo = null; foo.ToString()` but that's what you can get with IoC-frameworks. `ToString` demands an object, which is another type than "maybe an object". Just because c#/java/javascript doesn't make a distinction compile-time between `Object` and `Maybe Object` doesn't mean that there is no difference (i.e. there is a difference). Thus you'll get less type safety when using IoC-frameworks.That aside I think that less type safety is a secondary problem compared to the globals that IoC introduce.
finnsson
I agree with your 1st point to some extent, I'd like some explanation of 2nd and the 3rd has never been a problem for me. Your C# sample uses the IoC container as a service locator, a common mistake. And comparing C# vs Haskell = apples vs oranges.
Mauricio Scheffer
obviously you're confused about what's DI and what's Service Locator Pattern..
Carl Hörberg
+2  A: 

You don't need an IoC container.

But if you're rigorously following a DI pattern, you'll find that having one will remove a ton of redundant, boring code.

That's often the best time to use a library/framework, anyway - when you understand what it's doing and could do it without the library.

kyoryu
+4  A: 

I've found that correctly implementing Dependency Injection tends to force programmers to use a variety of other programming practices that help to improve the testability, flexibility, maintainability, and scalability of code: practices like the Single Responsibility Principle, Separations of Concerns, and coding against APIs. It feels like I'm being compelled to write more modular, bite-sized classes and methods, which makes the code easier to read, because it can be taken in bite-sized chunks.

But it also tends to create rather large dependency trees, which are far more easily managed via a framework (especially if you use conventions) than by hand. Today I wanted to test something really quickly in LINQPad, and I figured it'd be too much bother to create a kernel and load in my modules, and I ended up writing this by hand:

var merger = new SimpleWorkflowInstanceMerger(
    new BitFactoryLog(typeof(SimpleWorkflowInstanceMerger).FullName), 
    new WorkflowAnswerRowUtil(
        new WorkflowFieldAnswerEntMapper(),
        new ActivityFormFieldDisplayInfoEntMapper(),
        new FieldEntMapper()),
    new AnswerRowMergeInfoRepository());

In retrospect, it would have been quicker to use the IoC framework, since the modules define pretty much all of this stuff by convention.

Having spent some time studying the answers and comments on this question, I am convinced that the people who are opposed to using an IoC container aren't practicing true dependency injection. The examples I've seen are of practices that are commonly confused with dependency injection. Some people are complaining about difficulty "reading" the code. If done correctly, the vast majority of your code should be identical when using DI by hand as when using an IoC container. The difference should reside entirely in a few "launching points" within the application.

In other words, if you don't like IoC containers, you probably aren't doing Dependency Injection the way it's supposed to be done.

Another point: Dependency Injection really can't be done by hand if you use reflection anywhere. While I hate what reflection does to code navigation, you have to recognize that there are certain areas where it really can't be avoided. ASP.NET MVC, for example, attempts to instantiate the controller via reflection on each request. To do dependency injection by hand, you would have to make every controller a "context root," like so:

public class MyController : Controller
{
    private readonly ISimpleWorkflowInstanceMerger _simpleMerger;
    public MyController()
    {
        _simpleMerger = new SimpleWorkflowInstanceMerger(
            new BitFactoryLog(typeof(SimpleWorkflowInstanceMerger).FullName), 
            new WorkflowAnswerRowUtil(
                new WorkflowFieldAnswerEntMapper(),
                new ActivityFormFieldDisplayInfoEntMapper(),
                new FieldEntMapper()),
            new AnswerRowMergeInfoRepository())
    }
    ...
}

Now compare this with allowing a DI framework to do it for you:

public MyController : Controller
{
    private readonly ISimpleWorkflowInstanceMerger _simpleMerger;
    public MyController(ISimpleWorkflowInstanceMerger simpleMerger)
    {
        _simpleMerger = simpleMerger;
    }
    ...
}

Using a DI framework, note that:

  • I can unit-test this class. By creating a mock ISimpleWorkflowInstanceMerger, I can test that it gets used the way I anticipate, without the need for a database connection or anything.
  • I use far less code, and the code is much easier to read.
  • If one of my dependency's dependency's changes, I don't have to make any changes to the controller. This is especially nice when you consider that multiple controllers are likely to use some of the same dependencies.
  • I never explicitly reference classes from my data layer. My web application can just include a reference to the project containing the ISimpleWorkflowInstanceMerger interface. This allows me to break the application up into separate modules, and maintain a true multi-tier architecture, which in turn makes things much more flexible.

A typical web application will have quite a few controllers. All of the pain of doing DI by hand in each controller will really add up as your application grows. If you have an application with only one context root, which never tries to instantiate a service by reflection, then this isn't as big a problem. Nevertheless, any application that uses Dependency Injection will become extremely expensive to manage once it reaches a certain size, unless you use a framework of some kind to manage the dependency graph.

StriplingWarrior