Promise based service container for dependency injection.
npm install async-service-container
- Node.js v7.6.0 or higher
const { ServiceContainer } = require('async-service-container');
const container = new ServiceContainer();
class MyService {}
container.register([
{
name: 'myService',
service: new MyService(),
},
]);
Create a new service container.
property
The name of the injector property. Default:services
symbol
services
Array of services to register. Each array element has the following properties:name
The name to register the service with.service
The actual service object.options
Optional. Options given to the service'sinit(..)
method.
Returns a promise which gets resolved when all the services have been initialized. A rejected promise is returned if any service initialization returns a rejected promise.
Deinitialize all services in the container.
Returns a promise which gets resolved when all the services have been deinitialized. A rejected promise is returned if any service deinitialization returns a rejected promise.
Get a service with the given name.
Releases the services from this service's dependencies.
Injects the services into the given object.
Get a promise from the container with the given name.
Rejected promise returned for an invalid name.
Set a promise into the container with the given name.
Throws for an invalid name and promise.
Returns dependencies for service registered as name
in an array.
Creates an injector into the given object.
Returns the created injector.