1

TL;DR Version

Can you explain the concept of dependency injection to an 'enthusiast programmer' with a fundamental understanding of programming. i.e. Classes, functions and variables.

What is the purpose of dependency injection, is it purely a readability / ease of programming concept or does it provide compile time benefits also?

My original more waffly version!

My coding skills are reasonably rudimentary. (it isn't my primary skill, but it does sometimes come in handy to proof of concept something) I can hack stuff together and make things work, but I'm always perfectly aware that there are a host of better / more efficient ways of doing things.

Primarily I bounce things around between functions and classes and variables! (like what I learnt on my c64 a long time ago!)

Dependency injection seems to be everywhere lately, and while I think I kind of get it, I feel like I'm missing a point (or something)

The problem seems to be when I try to google around to understand what it is at a fundamental level, it very quickly gets very complicated and my head hurts (I'm a bear of very small brain and big words confuse me!)

So I'm hoping someone can explain dependency injection to a five year old! What is it, why do I need it, how does it make my coding better. (ideally working in concepts of functions, classes and variables!)

This is largely language independent, it seems to be a thing that all languages use, but my language of choice is usually C# (typically ASP/MVC though some native Windows / Console) though I've recently started poking around with Nodejs also.

Actually it seems this is a duplicate of this question - What is dependency injection?

(which fared much better than my version! - that's what I get for waffling)

Community
  • 1
  • 1
Michael B
  • 11,887
  • 6
  • 38
  • 74
  • 2
    Code injection or _dependency_ injection? Either way your question may be off-topic for SO because it's not about _"[a specific programming problem](http://stackoverflow.com/help/on-topic)"_ but rather a dictionary definition. –  Dec 10 '15 at 09:12
  • I think I have always kind of merged the two together in my thoughts about (I'm aware that they're not! Its just that I don't understand either so they blend together in a synergy of misunderstanding!) – Michael B Dec 10 '15 at 09:15
  • @Micky my thought was that it would come under 'a practical, answerable problem that is unique to software development' Is there a better site to ask such a question on? – Michael B Dec 10 '15 at 09:17
  • @nikosm thank you, yes I am actually taking about dependency injection, I will update the question (if it starts open long enough) ;) – Michael B Dec 10 '15 at 09:36
  • @JamieEltringham see edit ;) changing the tag found it for me! – Michael B Dec 10 '15 at 10:57

2 Answers2

1

Dependency Injection allows the developer to put a class reference into a project a will without having to create it as an object.

In the case of spring, where I have most knowledge of DI, I would like the classpath in a config.xml file and I can pass this class reference to any class were I need it to be called. The class being injected is like a singleton as only that one reference is being passed without declaring it as a singleton. It is usually a service class that is injected.

Dependency injection allows a client the flexibility of being configurable. Only the client's behaviour is fixed. This can save time swapping out what is to be injected in an xml file without the need to recompile the project.

This is why I say it is more like a singleton using only one instance

    public class Foo {    
        // Constructor
        public Foo() {
            // Specify a specific implementation in the constructor instead of using dependency injection
            Service service1 = new ServiceExample();
        }

        private void RandomMethod() {
            Service service2 = new ServiceExample();
        }
    }

Here the one service is used twice because two instances are created. I have seen projects where class files have become so big where a service class was created three times through out the one class in different methods.

    public class Foo {
        // Internal reference to the service
        private Service service1;

        // Constructor
        public Foo(Service service1) {
            this.service1 = service1;
        }
    } 

The same issuse can be created in the second example but by having all dependencies in the constructor, it makes it a little more obvious to the developer looking at the code what is being used and that the service has already been created from the start.

Tomaltach
  • 913
  • 1
  • 11
  • 30
  • dependency injection is not only for class/object references, it can be for strings, templates, configuration parameters services, or other parts of code – Nikos M. Dec 10 '15 at 15:45
0

Code Injection may have various meanings depending on context.

For example, in a security context it can mean malicious code being injected into your application (eg. sql-injection).

In other contexts (e.g aspect-oriented programming) it might mean a way to patch a method with additional code for an aspect.

Dependency Injection is something different and means a way for one part of code (e.g a class) to have access to dependencies (other parts of code, e.g other classes, it depends upon) in a modular way without them being hardcoded (so they can change or be overriden freely, or even be loaded at another time, as needed)

Nikos M.
  • 8,033
  • 4
  • 36
  • 43