Share via


ServiceCollection class

ServiceCollection is an interface that describes a set of methods to register services. This, in a lighter way, mimics the .NET dependency injection service collection functionality, except for instances rather than types.

Constructors

ServiceCollection(Record<string, unknown>)

Construct a Providers instance

Methods

addFactory<InstanceType, Dependencies>(string, string[], DependencyFactory<InstanceType, Dependencies, false>)

Register a factory for a key with a set of dependencies.

addFactory<InstanceType>(string, Factory<InstanceType, false>)

Register a factory for a key.

addInstance<InstanceType>(string, InstanceType)

Register an instance by key. This will overwrite existing instances.

composeFactory<InstanceType, Dependencies>(string, string[], DependencyFactory<InstanceType, Dependencies, true>)

Register a factory (that expects an initial value that is not undefined) for a key with a set of dependencies.

composeFactory<InstanceType>(string, Factory<InstanceType, true>)

Register a factory (that expects the initial value that is not undefined) for a key.

makeInstance<InstanceType>(string, boolean)

Build a single service.

makeInstances<InstancesType>()

Build the full set of services.

mustMakeInstance<InstanceType>(string, boolean)

Build a single service and assert that it is not undefined.

mustMakeInstances<InstancesType>(string[])

Build the full set of services, asserting that the specified keys are not undefined.

Constructor Details

ServiceCollection(Record<string, unknown>)

Construct a Providers instance

new ServiceCollection(defaultServices?: Record<string, unknown>)

Parameters

defaultServices

Record<string, unknown>

default set of services

Method Details

addFactory<InstanceType, Dependencies>(string, string[], DependencyFactory<InstanceType, Dependencies, false>)

Register a factory for a key with a set of dependencies.

function addFactory<InstanceType, Dependencies>(key: string, dependencies: string[], factory: DependencyFactory<InstanceType, Dependencies, false>): ServiceCollection

Parameters

key

string

Key that factory will provide.

dependencies

string[]

Set of things this instance depends on. Will be provided to factory function via services.

factory

DependencyFactory<InstanceType, Dependencies, false>

Function that creates an instance to provide.

Returns

this for chaining.

addFactory<InstanceType>(string, Factory<InstanceType, false>)

Register a factory for a key.

function addFactory<InstanceType>(key: string, factory: Factory<InstanceType, false>): ServiceCollection

Parameters

key

string

Key that factory will provide.

factory

Factory<InstanceType, false>

Function that creates an instance to provide.

Returns

this for chaining.

addInstance<InstanceType>(string, InstanceType)

Register an instance by key. This will overwrite existing instances.

function addInstance<InstanceType>(key: string, instance: InstanceType): ServiceCollection

Parameters

key

string

Key of the instance being provided.

instance

InstanceType

Instance to provide.

Returns

this for chaining.

composeFactory<InstanceType, Dependencies>(string, string[], DependencyFactory<InstanceType, Dependencies, true>)

Register a factory (that expects an initial value that is not undefined) for a key with a set of dependencies.

function composeFactory<InstanceType, Dependencies>(key: string, dependencies: string[], factory: DependencyFactory<InstanceType, Dependencies, true>): ServiceCollection

Parameters

key

string

Key that factory will provide.

dependencies

string[]

Set of things this instance depends on. Will be provided to factory function via services.

factory

DependencyFactory<InstanceType, Dependencies, true>

Function that creates an instance to provide.

Returns

this for chaining.

composeFactory<InstanceType>(string, Factory<InstanceType, true>)

Register a factory (that expects the initial value that is not undefined) for a key.

function composeFactory<InstanceType>(key: string, factory: Factory<InstanceType, true>): ServiceCollection

Parameters

key

string

key of the instance being provided

factory

Factory<InstanceType, true>

Returns

this for chaining

makeInstance<InstanceType>(string, boolean)

Build a single service.

function makeInstance<InstanceType>(key: string, deep?: boolean): undefined | InstanceType

Parameters

key

string

Service to build.

deep

boolean

Whether to reconstruct all dependencies.

Returns

undefined | InstanceType

The service instance, or undefined.

makeInstances<InstancesType>()

Build the full set of services.

function makeInstances<InstancesType>(): InstancesType

Returns

InstancesType

all resolved services

mustMakeInstance<InstanceType>(string, boolean)

Build a single service and assert that it is not undefined.

function mustMakeInstance<InstanceType>(key: string, deep?: boolean): InstanceType

Parameters

key

string

Service to build.

deep

boolean

Wheter to reconstruct all dependencies.

Returns

InstanceType

The service instance

mustMakeInstances<InstancesType>(string[])

Build the full set of services, asserting that the specified keys are not undefined.

function mustMakeInstances<InstancesType>(keys: string[]): InstancesType

Parameters

keys

string[]

Instances that must be not undefined

Returns

InstancesType

All resolve services