0

Assuming I'm using an IoC container, why exactly must my types have their dependencies injected? Why can't every type just assume that the container exists and just retrieve the dependencies it needs for itself? What are the disadvantages to this latter approach?

I'm aware of property injection but I don't want that suggested as an answer even though it does save on having long parameter list constructors in more complex types. And this doesn't save on the code you ultimately need to type/maintain. Oh, and I'd prefer to have the dependencies readonly/final (personal preference).

So here's a typical example of constructor injection in C#:

public class Calculator
{
    private readonly IAdder _adder;
    private readonly ISubtractor _subtractor;
    private readonly IMultiplier _multiplier;
    private readonly IDivider _divider;

    public Calculator(IAdder adder, ISubtractor subtractor,
                      IMultiplier multiplier, IDivider divider)
    {
        _adder = adder;
        _subtractor = subtractor;
        _multiplier = multiplier;
        _divider = divider;
    }
}

And here's what I'd rather do:

public class Calculator
{
    private readonly IAdder _adder;
    private readonly ISubtractor _subtractor;
    private readonly IMultiplier _multiplier;
    private readonly IDivider _divider;

    public Calculator()
    {
        _adder = Container.Get<IAdder>();
        _subtractor = Container.Get<ISubtractor>();
        _multiplier = Container.Get<IMultiplier>();
        _divider = Container.Get<IDivider>();
    }
}

I'd like to maintain a list of pros and cons as the answers come in:

PROS

  • Clean constructors as types need not "pollute" their constructors with dependencies. It's not a big problem here because Calculator doesn't have anything in its constructor other than dependencies but imagine it did. E.g. public Calculator(Mode mode, bool UseDigitGrouping).
  • Client code doesn't break when a dependency's dependencies change.
  • Less code to maintain.

CONS:

  • Harder to change IoC container in the future.
  • It's not immediately clear what a type's dependencies are.
Thiru
  • 271
  • 2
  • 11
  • possible duplicate of [Dependency Injection vs Service Location](http://stackoverflow.com/questions/4985455/dependency-injection-vs-service-location) – Steven Jun 30 '12 at 19:12
  • related: http://stackoverflow.com/questions/1557781/whats-the-difference-between-the-dependency-injection-and-service-locator-patte – Steven Jun 30 '12 at 19:13
  • related: http://stackoverflow.com/questions/10356497/is-is-an-anti-pattern-to-inject-di-container-to-almost-each-class – Steven Jun 30 '12 at 19:13
  • 1
    source of truth: http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx – Steven Jun 30 '12 at 19:13
  • Thanks for this. I didn't realize that what I'm is the Service Locator pattern! I'm open to closing this question as others already cover the subject well. – Thiru Jun 30 '12 at 22:12

1 Answers1

1

Assuming I'm using an IoC container, why exactly must my types have all their dependencies injected? Why can't every type just assume that the container exists and just retrieve the dependencies it needs for itself? What are the disadvantages to this?

A couple of reasons I can think of, of which you mention one:

  • It's no longer apparent what dependencies your class has, if any
  • The class will assume a hidden dependency on the container, of the worst kind no less (assuming it either exists as a global or is accessible statically)

Your concern regarding how easy it is to adapt the code when a dependency's dependencies change is valid; that's a very strong argument for IoC (let the container do it for you automatically; as long as it's possible to satisfy all dependencies nothing will break).

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Geez people are fast on this site! – Thiru Jun 30 '12 at 17:18
  • @Thiru: I've discussed everything in my article here: http://www.codeproject.com/Articles/386164/Get-injected-into-the-world-of-inverted-dependenci – jgauffin Jul 01 '12 at 08:33
  • @jgauffin: Thanks for the link. You're article covers a lot but I'm not still completely sold on absolutely using dependency injection over service locator. I do understand the pros and cons but something still smells off to me. I should probably organize my thoughts into an article some day. – Thiru Jul 04 '12 at 01:17