kioc
v1.0.4
Published
The simplest IOC container you will find for node. It let's you do the work of inventing patterns on top of it.
Downloads
5
Readme
kioc
The simplest IOC container you will find for node.
It let's you do the work of inventing patterns on top of it.
Installation
npm install --save kioc
Methods
.set(key, value)
Registers a value on the container.
.use(component, singleton=false)
Registers a component on the container.
A component should be an object (of any kind) that contains the following properties:
- attachKey The key wherewith you later retrieve the component from the container.
- attach A function that returns the resolved component
To this function, the container instance gets passed in as the first argument, from which you can take dependencies for the component.
.has(key)
Check for the existence of a value or component on the container.
.get(key)
Retrieve a value or component from the container.
.setup()
Call setup
on every registered component (only if available).
You can call this after registering all components, and give the components a chance to setup necesarry stuff.
The return value of this method is an array of results from the calls to the setup methods.
Example
Examples can be found in the examples/
folder.
To run the examples, execute node examples/main.js
from this project's root.
Read the ... source!
It's so simple, you might as well read the source to see how it works.
'use strict';
/**
* A super simple IOC container.
*
* @class Container
* @param {Object} props
*/
function Container(props) {
// add props passed in via the constructor to "this"
Object.keys(props || {}).forEach(function (key) {
this.set(key, props[key]);
}.bind(this));
// initialize the component map
this.components = {};
}
/**
* Register a component in the container.
*
* @param {*} component
*/
Container.prototype.use = function (component) {
this.components[component.attachKey] = component;
};
/**
* Set any key on the container.
*
* @param {String} key
* @param {*} value
*/
Container.prototype.set = function (key, value) {
this[key] = value;
};
/**
* Check if key is registered in the container
*
* @param {String} key
*/
Container.prototype.has = function (key) {
return this.hasOwnProperty(key) || this.components.hasOwnProperty(key);
};
/**
* Get a component from the container.
*
* @param {String} key
* @returns {*} The instatiated component
*/
Container.prototype.get = function (key) {
// let the developer know what he is looking for is not available
if ( ! this.has(key)) {
throw new Error(key + ' component not available on the container!');
}
var component = this.components[key];
return component ? component.attach(this) : this[key];
};
/**
* Call setup (if available) on every registered component.
*
* @return {Array} An array of the setup call results
*/
Container.prototype.setup = function () {
var app = this;
var results = [];
Object.keys(this.components).forEach(function (key) {
var component = app.components[key];
if (component.setup) {
results.push(component.setup(app));
}
});
return results;
};
module.exports = Container;