containerr.js
v0.2.0
Published
A simple container for manage dependency injection.
Downloads
4
Maintainers
Readme
containerr.js
This library include a simple container for manage dependency injection
- Container accept functions as dependencies
- Function dependencies are singleton and are instantiated only when get it
Methods
/**
* Check if the dependence is in the container
*
* @param key
* @returns {boolean}
* @throws Error: The key must be a string
*/
has(key);
/**
* Get dependence
*
* @param key
* @returns {boolean}
* @throws Error: The key must be a string
* @throws Error: $key is not in the container
*/
get(key);
/**
* Load dependence
*
* @param key
* @param object
* @returns {boolean}
* @throws Error: The key must be a string
* @throws Error: $key is already in the container
*/
share(key, object);
Example
const { Containerr } = require('./containerr');
let container = new Containerr();
let dependence = {
name: 'dependence1'
};
let key = 'dependence1';
let key2 = 'dependence2';
let key3 = 'dependence2';
container.has(key); //false
container.share(key, dependence); //true
container.share(key2, () => {
return { obj: 'obj' };
}); //true
container.has(key); //true
container.get(key); //return dependence variable
container.get(key2); //return dependence variable
container.share(key, dependence); //Exception = Error: key is already in the container
container.get(key3); //Exception = Error: key is not in the container