dip - Dependency Injector w/ Promise Support
What is it?
dip
provides dependency injection with automatic promise resolution, parent/child relationships and factory dependencies
It's very similiar to angular's dependency injector, but works much better with standalone functions and on-the-fly dependencies (for example if you wanted to register a 'request' dependency that resolves to the current http request)
For now unresolved dependencies will resolve to undefined
Installation
npm install --save dip
Examples
Creating a dependency injector and a function that has dependencies
var DI = var di = 'foo': 'Hello' 'bar': 'World' { return foo + ', ' + bar + '!'}
Basic function call
di
Promises
didi
Factories
// Factories can return promises, factories can even be promises themselves (that resolve to functions)di di
Parent/Child injectors
var child = dichild
Mongoose/Express Example with Promises/Factories
di di app // Another methodapp // Without using injectapp
Manually specifying dependencies
DI// When you specify dependencies you can pass additional arguments, only specified dependencies will be resolved// returns a promise for the resultdi
Creating a resolved function (returns a promise)
var fn = di
Getting an resolved function (returns the return value)
di
Manually resolving an array of dependencies
// returns an array of dependencies either from the parameter names or specified manually via DI.inject(fn, arrayOfDependencies)var deps = DI // ['foo', 'bar']// returns the parameter namesvar deps = DI// or just specify your own arrayvar deps = 'foo' 'bar'di
For more information read the docs (./docs/index.html) or look at the tests (./tests)