Stone.jsDocs
Paradigm

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.

example.tsts
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

MethodTypeDescription
Container.create()() => ContainerCreate a container.
singleton(key, resolver)(key, (c) => V) => thisRegister a value built once and cached.
instance(key, value)(key, V) => thisRegister an already-built value.
alias(key, aliases)(key, string | string[]) => thisAdd alternative names.
resolve(key, singleton?)<V>(key) => VResolve, constructing if needed.
make(key)<V>(key) => VResolve, failing fast on an unknown key.
factory(key)<V>(key) => () => VA factory that resolves on each call.
bound / has(key) => booleanTest whether a key is bound / known.

Stone.js

Your app exists in every runtime. Until you run it.

An open-source project by Stone Foundation
Created by Mr. Stone (Evens Pierre)