@e22m4u/js-service
English | Русский
The «Service Locator» implementation for JavaScript.
Installation
npm install @e22m4u/js-service
The module supports ESM and CommonJS standards.
ESM
import {Service} from '@e22m4u/js-service';
CommonJS
const {Service} = require('@e22m4u/js-service');
Purpose
The module offers ServiceContainer
and Service
classes,
which can be used separately or together.
ServiceContainer
- classic version of the service locatorService
- hides the creation of the container and its distribution
The Service
class is convenient when the application has a single
entry point created by the new
operator. For example, if such a point
is the Application
class, we could inherit it from the Service
class
and access other services using the getService
method without worrying
about creating and storing their instances.
Moreover, if other services also inherit from the Service
class,
they can refer to each other using the getService
method,
as if we were passing the service container between them.
ServiceContainer
Methods:
get(ctor, ...args)
returns an existing or new instancehas(ctor)
checks if a constructor exists in the containeradd(ctor, ...args)
adds a constructor to the containeruse(ctor, ...args)
adds a constructor and creates its instanceset(ctor, service)
adds a constructor and its instance
get
The get
method of the ServiceContainer
class creates
an instance of the given constructor and saves it for next
access following the "singleton" principle.
Example:
import {ServiceContainer} from '@e22m4u/js-service';
const container = new ServiceContainer();
const myDate1 = container.get(Date);
const myDate2 = container.get(Date);
console.log(myDate1);
console.log(myDate2);
console.log(myDate1 === myDate2);
The get
method can accept constructor arguments.
If the container already has an instance of this
constructor, it will be recreated with new arguments.
Example:
const myDate1 = container.get(Date, '2025-01-01');
const myDate2 = container.get(Date);
const myDate3 = container.get(Date, '2025-05-05');
console.log(myDate1);
console.log(myDate2);
console.log(myDate3);
Inheritance
The ServiceContainer
constructor takes a parent container
as its first parameter, which is used as an alternative
if the constructor of the requested instance (service)
is not registered in the current one.
class MyService {}
const parentContainer = new ServiceContainer();
parentContainer.add(MyService);
const childContainer = new ServiceContainer(parentContainer);
const hasService = childContainer.has(MyService);
console.log(hasService);
Service
Methods:
getService(ctor, ...args)
returns an existing or new instancehasService(ctor)
checks if a constructor exists in the containeraddService(ctor, ...args)
adds a constructor to the containeruseService(ctor, ...args)
adds a constructor and creates its instancesetService(ctor, service)
adds a constructor and its instance
A service is just a class instance. However, if a service inherits
the Service
class, such a service encapsulating the creation
of the service container, its storage, and transfer to other services.
Example:
import {Service} from '@e22m4u/js-service';
class Foo extends Service {
method() {
const bar = this.getService(Bar);
}
}
class Bar extends Service {
method() {
const foo = this.getService(Foo);
}
}
class App extends Service {
method() {
const foo = this.getService(Foo);
const bar = this.getService(Bar);
}
}
const app = new App();
In the example above, we didn't worry about creating
a service container and passing it between services,
because this logic is encapsulated in the Service
class and its getService
method.
getService
The getService
method ensures the existence of a single
instance of the requested service, rather than creating
a new one each time. However, when passing additional
arguments, the service will be recreated with these
arguments passed to the constructor.
Example:
const foo1 = this.getService(Foo, 'arg');
const foo2 = this.getService(Foo);
console.log(foo1 === foo2);
const foo3 = this.getService(Foo, 'arg');
const foo4 = this.getService(Foo);
console.log(foo3 === foo4);
Tests
npm run test
License
MIT