bmax-service-containers
JavaScript app service containers
Description
Container classes for storing services as Singleton or as Factory.
- Singletons are instanced once during the 'set' method.
- Factories are instanced during the 'get' method.
The package provides the following service containers:
ServiceContainer
- Local service container
- Must be instanced with
new ServiceContainer()
- Musst be passed down via e.g. the react context api or a express middleware
GlobalServiceContainer
- Global service container
- Instances itself once and is available over the whole application
- Just needs to be imported
- Disadvantage: Can override itself in a microservice architecture
- If this is used, it is recommended to namespace your service names
Installation
$ npm i -S bmax-service-containers
Usage
The following section is showing the usage of GlobalServiceContainer
. The ServiceContainer
can also be used like this but it needs to be instanced first.
To add a service:
;; // Namespace the serviceconst MY_SERVICE = 'MyApp.MyService'; // The service will be set as singleton by defaultGlobalServiceContainer; // To set a service as factoryGlobalServiceContainer;
The set method expects 2-3 arguments: GlobalServiceContainer.set(servceName: string, ServiceClass, serviceType: string = 'singleton');
Every argiment after the third is passed to the constructor of the service class.
To get a service:
; // Namespace the serviceconst MY_SERVICE = 'MyApp.MyService'; // The service will be set as singleton by defaultconst theService = GlobalServiceContainer; // If the service is a factoryGlobalServiceContainer;
The set method expects 1 arguments GlobalServiceContainer.get(servceName: string);
Every argiment after the first is passed to the constructor of the service class.
To delete a service:
; // Namespace the serviceconst MY_SERVICE = 'MyApp.MyService'; // The service will be set as singleton by defaultGlobalServiceContainer;
To check if a service is available:
; // Namespace the serviceconst MY_SERVICE = 'MyApp.MyService'; // The service will be set as singleton by defaultif GlobalServiceContainer // TODO: Sonething...