AsyncIoC for Node JS
Asyncronous IoC and dependency injection for Node JS.
Usage
First create a container:
var ioc = ;
Then register services on that container:
ioc;ioc;
You can specify dependencies for a service:
ioc;
Load an entire folder:
ioc;
...all registered services will be named by the file/folder unless the $implement attribute is set.
And start a service with dependency injection:
iocstart'serviceThree';
You can also chain the methods:
start'serviceThree' ;
Service Signature
The factory/function for the service should have the following signature:
var { // ... // service initialization logic // ... return theService;};
Typically you would expose this in a module:
module { };
... where services is an object containing the injected services
Attributes
$implement
Alias of $implements
$implements
Defines the service or interface name this module implements.
Type: String or Array[String]
Default: The file or folder name.
Notes: A module can implement multiple services or interfaces.
// implement a service or interfacemodule { };moduleexports$implements = 'serviceName';
// implement multiple services or interfacesmodule { };moduleexports$implements = 'service1' 'service2';
$inject
Defines the services to be injected for this service. These should be supplied as an array of string values containing the service names.
Default: No services ([])
moduleexports = myService;moduleexports$inject = 'dependencyOne' 'dependencyTwo'; { servicesdependencyOne;}
Asyncronous Services
Method 1: Promises
Simply return a promise from the service factory.
moduleexports = myService; { var deferred = Qdefer; // this service will finish loading in 1 second ; return deferredpromise;}
Method 2: start() method
Return an object that defines a .start()
method that accepts a Node-style callback (ie: function(err)
) to be called on completion. The start method will be called before injecting into the first service.
moduleexports = myService; { return { // if an error occurs, call done() with the error if someErrorOccurred ; // this service will finish loading in 1 second ; } ; }
Shutdown / Stop process
TODO:
Method Summary
All container methods are chainable, except for get
and start
.
Still To Document
A lot to do!
- debug
- define
- defineAll / defineFolder
- get
- logTo
- recurseServices
- recurseInterfaces
- register
- registerAll / registerFolder
- reset
- start
- stopOn
- stopTimeout