Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Since learning (and loving) automated testing I have found myself using the dependency injection pattern in almost every project. Is it always appropriate to use this pattern when working with automated testing? Are there any situations were you should avoid using dependency injection?

share|improve this question
Related (probably not duplicate) -… – Steve314 Feb 21 '12 at 19:25
Sounds a little bit like the Golden Hammer anti-pattern. – Cuga Oct 1 '13 at 19:38
up vote 61 down vote accepted

Basically, dependency injection makes some (usually but not always valid) assumptions about the nature of your objects. If those are wrong, DI may not be the best solution:

  • First, most basically, DI assumes that tight coupling of object implementations is ALWAYS bad. This is the essence of the Dependency Inversion Principle: "a dependency should never be made upon a concretion; only upon an abstraction".

    This closes the dependent object to change based on a change to the concrete implementation; a class depending upon ConsoleWriter specifically will need to change if output needs to go to a file instead, but if the class were dependent only on an IWriter exposing a Write() method, we can replace the ConsoleWriter currently being used with a FileWriter and our dependent class wouldn't know the difference (Liskhov Substitution Principle).

    However, a design can NEVER be closed to all types of change; if the design of the IWriter interface itself changes, to add a parameter to Write(), an extra code object (the IWriter interface) must now be changed, on top of the implementation object/method and its usage(s). If changes in the actual interface are more likely than changes to the implementation of said interface, loose coupling (and DI-ing loosely-coupled dependencies) can cause more problems than it solves.

  • Second, and corollary, DI assumes that the dependent class is NEVER a good place to create a dependency. This goes to the Single Responsibility Principle; if you have code which creates a dependency and also uses it, then there are two reasons the dependent class may have to change (a change to the usage OR the implementation), violating SRP.

    However, again, adding layers of indirection for DI can be a solution to a problem that doesn't exist; if it is logical to encapsulate logic in a dependency, but that logic is the only such implementation of a dependency, then it is more painful to code the loosely-coupled resolution of the dependency (injection, service location, factory) than it would be to just use new and forget about it.

  • Lastly, DI by its nature centralizes knowledge of all dependencies AND their implementations. This increases the number of references that the assembly which performs the injection must have, and in most cases does NOT reduce the number of references required by actual dependent classes' assemblies.

    SOMETHING, SOMEWHERE, must have knowledge of the dependent, the dependency interface, and the dependency implementation in order to "connect the dots" and satisfy that dependency. DI tends to place all that knowledge at a very high level, either in an IoC container, or in the code that creates "main" objects such as the main form or Controller which must hydrate (or provide factory methods for) the dependencies. This can put a lot of necessarily tightly-coupled code and a lot of assembly references at high levels of your app, which only needs this knowledge in order to "hide" it from the actual dependent classes (which from a very basic perspective is the best place to have this knowledge; where it's used).

    It also normally doesn't remove said references from lower down in code; a dependent must still reference the library containing the interface for its dependency, which is in one of three places:

    • all in a single "Interfaces" assembly that becomes very application-centric,
    • each one alongside the primary implementation(s), removing the advantage of not having to recompile dependents when dependencies change, or
    • one or two apiece in highly-cohesive assemblies, which bloats the assembly count, dramatically increases "full build" times and decreases application performance.

    All of this, again to solve a problem in places where there may be none.

share|improve this answer
SRP = single responsibility principle, for anyone else wondering. – Theodore Murdock Feb 22 '12 at 17:26
Yes, and LSP is the Liskov Substitution principle; given a dependency on A, the dependency should be able to be met by B which derives from A without any other change. – KeithS Feb 22 '12 at 17:29
Thanks for your detailed answer +1 and accepted – Tom Squires Feb 24 '12 at 17:15
the dependency injection specifically help where you need to get class A from class B from class D etc. in that case ( and only that case) the DI framework may generate less assembly bloat than poor's man injection. also I never had a bottleneck caused by DI.. think to maintenance cost, never to cpu cost because code can always be optimized , but doing that without a reason has a cost – DarioOO Dec 1 '14 at 23:05
Would another assumption be "if a dependancy changes it changes everywhere"? Or else you have to look at all consuming classes anyway – Richard Tingle Oct 25 '15 at 10:14

Outside of dependency-injection frameworks, dependency injection (via constructor injection or setter injection) is very nearly a zero-sum game: you decrease the coupling between object A and it's dependency B, but now any object that needs an instance of A must now also construct object B.

You've slightly reduced the coupling between A and B, but reduced A's encapsulation, and increased coupling between A and any class that must construct an instance of A, by coupling them to A's dependencies as well.

So dependency injection (without a framework) is about equally harmful as it is helpful.

The extra cost is often easily justifiable, however: if the client code knows more about how to construct the dependency than the object itself does, then dependency injection really does reduce coupling; for example, a Scanner doesn't know much about how to obtain or construct an input stream to parse input from, or what source the client code wants to parse input from, so constructor injection of an input stream is the obvious solution.

Testing is another justification, in order to be able to use mock dependencies. That should mean adding an extra constructor used for testing only that allows dependencies to be injected: if you instead change your constructors to always require dependencies to be injected, suddenly, you have to know about your dependencies' dependencies' dependencies in order to construct your direct dependencies, and you can't get any work done.

It can be helpful, but you should definitely ask yourself for each dependency, is the testing benefit worth the cost, and am I really going to want to mock this dependency while testing?

When a dependency-injection framework is added, and the construction of dependencies is delegated not to client code but instead to the framework, the cost/benefit analysis changes greatly.

In a dependency-injection framework, the burden of choosing the right dependencies typically returns to the programmer of the dependent class, by giving appropriate annotations to indicate where the dependency should be sourced from. The ability to circumvent those instructions when it's beneficial for testing is just gravy.

I don't see any reason to want to avoid dependency injection if using such a framework.

share|improve this answer
Just a quick comment about the dependency mocking in tests and "you have to know about your dependencies' dependencies' dependencies"... That's not true at all. One does not need to know or care about some concrete implementation's dependencies if a mock is being injected. One only needs to know about the direct dependencies of the class under test, which are satisfied by the mock(s). – Eric King Feb 21 '12 at 21:36
You misunderstand, I'm not talking about when you inject a mock, I'm talking about the real code. Consider class A with dependency B, which in turn has dependency C, which in turn has dependency D. Without DI, A constructs B, B constructs C, C constructs D. With construction injection, to construct A, you must first construct B, to construct B you must first construct C, and to construct C you must first construct D. So class A now has to know about D, the dependency of a dependency of a dependency, in order to construct B. This leads to excessive coupling. – Theodore Murdock Feb 22 '12 at 16:33
There wouldn't be so much cost to having an extra constructor used for testing only that allows the dependencies to be injected. I'll try to revise what I said. – Theodore Murdock Feb 22 '12 at 16:37
Ok, I misunderstood your point then, as I agree with you. I thought you were talking about the testing process itself. – Eric King Feb 22 '12 at 16:37
Is not new X() the traditional "annotation" for specifying which implementation of an interface you'd actually like to use. Why is a runtime checked annotation better than a compile time checked piece of java code. – Richard Tingle Oct 25 '15 at 10:20
  1. if you are creating database entities, you should rather have some factory class which you will inject instead to your controller,

  2. if you need to create primitive objects like ints or longs. Also you should create "by hand" most of the standard library objects like dates, guids, etc.

  3. if you would like to inject configuration strings it's probably better idea to inject some configuration objects (in general it is recommended to wrap simple types into meaningful objects: int temperatureInCelsiusDegrees -> CelciusDeegree temperature)

And don't use Service locator as dependency injection alternative, it's anti-pattern, more info:

share|improve this answer
All the points are about using different form of injection rather than not using it altogether. +1 for the link though. – Jan Hudec Sep 23 '13 at 15:02
Service Locator is NOT an anti-pattern and the guy who's blog you linked to is not an expert in patterns. That he got StackExchange-famous is kind of a disservice to software design. Martin Fowler (author of staple Patterns of Enterprise Application Architecture) has a more reasonable view on the matter: – Colin Apr 11 at 21:52

When you don't stand to gain anything by making your project maintainable and testable.

Seriously, I love IoC and DI in general, and I'd say that 98% of the time I will use that pattern without fail. It's especially important in a multi-user environment, where you code can be reused again and again by different team members and different projects, as it separates logic from implementation. Logging is a prime example of this, an ILog interface injected into a class is a thousand times more maintainable than simply plugging in your logging framework-du-jour, as you have no guarantee another project will use the same logging framework (if it uses one at all!).

However, there are times when it is not an applicable pattern. For example, functional entry points that are implemented in a static context by a non-overridable initialiser (WebMethods, I'm looking at you, but your Main() method in your Program class is another example) simply cannot have dependencies injected at initialisation time. I'd also go as far as to say that a prototype, or any throw-away investigative piece of code, is also a bad candidate; the benefits of DI are pretty much mid-to-long-term benefits (testability and maintainability), if you are certain that you will throw away the majority of a piece of code within a week or so I would say you gain nothing by isolating dependencies, just spend the time you'd normally spend testing and isolating dependencies getting the code working.

All in all, it's sensible to take a pragmatic approach to any methodology or pattern, as nothing is applicable 100% of the time.

One thing to note is your comment about automated testing: my definition of this is automated functional tests, for example scripted selenium tests if you are in a web context. These are generally completely black-box tests, with no need to know about the inner workings of the code. If you were referring to Unit- or Integration-tests I'd say that the DI pattern is almost always applicable to any project that heavily relies on that kind of white-box testing, since, for example, it allows you to test things like methods that touch the DB without any need for a DB to be present.

share|improve this answer
I don't understand what you mean about logging. Surely all loggers aren't going to conform to the same interface so you'd have to write your own wrapper to translate between this projects way of logging and a specific logger (assuming you wanted to be able to easily change loggers). So after that what does DI give you? – Richard Tingle Oct 25 '15 at 10:27
@RichardTingle I'd say that you should define your logging wrapper as an interface, and then you write an lightweight implementation of that interface for each external logging service, instead of using a single logging class that wraps multiple abstractions. It's the same concept you're proposing but abstracted at a different level. – Ed Woodcock Oct 28 '15 at 16:03

An alternative to Dependancy Injection is using a Service Locator. A Service Locator is easier to understand, debug, and makes constructing an object simpler especially if you aren't using a DI framework. Service Locators are a good pattern for managing external static dependancies, for instance a database that you would otherwise have to pass into every object in your data access layer.

When refactoring legacy code, it is often easier to refactor to a Service Locator than to Dependancy Injection. All you do is replace instantiations with service lookups and then fake out the service in your unit test.

However, there are some downsides to the Service Locator. Knowing the depandancies of a class is more difficult because the dependancies are hidden in the class's implementation, not in constructors or setters. And creating two objects that rely on different implementations of the same service is difficult or impossible.

TLDR: If your class has static dependancies or you are refactoring legacy code, a Service Locator is arguably better than DI.

share|improve this answer
The Service Locator hides dependencies in your code. It's not a good pattern to use. – Kyralessa Feb 20 '12 at 23:10
When it comes to static dependencies, I'd rather see facades slapped on them that implement interfaces. These can then be dependency injected, and they fall on the static grenade for you. – Erik Dietrich Feb 21 '12 at 0:26
@Kyralessa I agree, a Service Locator has many downsides and DI is almost always preferable. However, I believe there are a couple exceptions to the rule, as with all programming principles. – Garrett Hall Feb 21 '12 at 14:13
Service location's main accepted use is within a Strategy pattern, where a "strategy picker" class is given enough logic to find the strategy to use, and either hand it back or pass a call through to it. Even in this case, the strategy picker can be a facade for a factory method provided by an IoC container, which is given the same logic; the reason you'd break it out is to put logic where it belongs and not where it's most hidden. – KeithS Feb 21 '12 at 19:18
To quote Martin Fowler, "The choice between (DI and Service Locator) is less important than the principle of separating configuration from use". The idea that DI is ALWAYS better is hogwash. If a service is used globally, it's more cumbersome and brittle to use DI. Additionally, DI is less favorable for plugin architectures where implementations aren't known till runtime. Think how awkward it would be to always pass additional arguments to Debug.WriteLine() and the like! – Colin Apr 11 at 21:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.