js-di
This is a pretty simple DI-container for ES6.
Install
$ npm install simple-js-di
A very simple example
;; let di = ;di; // add FooClass as service named 'foo'const foo = di; // get an instance of FooClass
One more example
; bar = null; { thisbar = bar; } { return thisbargreeting + ', ' + thisbarname + '!'; } name = null; greeting = null; { thisgreeting = greeting; thisname = name; } let di = ;di;di;di;const foo = di; console; // Hello, World!
For more examples see https://github.com/pilov-pa/js-di/tree/master/examples
Api
add(name, className, dependencies, shared)
- addMulti(config)
resolve(name)
- remove(name)
- has(name)
- addParameters(parameters)
- addParameter(name, value)
- getParameter(parameterName)
- removeParameter(parameterName)
- hasParameter(parameterName)
add(name, value, dependencies, shared)
Using method Adds new dependency to the container.
Example
di;
or
di;
Method add()
has 5 arguments:
name
Argument This is an alias of the dependency that you should use to resolve it.
className
Argument This argument can be a class or a value. If it is a class then resolving returns the result of calls the class constructor.
args
Argument This argument is array of service dependencies should be pass to service constructor.
All dependencies should be registered in the di-container.
If dependency name has prefix @
then dependency is an another service.
If dependency name has prefix :
then dependency is a parameter.
Otherwise, the dependency is the passed value as is.
Default the empty array.
shared
Argument If this argument is false, each resolving will return a new instance of the class. If this argument is true, only the first resolving will create a new instance, the next calls will use the alredy created instance. Default true.
tags
Argument Tags list. See getByTag()
resolve(name)
Using method Resolving the dependency by the name. All subdependencies will be resolved automatically if necessary.
Method has only one argument: name.
getByTag(tag)
Using method Get all services tagged by tag