@kos-ng-it-konsult/service-locator
v0.0.4
Published
Service locator package for KOS NG IT Konsult
Downloads
6
Maintainers
Readme
@kos-ng-it-konsult/service-locator
This is a dependency injection package for KOS NG IT Konsult.
Quick Start
Install
$ npm i --save @kos-ng-it-konsult/service-locator
Basic Usage
const service_locator = require('@kos-ng-it-konsult/service-locator');
const adderFunc = function (num1, num2) {
return num1 + num2;
};
const multiplierFunc = function (num1, num2) {
return num1 * num2;
};
// Register service
service_locator.register('adder', function () {
return adderFunc;
});
service_locator.register('multiplier', function () {
return multiplierFunc;
});
// Count registered services
console.log(service_locator.countServices()); // return 2
// Invoke service
const invoked = service_locator.invoke('adder');
console.log(invoked(4, 9)); // returns 13;
// Bulk invoke services
const { adder, multiplier } = service_locator.bulkInvoke([ 'adder', 'multiplier' ]);
console.log(adder(3, 6)); // return 9
console.log(multiplier(6, 3)); // return 18
// Unregister service
service_locator.unregister('adder');
// Clear registered services
service_locator.clean();
console.log(service_locator.countServices()); // return 0
Running the Example
To run the example, simply run the command:$ node ./example/sample.js
Running Tests
Run$ npm test