tiny-ioc
A really simple and lightweight ioc container built in nodejs
Installation
$ npm install tiny-ioc
How to use tiny-ioc
Basic usage:
You register your dependency in the container with a unique name. If you use the same name twice, tiny-ioc will throw an exception.
var ioccontainer = ; var { thisname = "my name is ronald" }ioccontainer; var resolvedobject = ioccontainer;console; ==> outputs: "my name is ronald"
Nested Dependencies:
When resolving objects that have dependencies on other services, tiny-ioc will auto resolve them by default for you by mapping the name of the parameters.
var ioccontainer = ; var { this { console; } }var { this { logger; }} ioccontainer;ioccontainer; var resolvedobject = ioccontainer;resolvedobject ==> outputs: "something happened!"
Singletons:
Tiny-ioc can also register your objects as singletons.
example:
var ioccontainer = ; var { thisname = '';} ioccontainer; var resolvedobject = ioccontainer;resolvedobjectname = "Ronald"; var secondResolvedObject = ioccontainer;console
Resolving dependencies without registration:
If you have an object that has dependencies on other services, you can always let tiny-ioc inject those for you, even if the your object is not registered in the container.
var ioccontainer = ; var { this { console; } } ioccontainer; var { this { logger; }} var myobjectinstance = ioccontainer;resolvedobject ==> outputs: "something happened!"
General
By default tiny-ioc will try to find all nested dependencies of a resolved object and will throw an error if it cannot find one. You can bypass this behavior when registering your object.
example:
var ioccontainer = ; var { // some inner logic } ioccontainer; var myobjectinstance = ioccontainer;==> does not throw