- Prefix by dependency injector name
- Fetch dependency injector instance
- Everything is case insensitive
diFactory( name:string [, imports:di|di[] ] ):di
Create a new dependency injector. The name is used in error messages, import() and showDependencies().
name {string}
: Name of the dependency injector[imports] {di|di[]}
: Existing dependency injectors to import
{di}
: a new dependency injector
var diFactory = require('sod-di');
var app = diFactory('app');
var service = diFactory('service', [app]);
mapInvoke( dis:di[], fn:Function ):*[]
Call di.invoke(fn)
on every dependency injector inside dis
and return array of results.
dis {di[]}
: Array of dependency injectorsfn {function}
: Function to invoke
{*[]}
: The return values of each fn
invokation
var diFactory = require('sod-di');
var app1 = diFactory('app1');
var app2 = diFactory('app2');
app1.register('value').value('app 1 value');
app2.register('value').value('app 2 value');
var result = diFactory.mapInvoke([app1, app2], function(di, value) {
return di.diName + ' :: ' + value;
});
console.log(result); // stdout: [ 'app1 :: app 1 value', 'app2 :: app 2 value' ]
di.invoke( fn:function [, custom:object ] [, onError: function(error) ] )
or
di( fn:function [, custom:object ] [, onError: function(error) ] )
Inject dependencies on fn
.
fn {function}
: The function to inject the dependencies on[custom] {object}
: Provide custom dependencies[onError] {function(error)}
: Error callback in case this methods triggers an error. If onError is missing, the error will be thrown
{*}
: The return value of fn
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('value').value('my value');
function myMethod(value) {
console.log(value);
}
app(myMethod); // stdout: 'my value'
app.invoke(myMethod); // stdout: 'my value'
app(myMethod, { value: 'custom value' }); // stdout: 'custom value'
file( file:string|string[] [, custom:Object ] [, onError:function(err) ] ):*
Does require(file)
and calls invoke()
if file returns a function. Adds file
to error message on error.
file {string|string[]}
: Absolute path/to/file to require(). If file === string[], then file() adds the paths together withpath.join(...file)
[custom] {object}
: Provide custom dependencies[onError] {function(error)}
: Error callback in case this methods triggers an error. If onError is missing, the error will be thrown
{*}
: The return value of function from file
var diFactory = require('sod-di');
var app = diFactory('app');
app.file('/my/function.js');
callback( fn:Function [, custom:Object ] [, onError:function(err) ] ):function
Creates a function, that calls invoke() upon its execution.
fn {function}
: The function to inject the dependencies on[custom] {object}
: Provide custom dependencies[onError] {function(error)}
: Error callback in case this methods triggers an error. If onError is missing, the error will be thrown
{function}
: The callback function
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('value').value('my value');
var callback = app.callback(function(value) {
console.log(value);
});
setTimeout(callback, 500); // stdout after 500ms: 'my value'
register( name:string [, onError:function(err) ] ):Dependency
Register a dependency.
name {string}
: Name of the dependency[onError] {function(error)}
: Error callback in case an error triggers in the register chain. If onError is missing, the error will be thrown
{Dependency}
: Chainable instance of Dependency to modify dependency value
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('value').value('my value');
app(function(value) {
console.log(value); // stdout: 'my value'
});
// error handling
app.register('value', function(error) {
console.log(error); // COULD_NOT_REQUIRE error
}).file('this/file/does/not/exist');
get( name:string [, publicOnly:boolean] [, onError:function(err) ] ):*|null
Get a single dependency.
name {string}
: Name of the dependency[publicOnly=false] {boolean}
: Only look for dependencies that aresetPublic(name)
. Is true for every imported dependency injector[onError] {function(error)}
: Error callback in case this methods triggers an error. If onError is missing, the error will be thrown. An error can happen ifname
is a factory and this factory requests dependencies that do not exist.
{*|null}
: Dependency value or null if dependency was not found
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('value').value('my value');
app.register('myFactory').factory(function(doesNotExist) {});
app.get('value'); // 'my value'
app.get('doesNotExist'); // null
app.get('myFactory'); // throws `new diFactory.Error` as 'myFactory' requests a dependency, that does not exist
require( name:string [, onError:function(err) ] ):*
Get a single dependency. Throw an error if dependency is not available.
name {string}
: Name of the dependency[onError] {function(error)}
: Error callback in case this methods triggers an error. If onError is missing, the error will be thrown
{*}
: Dependency value
var diFactory = require('sod-di');
var app = diFactory('app');
app.require('value'); // throws `new diFactory.Error` as 'value' does not exist
import( imports:di|di[] ):di
Import one or many existing dependency injectors.
imports {di|di[]}
: Dependency injector(s)
{di}
: self
var diFactory = require('sod-di');
var app = diFactory('app');
var service = diFactory('service');
app.register('public').value('my public value').public();
service.import(app);
service.get('public'); // 'my public value'
newChild( name:string [, imports:di|di[] ] ):di
Create a new dependency injector and add self & self.imports as imports of the new instance.
name {string}
: Name of the dependency injector[imports] {di|di[]}
: Existing dependency injectors to import
{di}
: a new dependency injector
var diFactory = require('sod-di');
var app = diFactory('app');
var service = app.newChild('service');
app.register('public').value('my public value').public();
service.get('public'); // 'my public value'
showDependencies( [ fn:function(output) ] [, inherited:boolean ] [, processed:Object ] )
List all private and public dependencies of dependency injector and all public ones from the imported injectors.
[fn] {function(output)=console.log}
: returns output tofn
. Iffn
is missing, the output is being printed to console.log[inherited] {boolean=false}
: used internally for recursion[processed] {object}
: used internally to prevent circular recursion
{undefined}
You receive the chainable dependency object by calling register()
value( value:* ):Dependency
Register a value.
value {*}
{Dependency}
: self
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('value').value(1);
app.register('logger').value(console.log.bind(console));
app(function(logger, value) {
logger(value); // stdout: 1
});
factory( fn:function ):Dependency
Register a function that becomes lazy invoked upon first retrieval. The return value of fn
will be used as dependency value.
fn {function}
: Factory function
{Dependency}
: self
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('value').value(1);
app.register('logger').value(console.log.bind(console));
// a factory function is called once on first injection and can request dependencies itself
app.register('result').factory(function(value) {
return value + 1;
});
app(function(logger, value, result) {
logger(value, result); // stdout: 1, 2
});
file( file:string|string[] ):di
Does require(file)
and calls factory()
if file returns a function and value()
otherwise. Adds file
to error message on error.
file {string|string[]}
: Absolute path/to/file. If file === string[], then file() adds the paths together withpath.join(...file)
{Dependency}
: self
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('value').file('/my/value.js');
app(function(value) {
console.log(value);
});
fileValue( file:string|string[] ):di
Does require(file)
and calls value()
. Shows filename in error message on error.
file {string|string[]}
: Absolute path/to/file. If file === string[], then file() adds the paths together withpath.join(...file)
{Dependency}
: self
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('value').fileValue('/my/value.js');
fileFactory( file:string|string[] ):di
Does require(file)
and calls factory()
. Shows filename in error message on error.
file {string|string[]}
: Absolute path/to/file. If file === string[], then file() adds the paths together withpath.join(...file)
{Dependency}
: self
var diFactory = require('sod-di');
var app = diFactory('app');
app.register('factory').fileFactory('/my/factory.js');
get( ):*|null
Returns value of current dependency. By default, factory() and fileFactory() lazy invoke on first use. You can enforce its evaluation by calling this method directly after registration.
none
{*|null}
: value
var diFactory = require('sod-di');
var app = diFactory('app');
var value = app.register('factory').fileFactory('/my/factory.js').get();
public( ):Dependency
Set the dependency to public. This value will we visible if its being requested as imported dependency injector.
none
{Dependency}
: self
var diFactory = require('sod-di');
var app = diFactory('app');
var service = app.newChild('service');
app.register('public').value('my public value').public();
service.get('public'); // 'my public value'
You can target a specific dependency injector by prefixing the dependency
var diFactory = require('sod-di');
var app = diFactory('app');
var service = app.newChild('service');
app.register('value').value('my app value').public();
service.register('value').value('my service value');
service(function(value, appValue, serviceValue) {
console.log(value); // 'my service value'
console.log(appValue); // 'my app value'
console.log(serviceValue); // 'my service value'
});
Every dependency injector adds itself as "di" and "[name]di"
var diFactory = require('sod-di');
var app = diFactory('app');
app(function(di) {
console.log(di === app); // stdout: true
});
Every dependency is case insensitive. Uppercase letters may be used for readability.
var diFactory = require('sod-di');
var app = diFactory('aPP');
app.register('valUE').value('my app value');
service(function(valUE, aPPvalUE, value, appValue, appvalue) {
console.log(valUE); // stdout: 'my app value'
console.log(aPPvalUE); // stdout: 'my app value'
console.log(value); // stdout: 'my app value'
console.log(appValue); // stdout: 'my app value'
console.log(appvalue); // stdout: 'my app value'
});