> ## Content Index
> Fetch the complete content index at: https://www.binarysludge.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# node-di: a dependency injection framework for node.js
- URL: https://www.binarysludge.com/2013/07/25/node-di-a-dependency-injection-framework-for-node-js/
- Published: 2013-07-25T22:52:39.000Z
- Updated: 2013-07-25T22:52:39.000Z
- Author: Andrew Martin
- Tags: Clean Code, DI, Javascript, Programming, #Import 2026-10-22 12:56

[Uncle Bob](http://en.wikipedia.org/wiki/Robert%5FCecil%5FMartin?ref=binarysludge.com) is a proponent of inversion of control to counter what he defines as "bad design":

> There is one set of criteria that I think all engineers will agree with. A piece of software that fulﬁlls its requirements and yet exhibits any or all of the following three traits has a bad design. 
> 1. It is hard to change because every change affects too many other parts of the system. (Rigidity)
> 2. When you make a change, unexpected parts of the system break. (Fragility)
> 3. It is hard to reuse in another application because it cannot be disentangled from the current application. (Immobility)  
> ...  
> What is it that makes a design rigid, fragile and immobile? It is the interdependence of the modules within that design.

[source](https://docs.google.com/file/d/0BwhCYaYDn8EgMjdlMWIzNGUtZTQ0NC00ZjQ5LTkwYzQtZjRhMDRlNTQ3ZGMz/edit?hl=en&ref=binarysludge.com) 

One solution to standardise the interfaces of modules (or classes) is dependency injection. Vojta Jina has put together a powerful, lightweight [dependency injection library](https://github.com/vojtajina/node-di?ref=binarysludge.com) for node.js. Its example is testament to its simplicity:

var Car = function(engine) {
  this.start = function() {
    engine.start();
  };
};

var createPetrolEngine = function(power) {
  return {
    start: function() {
      console.log('Starting engine with ' + power + 'hp');
    }
  };
};

// a module is just a plain JavaScript object
// it is a recipe for the injector, how to instantiate stuff
var module = {
  // if an object asks for 'car', the injector will call 
  // new Car(...) to produce it
  'car': ['type', Car],
  // if an object asks for 'engine', the injector will call 
  // createPetrolEngine(...) to produce it
  'engine': ['factory', createPetrolEngine],
  // if an object asks for 'power', the injector will give it number 1184
  'power': ['value', 1184] // probably Bugatti Veyron
};

var di = require('di');
var injector = new di.Injector([module]);

injector.invoke(function(car) {
  car.start();
});

Martin Fowler has more to say on [the benefits of Dependency Injection](http://www.martinfowler.com/articles/injection.html?ref=binarysludge.com).