2.1.1 • Published 5 years ago

tiny-di v2.1.1

Weekly downloads
10
License
MIT
Repository
github
Last release
5 years ago

tiny-di Build Status Coverage Status

npm

A tini tiny dependency injection container for node.js/io.js

example

// main.js
const tiny = require('tiny-di')();

// `require` the module now and bind it to `app`
tiny.bind('app').load('lib/app');

const something = require('something');
something.setup();

// bind an existing object to `something`
tiny.bind('something').to(something);

// layz bind
// this module is loaded as soon as somebody requests the binding `another`
tiny.bind('another').lazy('v1/another');

// use namespaces
tiny.ns('some/ns').to('./some/local/dir/');

 // returns a promise with content of require('./some/local/dir/foo')
 tiny.get('some/ns/foo');

 // returns contents of require('./some/local/dir/foo')
 // CARE: throws exception if some/ns/foo or a dependency requires a resolved injection
 tiny.getSync('some/ns/foo');


// use custom providers
tiny.provide('Something').by(somethingProvider);

// this function is called whenever a module requires `Somehing`
function somethingProvider(env, injector) {
  return 'I was required by ' + env.binding;
}

// tiny-di comes with built-in resolver which tries
// to cover basic use cases.
// if you need a more advanced resolving of your deps, you can
// set a custom resolver
// have a look at the built-in resolver before doing this!
tiny.setResolver(function(file) {
  return require(file);
});


...
// module
module.exports = Module;

Module.$inject = ['app', 'something', 'another'];
function Module(app, something, another) {

  // use the constructor pattern for your modules/libs/classes..
  if (!(this instanceof Module)) {
    return new Module(app, something, another);
  }

  var self = this;

  // app, something and another are injected :)
  ...
}

Advanced Injector configuration

// module
module.exports = Module;

Module.$inject = ['app', 'something', 'another'];
Module.$type = 'class';
function Module(app, something, another) {
  // callAs='class' tells the injector to instantiate a new class
  // now you don't need to use the constructor pattern. yah!

  var self = this;

  // (this instanceof Module) -> true!
}

Support async dependencies

Something.$inject = [['one', { resolve: true }], 'two', 'three'];
Something.$type = 'function'; // 'class'
function Something(one, two, three) {
  // the 'one' dependency is resolved before passed to Something
  console.log(one);
}

examples

look at the example-folder for a simple howto

develop

npm install
npm run watch

tests

Test coverage is quite good at the moment (see badge above).

npm install
npm test

license

MIT

3.0.0-beta8

5 years ago

2.1.1

5 years ago

3.0.0-beta7

5 years ago

3.0.0-beta6

5 years ago

3.0.0-beta5

5 years ago

3.0.0-beta3

5 years ago

3.0.0-beta2

5 years ago

3.0.0-beta1

5 years ago

3.0.0-beta0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.2

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.4.3

7 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago