container.Container - NealRame/ts-injector GitHub Wiki
container.Container
• new Container()
▸ alias<T>(token, service): Container
Register an alias for a service with the container.
| Name | Type |
|---|---|
T |
unknown |
| Name | Type | Description |
|---|---|---|
token |
Token<T> |
|
service |
TConstructor<T> |
the service to alias with the token. |
this container.
▸ get<T>(id, fallback?): T
Get a value from the container.
Throws
Throws
| Name | Type |
|---|---|
T |
unknown |
| Name | Type | Description |
|---|---|---|
id |
TServiceIdentifier<T> |
a service identifier. |
fallback? |
T |
a fallback value. |
T
▸ has(id): boolean
Check if the containers knows about the given alias or service.
| Name | Type | Description |
|---|---|---|
id |
TServiceIdentifier |
a Token or a Class. |
boolean
true if the container knows about the given alias or service.
▸ 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
}| Name |
|---|
U |
| Name | Type | Description |
|---|---|---|
service |
TServiceIdentifier<U> |
Service identifier. |
fallback? |
U |
Fallback value if the service is not found. |
fn
a class field decorator.
▸ <T>(target, context): () => U
| Name | Type |
|---|---|
T |
extends object
|
| Name | Type |
|---|---|
target |
undefined |
context |
ClassFieldDecoratorContext<T, U> |
fn
▸ (): U
U
▸ remove(token): Container
Remove a service alias or a value from the container.
| Name | Type |
|---|---|
token |
Token<unknown> |
this container.
▸ 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 {}| Name |
|---|
T |
| Name | Type |
|---|---|
option? |
TServiceOption<T> |
fn
a class decorator.
▸ (target, context): TConstructor<T>
| Name | Type |
|---|---|
target |
TConstructor<T> |
context |
ClassDecoratorContext<TConstructor<T>> |
TConstructor<T>
▸ set<T>(token, value): Container
Register a value with the container.
| Name | Type |
|---|---|
T |
unknown |
| Name | Type | Description |
|---|---|---|
token |
Token<T> |
|
value |
T |
the value to assign to the token. |
this container.