tokioc
v0.0.2
Published
Another simple IOC
Downloads
1
Readme
#Tokioc ##A very simple IOC container for NodeJS
###How to use ####Install tokioc npm install tokioc ####Get Tokioc container instance
var ioc = require('tokioc');
####Registration
Tokioc supports the registration of objects and constructors.
#####Object registration
var target = {
propA: 'propA'
};
ioc.register('objByNameSingleInstance', target);
#####Constructor registration
var Target = function () {
var self = this;
self.now = new Date();
};
ioc.register('objByNameCtor', Target);
####Customizable lifetime manager Tokioc comes by default with a simple instance manager which caches resolved instances. Lifetime managers can be customized for different purposes. Lifetime managers must support setInstance and getInstance methods.
/**
* Container lifetime manager
*/
var ContainerLifetimeResolutionManager = function () {
var _instances = {};
function setInstance(name, target) {
_instances[name] = target;
}
/**
* Gets instance by name
*/
function getInstance(name) {
return _instances[name];
}
this.setInstance = setInstance;
this.getInstance = getInstance;
};
Register a dependency with a custom lifetime manager:
ioc.register('objByNameCtor', Target, new CustomLifetimeManager());
####Dependencies
Dependencies can only be declared on constructor registrations. Declare dependencies this way:
var Obj1 = function (obj2) {
var self = this;
self.obj2 = obj2;
};
var Obj2 = function () {
var self = this;
};
ioc.register('objByNameCtor', Obj1);
ioc.register('objByNameCtor2', Obj2);
Obj1.$dependencies = ['objByNameCtor2'];
Keep in mind that the constructor dependencies are applied in the order of the $dependencies array. ####Resolution
Resolve using the resolve method:
ioc.resolve('objByRef5', function (instance) { /* Do what you will with the resolution */ });