node-service-locator
service locator for node js
Allow to load instanse of service with injected dependency of other service. Service dependencies are described in declarative way. Locator is a singletone so you can register services ones and then use it everywhere in your app
Install
npm install node-service-locator
Usage
Simple Services
Lets assume that we have a folder 'Services' in project and and file Logger with logger service in it.
// Services/Logger.js exports { console;};
Service description example
// services.js file moduleexports = "logger": "path": "Services/Logger"
Usage Example
var serviceList = ; var basePath = __dirname; var locator = ;locator; //load simple objectvar logger = locator;
Servise as a constructor function
// Services/ClassLogger.js var { thislevel = errorLevel || "debug";}; ClassLoggerprototype { console;}; moduleexports = ClassLogger;
In order to return from locator an instance of this service you should add instantiate and parameters keys to service description
"warningLogger": "path": "Services/ClassLogger" "instantiate": true "params": "warning"
And than in you app:
var warningLogger = locator; warningLogger;
Also you have possibility to pass one service as a parameter to anather to do this add '@' to service name in the params field
"ClassWithWarningLogger": "path": "Services/ClassWithWarningLogger" "instantiate": true "params": "foo" "@warningLogger" 23
Also you can register some services on th fly
locator; console;// => foo
Invoke some function with services as parameters
locator; locator; // => foo
LICENSE
More examples in example folder