0.1.5 • Published 8 years ago
@nodeart/injector v0.1.5
Dependency Injection
This module provides dependency injection for browser and node.
There are five resolutionStrategies available:
factory- each time a new instance will be created.singleton- only one instance will be created.value- just will be returned.constant- the same asvaluebut can't be reassign.alias- used to create an alias for some dependency.
Basic usage:
const { Injector } = require('@nodeart/injector');
const injector = new Injector();
class X {
constructor(a, b) {
console.log(a, b);
}
static get $inject() {
return ['a', 'b'];
}
}
const depX = Injector.DIConfig.create({
name: 'X',
resolutionStrategy: 'factory',
value: X
});
const depA = Injector.DIConfig.create({
name: 'a',
resolutionStrategy: 'value',
value: 'A'
});
const depB = Injector.DIConfig.create({
name: 'b',
resolutionStrategy: 'constant',
value: 'B'
});
const depC = Injector.DIConfig.create({
name: 'c',
resolutionStrategy: 'singleton',
value: 'C'
});
const alias = Injector.DIConfig.create({
name: 'aliasForC',
resolutionStrategy: 'alias',
value: 'C' // actual dependency
});
injector.register(depX, depA, depC);
injector.resolve('X');
// A BNode.js usage
Alongside with Injector, InjectorNode class is available. It inherits from Injector and in addition to
main functionality it can search for modules inside file system.
It can be useful if you write a big node.js project and do not want to manage dependencies on your own.
Just call a bootstrap method with the list of folders or files you'd like to register and InjectorNode do everything
else for you. To be registered module must exports and instance of DependencyConfig class
(which is the same as calling Injector.DIConfig.create method over an object).
If you want a recursive search just pass true as a second argument to bootstrap method.
const { InjectorNode } = require('@nodeart/injector');
const injector = new InjectorNode();
injector.bootstrap([
'.',
'../dependencies',
'../dependency_file.js'
], true);