Skip to content
Buy me a coffee

ServiceCollection

Defined in: src/service-collection.ts:43

Collects service registrations, then builds a ServiceProvider. Modeled on .NET’s IServiceCollection.

const services = new ServiceCollection();
services.addSingleton(Logger, ConsoleLogger);
services.addScoped(UserService, [Logger]);
const provider = services.buildServiceProvider({ validateScopes: true });

new ServiceCollection(): ServiceCollection

ServiceCollection

addKeyedScoped<T>(token, factory, key): this

Defined in: src/service-collection.ts:125

Registers a keyed scoped service. See addKeyedSingleton.

T

Token<T>

ServiceFactory<NoInfer<T>>

ServiceKey

this

addKeyedScoped<T, A>(token, implementation, key, dependencies): this

Defined in: src/service-collection.ts:126

Registers a keyed scoped service. See addKeyedSingleton.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

ServiceKey

Dependencies<A>

this

addKeyedScoped<T>(token, implementation, key): this

Defined in: src/service-collection.ts:127

Registers a keyed scoped service. See addKeyedSingleton.

T

Token<T>

Newable<NoInfer<T>, []>

ServiceKey

this


addKeyedSingleton<T>(token, factory, key): this

Defined in: src/service-collection.ts:117

Registers a keyed singleton, resolved with getKeyedService(token, key). Keyed registrations are separate from plain ones: getService(token) never returns them.

T

Token<T>

ServiceFactory<NoInfer<T>>

ServiceKey

this

addKeyedSingleton<T, A>(token, implementation, key, dependencies): this

Defined in: src/service-collection.ts:118

Registers a keyed singleton, resolved with getKeyedService(token, key). Keyed registrations are separate from plain ones: getService(token) never returns them.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

ServiceKey

Dependencies<A>

this

addKeyedSingleton<T>(token, implementation, key): this

Defined in: src/service-collection.ts:119

Registers a keyed singleton, resolved with getKeyedService(token, key). Keyed registrations are separate from plain ones: getService(token) never returns them.

T

Token<T>

Newable<NoInfer<T>, []>

ServiceKey

this


addKeyedTransient<T>(token, factory, key): this

Defined in: src/service-collection.ts:133

Registers a keyed transient service. See addKeyedSingleton.

T

Token<T>

ServiceFactory<NoInfer<T>>

ServiceKey

this

addKeyedTransient<T, A>(token, implementation, key, dependencies): this

Defined in: src/service-collection.ts:134

Registers a keyed transient service. See addKeyedSingleton.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

ServiceKey

Dependencies<A>

this

addKeyedTransient<T>(token, implementation, key): this

Defined in: src/service-collection.ts:135

Registers a keyed transient service. See addKeyedSingleton.

T

Token<T>

Newable<NoInfer<T>, []>

ServiceKey

this


addScoped<T>(token, factory): this

Defined in: src/service-collection.ts:64

Registers a scoped service: one instance per scope. Same forms as addSingleton.

T

Token<T>

ServiceFactory<NoInfer<T>>

this

addScoped<T>(token): this

Defined in: src/service-collection.ts:65

Registers a scoped service: one instance per scope. Same forms as addSingleton.

T

Newable<T, []>

this

addScoped<T, A>(token, dependencies): this

Defined in: src/service-collection.ts:66

Registers a scoped service: one instance per scope. Same forms as addSingleton.

T

A extends unknown[] = any[]

Newable<T, A>

Dependencies<A>

this

addScoped<T>(token, implementation): this

Defined in: src/service-collection.ts:67

Registers a scoped service: one instance per scope. Same forms as addSingleton.

T

Token<T>

Newable<NoInfer<T>, []>

this

addScoped<T, A>(token, implementation, dependencies): this

Defined in: src/service-collection.ts:68

Registers a scoped service: one instance per scope. Same forms as addSingleton.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

Dependencies<A>

this


addSingleton<T>(token, factory): this

Defined in: src/service-collection.ts:54

Registers a singleton: one instance for the root provider and all its scopes.

  • addSingleton(Class) / addSingleton(Class, [deps]): the class is its own token.
  • addSingleton(token, Implementation, [deps]): deps are checked against the constructor’s parameters.
  • addSingleton(token, (provider) => instance): a factory, may be async.

T

Token<T>

ServiceFactory<NoInfer<T>>

this

addSingleton<T>(token): this

Defined in: src/service-collection.ts:55

Registers a singleton: one instance for the root provider and all its scopes.

  • addSingleton(Class) / addSingleton(Class, [deps]): the class is its own token.
  • addSingleton(token, Implementation, [deps]): deps are checked against the constructor’s parameters.
  • addSingleton(token, (provider) => instance): a factory, may be async.

T

Newable<T, []>

this

addSingleton<T, A>(token, dependencies): this

Defined in: src/service-collection.ts:56

Registers a singleton: one instance for the root provider and all its scopes.

  • addSingleton(Class) / addSingleton(Class, [deps]): the class is its own token.
  • addSingleton(token, Implementation, [deps]): deps are checked against the constructor’s parameters.
  • addSingleton(token, (provider) => instance): a factory, may be async.

T

A extends unknown[] = any[]

Newable<T, A>

Dependencies<A>

this

addSingleton<T>(token, implementation): this

Defined in: src/service-collection.ts:57

Registers a singleton: one instance for the root provider and all its scopes.

  • addSingleton(Class) / addSingleton(Class, [deps]): the class is its own token.
  • addSingleton(token, Implementation, [deps]): deps are checked against the constructor’s parameters.
  • addSingleton(token, (provider) => instance): a factory, may be async.

T

Token<T>

Newable<NoInfer<T>, []>

this

addSingleton<T, A>(token, implementation, dependencies): this

Defined in: src/service-collection.ts:58

Registers a singleton: one instance for the root provider and all its scopes.

  • addSingleton(Class) / addSingleton(Class, [deps]): the class is its own token.
  • addSingleton(token, Implementation, [deps]): deps are checked against the constructor’s parameters.
  • addSingleton(token, (provider) => instance): a factory, may be async.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

Dependencies<A>

this


addTransient<T>(token, factory): this

Defined in: src/service-collection.ts:74

Registers a transient service: a new instance on every resolution. Same forms as addSingleton.

T

Token<T>

ServiceFactory<NoInfer<T>>

this

addTransient<T>(token): this

Defined in: src/service-collection.ts:75

Registers a transient service: a new instance on every resolution. Same forms as addSingleton.

T

Newable<T, []>

this

addTransient<T, A>(token, dependencies): this

Defined in: src/service-collection.ts:76

Registers a transient service: a new instance on every resolution. Same forms as addSingleton.

T

A extends unknown[] = any[]

Newable<T, A>

Dependencies<A>

this

addTransient<T>(token, implementation): this

Defined in: src/service-collection.ts:77

Registers a transient service: a new instance on every resolution. Same forms as addSingleton.

T

Token<T>

Newable<NoInfer<T>, []>

this

addTransient<T, A>(token, implementation, dependencies): this

Defined in: src/service-collection.ts:78

Registers a transient service: a new instance on every resolution. Same forms as addSingleton.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

Dependencies<A>

this


addValue<T>(token, value): this

Defined in: src/service-collection.ts:141

Registers an existing value (config, constants, an instance you built). Any value works, null and undefined included. Never disposed by the container.

T

Token<T>

NoInfer<T>

this


buildServiceProvider(options?): ServiceProvider

Defined in: src/service-collection.ts:171

Builds the provider. Registrations are copied, so changing this collection afterwards does not affect providers already built.

BuildOptions = {}

ServiceProvider


getCircularDependencies(): CircularDependency[]

Defined in: src/service-collection.ts:194

Every dependency cycle among plain registrations.

CircularDependency[]


getDependencyTree(token): DependencyTreeNode

Defined in: src/service-collection.ts:179

Dependency tree of token, following the last registration of each dependency.

Token

DependencyTreeNode


has<T>(token, key?): boolean

Defined in: src/service-collection.ts:163

True if token (or token with key) is registered.

T

Token<T>

ServiceKey

boolean


remove<T>(token): this

Defined in: src/service-collection.ts:146

