2

I usually create my javascript "controllers" in the following way:

var module = (function(){

    function init(someService){
        someService.doSomething();
    }

    return {
        init: init
    };

})();
module.init(SomeService);

I've just stumbled upon dependency injetion in javascript (e.g. JavaScript Dependency Injection).

What I would like to know is, from a testing point of view, is there any advantage using the Injector in my link to inject mocks and such, versus simply passing them do the init function like I do above.

To elaborate, I could just pass a SomeService mock when I initialize my tests today. So is there any point for me to use the Injector or something similar?

Community
  • 1
  • 1
filur
  • 2,116
  • 6
  • 24
  • 47

1 Answers1

4

You're already doing Dependency Injection. Instead of initializing someService in the module, you're passing it into the init method. That's exactly what Dependency Injection is about!

One of these advantages is the easiness of testing, as you can just inject a mock of someService (as you already said).

The use of an injector/dependency injection container is to manage all these dependencies. Imagine having more modules, all having their dependencies. It would soon become a nightmare to manage the initialization of all these classes.

That's were the container steps in and makes DI a joy again.

So to answer your question, if this is all the code you have, there is no point in needing an injector/container.


// the Module object
var Module = function (someService) {
    this.someService = someService;
};
Module.prototype.do = function () {
    this.someService.doSomething();
};

// configuring the injector
Injector.add('someService', new SomeService());

// retrieving the module instance with the SomeService being injected
var module = Injector.get(Module);

module.do();

More examples can be seen on http://www.yusufaytas.com/dependency-injection-in-javascript/

Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • I agree with Wouter J, you're already doing dependency injection. I think the Test problematic you're talking about is more about how / which files you load. If you have a module loader (such as requirejs, browserify or webpack ...), you could shim some modules in test mode so that they would be replaced. If you don't have a module loader, you can can simply switch the script tags you add in test mode. – topheman May 22 '15 at 22:30
  • Thanks for clarifying! This is obviously just a dummy example. Would you mind showing me how to use the Injector in my example? – filur May 22 '15 at 22:31
  • 1
    @filur added an example – Wouter J May 22 '15 at 22:36
  • Totally agree, dependency injection is just a technique to keep your dependencies visible and your codebase (and mind) sane. It's perfectly fine to inject the dependencies manually like you're doing right now (this is called "poor man's DI" but is a valid pattern nonetheless). If managing these dependencies becomes painful (e.g. when SomeService in turn accepts a dependency, etc.) you might consider using an IOC container that does the heavy lifting for you. A very good post on this topic (by the author of "Dependency Injection in .NET"): http://blog.ploeh.dk/2012/11/06/WhentouseaDIContainer/ – prgmtc May 23 '15 at 18:02