container.Container - NealRame/ts-injector GitHub Wiki

Class: Container

container.Container

Table of contents

Constructors

Methods

Constructors

constructor

new Container()

Methods

alias

alias<T>(token, service): Container

Register an alias for a service with the container.

Type parameters

Name Type
T unknown

Parameters

Name Type Description
token Token<T>
service TConstructor<T> the service to alias with the token.

Returns

Container

this container.

Defined in

src/lib/container.ts:224


get

get<T>(id, fallback?): T

Get a value from the container.

Throws

Throws

Type parameters

Name Type
T unknown

Parameters

Name Type Description
id TServiceIdentifier<T> a service identifier.
fallback? T a fallback value.

Returns

T

Defined in

src/lib/container.ts:187


has

has(id): boolean

Check if the containers knows about the given alias or service.

Parameters

Name Type Description
id TServiceIdentifier a Token or a Class.

Returns

boolean

true if the container knows about the given alias or service.

Defined in

src/lib/container.ts:172


inject

inject<U>(service, fallback?): <T>(target: undefined, context: ClassFieldDecoratorContext<T, U>) => () => U

Register a field to be injected with a service.

Example

const container = new Container()
@container.service()
class ServiceA {}

@container.service()
class ServiceB {
   @container.inject(ServiceA)
   private _serviceA!: ServiceA
}

Type parameters

Name
U

Parameters

Name Type Description
service TServiceIdentifier<U> Service identifier.
fallback? U Fallback value if the service is not found.

Returns

fn

a class field decorator.

▸ <T>(target, context): () => U

Type parameters
Name Type
T extends object
Parameters
Name Type
target undefined
context ClassFieldDecoratorContext<T, U>
Returns

fn

▸ (): U

Returns

U

Defined in

src/lib/container.ts:155


remove

remove(token): Container

Remove a service alias or a value from the container.

Parameters

Name Type
token Token<unknown>

Returns

Container

this container.

Defined in

src/lib/container.ts:251


service

service<T>(option?): (target: TConstructor<T>, context: ClassDecoratorContext<TConstructor<T>>) => TConstructor<T>

Register a service with the container.

See

TServiceOption for more information.

Example

Register a service with the container.

const container = new Container()
@container.service()
class Foo {}

Example

Register a service with the container as a singleton.

const container = new Container()
@container.service({
   lifecycle: ServiceLifecycle.Singleton,
})
class Foo {}

Type parameters

Name
T

Parameters

Name Type
option? TServiceOption<T>

Returns

fn

a class decorator.

▸ (target, context): TConstructor<T>

Parameters
Name Type
target TConstructor<T>
context ClassDecoratorContext<TConstructor<T>>
Returns

TConstructor<T>

Defined in

src/lib/container.ts:113


set

set<T>(token, value): Container

Register a value with the container.

Type parameters

Name Type
T unknown

Parameters

Name Type Description
token Token<T>
value T the value to assign to the token.

Returns

Container

this container.

Defined in

src/lib/container.ts:238

⚠️ **GitHub.com Fallback** ⚠️