Registers a service with Scoped lifetime. One instance is created per ServiceScope; within a scope every resolution returns the same instance.
The schema used as the service identifier.
A ServiceFactory function that creates the service instance. Unlike addSingleton, a plain value is not accepted because scoped services must be freshly created per scope.
Optionaloptions: ServiceRegistrationOptionsOptional ServiceRegistrationOptions.
this for chaining.
Registers a Scoped service whose dependencies are described by a FunctionSchemaBuilder.
The schema used as the service identifier.
A FunctionSchemaBuilder describing the dependency schemas via its parameters.
A function receiving the resolved dependencies and returning the service instance.
Optionaloptions: ServiceRegistrationOptionsOptional ServiceRegistrationOptions.
this for chaining.
Registers a service with Singleton lifetime. The factory (or value) is called at most once; every subsequent resolution returns the same instance.
The schema used as the service identifier.
Either a ServiceFactory function that receives the provider, or a plain value (which is wrapped in a factory automatically).
Optionaloptions: ServiceRegistrationOptionsOptional ServiceRegistrationOptions.
this for chaining.
Registers a Singleton service whose
dependencies are described by a FunctionSchemaBuilder. The
container resolves each parameter schema from the registry and passes
the resolved values to implementation.
The schema used as the service identifier for the created service.
A FunctionSchemaBuilder whose
introspect().parameters list the dependency schemas.
A function whose parameters match the
funcSchema parameter schemas (in order) and returns the service
instance.
Optionaloptions: ServiceRegistrationOptionsOptional ServiceRegistrationOptions.
this for chaining.
Registers a pre-created instance as a Singleton.
Unlike addSingleton, the second argument is always treated as the
service value — never as a factory function. This makes it safe to
register a function-typed service (e.g. a schema backed by func())
without the container accidentally invoking it as a factory.
The schema used as the service identifier.
The pre-created value to register. Can be any type including a function.
Optionaloptions: ServiceRegistrationOptionsOptional ServiceRegistrationOptions.
this for chaining.
const config = { port: 3000, host: 'localhost' };
services.addSingletonInstance(IConfig, config);
const IHandler = func();
const myHandler = (req: Request) => new Response('ok');
services.addSingletonInstance(IHandler, myHandler);
Registers a service with Transient lifetime. A new instance is created on every resolution.
The schema used as the service identifier.
A ServiceFactory function that creates the service instance.
Optionaloptions: ServiceRegistrationOptionsOptional ServiceRegistrationOptions.
this for chaining.
Registers a Transient service whose dependencies are described by a FunctionSchemaBuilder.
The schema used as the service identifier.
A FunctionSchemaBuilder describing the dependency schemas via its parameters.
A function receiving the resolved dependencies and returning the service instance.
Optionaloptions: ServiceRegistrationOptionsOptional ServiceRegistrationOptions.
this for chaining.
Creates a new ServiceProvider from the registrations currently contained in this collection.
Optionaloptions: ServiceProviderOptionsOptional ServiceProviderOptions to control provider behaviour (e.g. scope validation).
A new ServiceProvider instance.
A mutable collection of service registrations. Build up the collection by calling addSingleton, addScoped, or addTransient, then call buildServiceProvider to create an immutable ServiceProvider.
Schema instances act as service keys via reference equality — the same schema object used during registration must be used during resolution.
Example: Basic registration and resolution
Example: Function-schema-driven registration
See