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>): this

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

this for chaining

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

Register a factory for a key.

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

Parameters

key

string

key that factory will provide

factory

Factory<InstanceType, false>

function that creates an instance to provide

Returns

this

this for chaining

addInstance<InstanceType>(string, InstanceType)

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

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

Parameters

key

string

key of the instance being provided

instance

InstanceType

instance to provide

Returns

this

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>): this

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

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>): this

Parameters

key

string

key of the instance being provided

factory

Factory<InstanceType, true>

Returns

this

this for chaining

makeInstance<InstanceType>(string, boolean)

Build a single service.

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

Parameters

key

string

service to build

deep

boolean

reconstruct all dependencies

Returns

InstanceType | undefined

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

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