Skip to content
/ node-di Public
forked from vojtajina/node-di

Dependency Injection framework for Node.js

License

Notifications You must be signed in to change notification settings

btford/node-di

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dependency Injection framework for Node.js

Example

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();
});

For more examples, check out the tests. You can also check out Karma and its plugins for more complex examples.

Registering stuff

type(token, Constructor)

To produce the instance, Constructor will be called with new operator.

var module = {
  'engine': ['type', DieselEngine]
};

factory(token, factoryFn)

To produce the instance, factoryFn will be called (without any context) and its result will be used.

var module = {
  'engine': ['factory', createDieselEngine]
};

value(token, value)

Register the final value.

var module = {
  'power': ['value', 1184]
};

Annotation

The injector looks up tokens based on argument names:

var Car = function(engine, license) {
  // will inject objects bound to 'engine' and 'license' tokens
};

You can also use comments:

var Car = function(/* engine */ e, /* x._weird */ x) {
  // will inject objects bound to 'engine' and 'x._weird' tokens
};

Sometimes it is helpful to inject only a specific property of some object:

var Engine = function(/* config.engine.power */ power) {
  // will inject 1184 (config.engine.power),
  // assuming there is no direct binding for 'config.engine.power' token
};

var module = {
  'config': ['value', {engine: {power: 1184}, other : {}}]
};

Differences to Angular's DI

  • no config/runtime phases (configuration happens by injecting a config object)
  • no global module register
  • no array annotations (comments annotations instead)
  • comment annotation
  • no decorators (maybe not yet?)
  • service -> type
  • child injectors

Made for Karma. Heavily influenced by AngularJS. Also inspired by Guice and Pico Container.

About

Dependency Injection framework for Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published