Primitives
Service container
@stone-js/service-container is dependency injection as a small library. The framework creates one per event as the ephemeral context, but the container stands alone: register bindings, resolve them, and let it construct dependency graphs for you.
#Standalone use
Create a container, register bindings, and resolve. The same methods documented in the DI section are the container's public surface.
import { Container } from '@stone-js/service-container'
const container = Container.create()
container.instance('config', { pageSize: 20 })
container.singleton('tasks', (c) => new TaskService(c.make('config')))
container.alias('tasks', ['taskService'])
const tasks = container.resolve('tasks') // built once, cached
const same = container.make('taskService') // resolved via the alias#The surface
| Method | Type | Description |
|---|---|---|
| Container.create() | () => Container | Create a container. |
| singleton(key, resolver) | (key, (c) => V) => this | Register a value built once and cached. |
| instance(key, value) | (key, V) => this | Register an already-built value. |
| alias(key, aliases) | (key, string | string[]) => this | Add alternative names. |
| resolve(key, singleton?) | <V>(key) => V | Resolve, constructing if needed. |
| make(key) | <V>(key) => V | Resolve, failing fast on an unknown key. |
| factory(key) | <V>(key) => () => V | A factory that resolves on each call. |
| bound / has | (key) => boolean | Test whether a key is bound / known. |