node-ioc
v2.1.0
Published
a simple ioc container
Downloads
396
Maintainers
Readme
NODE-IOC - Inversion of control
Example
A container is a object which holds the relationships between various components and their life cycle. Create a container as follows ~
var Container = require('node-ioc')
var container = new Container()
Component Registration
Once a container
is created we must register all the components and their factories. Registration is an important part of dependency injection and should all be at one place. So whenever there is a change in the component dependencies, or your container itself, you can just change it here.
API
registerInstance
: Registers a value for a class. So whenever that class is being resolved, that value would be provided instead.class jQuery {} container.registerInstance(window.$).as(jQuery) container.resolve(jQuery) === window.$ // true
registerType
: This would be the most common use case, where a single class depends on multiple classes. In the following case if you try to resolveSampleClass0
, the container will instantiateSampleClass1
andSampleClass2
and then provide their instances to the constructor ofSampleClass0
.class SampleClass0{ constructor (sc1, sc2){ this.sc1 = sc1 this.sc2 = sc2 } } class SampleClass1{} class SampleClass2{} container.registerType(SampleClass1, SampleClass2).as(SampleClass0) const sc0 = container.resolve(SampleClass0) sc0.sc1 instanceof SampleClass1 // true sc0.sc2 instanceof SampleClass2 // true
register
: This allows you to create a custom factory. SometimesregisterType
andregisterInstance
are just not good enough. For instance —class CurrentDate {} class SampleClass { constructor (date) { console.log(date) } } container.register(() => new Date()).as(CurrentDate) container.registerType(CurrentDate).as(SampleClass) container.resolve(SampleClass) // Sat Jan 30 2016 12:57:29 GMT+0530 (IST)
The
register
function's first param is the current instance of the container.singleton
: This implements the standard singleton pattern.class SampleClass {} container.register(new SampleClass()).as(SampleClass).singleton() const s0 = container.resolve(SampleClass) const s1 = container.resolve(SampleClass) s0 === s1 // true
Salient Features
- Detection for cyclic dependency
- Support for Container as a dependency using
registerWithTypes
class SampleClass {}
container.registerWithTypes(Container).as(SampleClass)
In this case the current instance of Container
will be passed as the first argument to the constructor of the SampleClass