0

What is the "essence" of dependency injection? Is it the idea of dynamically swapping out/in core logical/structural aspects of a program at runtime?

Traditionally, this is done in code via some DI container. For example, using Structure Map in C#:

container.For<ICacheService>().Use<InMemoryCache>();

However, I've written several programs which inject code via configuration, in XML. Something like:

<cacheServiceProvider use="My.Namespace.InMemoryCache, MyAssembly"/>

That config is iterated at startup, and the various classes are instantiated for use throughout the program.

Is it fair to say that this is a form of DI? "Dependency Injection by Configuration"? Or does DI require configuration in code and the use of some container system?

Are their other philosophical or conventional aspects required to say something is "dependency injected," or am I completely misunderstanding this?

  • 2
    If it had an official definition, it would probably be a lot less broad. I still have no idea if dependency injection includes simply passing arguments to constructors or if it only means that sort of configuration stuff which I assume gets translated into passing arguments to constructors. – Ixrec May 27 '16 at 11:45
  • What you are describing here is dependency inversion / inversion of control, rather than dependency injection. That latter is neatly summed up by @Julian Hayward's answer. – David Arno May 27 '16 at 12:51
  • @DavidArno Confusingly, inversion of control containers are a specific way of doing dependency injection, and dependency injection is a specific way of achieving inversion of control – Ben Aaronson May 27 '16 at 13:10
8

The essence is simply that given an object, you pass its dependencies to it rather than let it instantiate them itself.

How you achieve that, by containers, configuration, or any other means, is up to you.

  • Do you have "pass dependencies" to every object every time they're created, or can those dependencies exist in a global state? So, you pass dependencies to the entire program, and expose them globally in such a way that multiple parts of that program can use them. – Deane May 27 '16 at 11:51
  • @Deane suppose you have a class Foo { List<Bar> bars; Baz baz; ... } without DI the constructor is public Foo() { bars = new ArrayList<Bar>(); baz = new SpecificBaz(); } and therefore the exact concrete subtype of List and Bar and Baz is tied to Foo. DI is allowing any List / Bar / Baz. All the other 'DI' stuff is just things trying to make life easier in that paradigm. – Caleth May 27 '16 at 12:14
  • +1. to answer @Deane-s comment: Implementing “dependency injection” means that the code has an api to handle "“dependency injection” (via constructor parameter, properties, methods,....). The DI container is responsible to handle the "global state" for the "entire program" (i.e. as a singleton or "every time they're created") – k3b May 27 '16 at 12:17
  • 1
    Note that for a small example, this will seem like a massive overkill. It is, but the benefit is that it forces you the programmer to think about compartmentalizing your code. – Caleth May 27 '16 at 12:23
  • 3
    I think that the very notion of tying the concept of DI to big XML configs, arcane conventions and all-around heavy frameworks is "harmful". It'd be like saying that "MVC is a project template by microsoft that requires you to name all your controllers 'NameController'" – sara May 27 '16 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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