Removes every registration of token, keyed ones included.

T

Token<T>

this


replace<T>(token, factory): this

Defined in: src/service-collection.ts:153

Replaces the plain registrations of token, keeping the lifetime of the last one (singleton if there was none).

T

Token<T>

ServiceFactory<NoInfer<T>>

this

replace<T, A>(token, implementation, dependencies): this

Defined in: src/service-collection.ts:154

Replaces the plain registrations of token, keeping the lifetime of the last one (singleton if there was none).

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

Dependencies<A>

this

replace<T>(token, implementation): this

Defined in: src/service-collection.ts:155

Replaces the plain registrations of token, keeping the lifetime of the last one (singleton if there was none).

T

Token<T>

Newable<NoInfer<T>, []>

this


tryAddScoped<T>(token, factory): this

Defined in: src/service-collection.ts:94

Like addScoped, but does nothing if token is already registered.

T

Token<T>

ServiceFactory<NoInfer<T>>

this

tryAddScoped<T>(token): this

Defined in: src/service-collection.ts:95

Like addScoped, but does nothing if token is already registered.

T

Newable<T, []>

this

tryAddScoped<T, A>(token, dependencies): this

Defined in: src/service-collection.ts:96

Like addScoped, but does nothing if token is already registered.

T

A extends unknown[] = any[]

Newable<T, A>

Dependencies<A>

this

tryAddScoped<T>(token, implementation): this

Defined in: src/service-collection.ts:97

Like addScoped, but does nothing if token is already registered.

T

Token<T>

Newable<NoInfer<T>, []>

this

tryAddScoped<T, A>(token, implementation, dependencies): this

Defined in: src/service-collection.ts:98

Like addScoped, but does nothing if token is already registered.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

Dependencies<A>

this


tryAddSingleton<T>(token, factory): this

Defined in: src/service-collection.ts:84

Like addSingleton, but does nothing if token is already registered.

T

Token<T>

ServiceFactory<NoInfer<T>>

this

tryAddSingleton<T>(token): this

Defined in: src/service-collection.ts:85

Like addSingleton, but does nothing if token is already registered.

T

Newable<T, []>

this

tryAddSingleton<T, A>(token, dependencies): this

Defined in: src/service-collection.ts:86

Like addSingleton, but does nothing if token is already registered.

T

A extends unknown[] = any[]

Newable<T, A>

Dependencies<A>

this

tryAddSingleton<T>(token, implementation): this

Defined in: src/service-collection.ts:87

Like addSingleton, but does nothing if token is already registered.

T

Token<T>

Newable<NoInfer<T>, []>

this

tryAddSingleton<T, A>(token, implementation, dependencies): this

Defined in: src/service-collection.ts:88

Like addSingleton, but does nothing if token is already registered.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

Dependencies<A>

this


tryAddTransient<T>(token, factory): this

Defined in: src/service-collection.ts:104

Like addTransient, but does nothing if token is already registered.

T

Token<T>

ServiceFactory<NoInfer<T>>

this

tryAddTransient<T>(token): this

Defined in: src/service-collection.ts:105

Like addTransient, but does nothing if token is already registered.

T

Newable<T, []>

this

tryAddTransient<T, A>(token, dependencies): this

Defined in: src/service-collection.ts:106

Like addTransient, but does nothing if token is already registered.

T

A extends unknown[] = any[]

Newable<T, A>

Dependencies<A>

this

tryAddTransient<T>(token, implementation): this

Defined in: src/service-collection.ts:107

Like addTransient, but does nothing if token is already registered.

T

Token<T>

Newable<NoInfer<T>, []>

this

tryAddTransient<T, A>(token, implementation, dependencies): this

Defined in: src/service-collection.ts:108

Like addTransient, but does nothing if token is already registered.

T

A extends unknown[] = any[]

Token<T>

Newable<NoInfer<T>, A>

Dependencies<A>

this


visualizeCircularDependencies(): string

Defined in: src/service-collection.ts:229

Every dependency cycle as text.

string


visualizeDependencyTree(token): string

Defined in: src/service-collection.ts:216

The dependency tree of token as text.

Token

string