Skip to main content

Class: ObjectContainer

@ima/core.ObjectContainer

The Object Container is an enhanced dependency injector with support for aliases and constants, and allowing to reference classes in the application namespace by specifying their fully qualified names.

Constructors

constructor

new ObjectContainer(namespace)

Initializes the object container.

Parameters

NameTypeDescription
namespaceNamespaceThe namespace container, used to access classes and values using their fully qualified names.

Defined in

packages/core/src/oc/ObjectContainer.ts:90

Properties

_bindingPlugin

Private Optional _bindingPlugin: string

The current plugin binding to OC.

The () method may be called for changing object container binding state only by the bootstrap script.

Defined in

packages/core/src/oc/ObjectContainer.ts:75


_bindingState

Private Optional _bindingState: BindingState

The current binding state.

The () method may be called for changing object container binding state only by the bootstrap script.

Defined in

packages/core/src/oc/ObjectContainer.ts:67


_entries

Private _entries: Map<Dependency<any>, Entry<any>>

Defined in

packages/core/src/oc/ObjectContainer.ts:76


_namespace

Private _namespace: Namespace

The namespace container, used to access classes and values using their fully qualified names.

Defined in

packages/core/src/oc/ObjectContainer.ts:82

Methods

#getDebugName

Private #getDebugName(name): string

Formats name, function, class constructor to more compact name/message to allow for cleaner debug Error messages.

Parameters

NameType
nameany

Returns

string

Defined in

packages/core/src/oc/ObjectContainer.ts:874


_createEntry

_createEntry<T>(classConstructor, dependencies?, options?): Entry<T>

Creates a new entry for the provided class or factory function, the provided dependencies and entry options.

Type parameters

Name
T

Parameters

NameTypeDescription
classConstructorEntryConstructor<T>The class constructor or factory function.
dependencies?any[]The dependencies to pass into the constructor or factory function.
options?EntryOptions-

Returns

Entry<T>

Created instance or generated value.

Defined in

packages/core/src/oc/ObjectContainer.ts:647


_createInstanceFromEntry

_createInstanceFromEntry<T, E>(entry, dependencies?): T

Creates a new instance of the class or retrieves the value generated by the factory function represented by the provided entry, passing in the provided dependencies.

The method uses the dependencies specified by the entry if no custom dependencies are provided.

Type parameters

NameType
TT
Eextends Entry<T>

Parameters

NameTypeDefault valueDescription
entryEundefinedThe entry representing the class that should have its instance created or factory faction to use to create a value.
dependenciesany[][]The dependencies to pass into the constructor or factory function.

Returns

T

Created instance or generated value.

Defined in

packages/core/src/oc/ObjectContainer.ts:686


_getEntry

_getEntry<T>(name): null | Entry<T>

Retrieves the entry for the specified constant, alias, class or factory function, interface, or fully qualified namespace path (the method checks these in this order in case of a name clash).

The method retrieves an existing entry even if a qualified namespace path is provided (if the target class or interface has been configured in this object container).

The method throws an Error if no such constant, alias, registry, interface implementation is known to this object container and the provided identifier is not a valid namespace path specifying an existing class, interface or value.

Type parameters

Name
T

Parameters

NameTypeDescription
nameDependency<T>Name of a constant or alias, factory function, class or interface constructor, or a fully qualified namespace path.

Returns

null | Entry<T>

The retrieved entry.

Throws

If no such constant, alias, registry, interface implementation is known to this object container.

Defined in

packages/core/src/oc/ObjectContainer.ts:535


_getEntryFromClassConstructor

_getEntryFromClassConstructor<T>(classConstructor): null | Entry<T>

Retrieves the class denoted by the provided class constructor.

The method then checks whether there are defined $dependencies property for class. Then the class is registered to this object container.

The method returns the entry for the class if the specified class does not have defined $dependencies property return null.

Type parameters

Name
T

Parameters

NameType
classConstructorOCInjectable<T>

Returns

null | Entry<T>

An entry representing the value at the specified classConstructor. The method returns null if the specified classConstructor does not have defined $dependencies.

Defined in

packages/core/src/oc/ObjectContainer.ts:841


_getEntryFromConstant

_getEntryFromConstant<T>(compositionName): null | Entry<() => T>

Retrieves the constant value denoted by the provided fully qualified composition name.

The method returns the entry for the constant if the constant is registered with this object container, otherwise return null.

Finally, if the constant composition name does not resolve to value, the method return null.

Type parameters

Name
T

Parameters

NameType
compositionNamestring

Returns

null | Entry<() => T>

An entry representing the value at the specified composition name in the constants. The method returns null if the specified composition name does not exist in the constants.

Defined in

packages/core/src/oc/ObjectContainer.ts:747


_getEntryFromNamespace

_getEntryFromNamespace<T>(path): null | Entry<T>

Retrieves the class denoted by the provided fully qualified name within the application namespace.

The method then checks whether there are dependencies configured for the class, no matter whether the class is an implementation class or an "interface" class.

The method returns the entry for the class if the class is registered with this object container, otherwise an unregistered entry is created and returned.

Finally, if the namespace path does not resolve to a class, the method return an unregistered entry resolved to the value denoted by the namespace path.

Alternatively, if a constructor function is passed in instead of a namespace path, the method returns null.

Type parameters

Name
T

Parameters

NameTypeDescription
pathstringNamespace path pointing to a class or a value in the application namespace, or a constructor function.

Returns

null | Entry<T>

An entry representing the value or class at the specified path in the namespace. The method returns null if the specified path does not exist in the namespace.

Defined in

packages/core/src/oc/ObjectContainer.ts:803


_isOptional

_isOptional<T>(name): boolean

Checks whether the name is marked as optional.

Type parameters

Name
T

Parameters

NameTypeDescription
nameDependency<T>Name of a constant or alias, factory function, class or interface constructor, or a fully qualified namespace path.

Returns

boolean

Defined in

packages/core/src/oc/ObjectContainer.ts:593


_isSpread

_isSpread<T>(name): boolean

Checks whether the name is marked as spread.

Type parameters

Name
T

Parameters

NameTypeDescription
nameDependency<T>Name of a constant or alias, factory function, class or interface constructor, or a fully qualified namespace path.

Returns

boolean

Defined in

packages/core/src/oc/ObjectContainer.ts:607


_updateEntryValues

_updateEntryValues<T, E>(entry, classConstructor, dependencies): void

The method update classConstructor and dependencies for defined entry. The entry throw Error for constants and if you try override dependencies more than once.

Type parameters

NameType
TT
Eextends Entry<T>

Parameters

NameTypeDescription
entryEThe entry representing the class that should have its instance created or factory faction to use to create a value.
classConstructorOCInjectable<T>The class constructor or factory function.
dependenciesany[]The dependencies to pass into the constructor or factory function.

Returns

void

Defined in

packages/core/src/oc/ObjectContainer.ts:626


bind

bind<T, C>(name, classConstructor, dependencies?): ObjectContainer

Binds the specified class or factory function and dependencies to the specified alias. Binding a class or factory function to an alias allows the class or function to be specified as a dependency by specifying the alias and creating new instances by referring to the class or function by the alias.

Also note that the same class or function may be bound to several aliases and each may use different dependencies.

The alias will use the default dependencies bound for the class if no dependencies are provided.

Type parameters

NameType
Textends keyof OCAliasMap
CC

Parameters

NameTypeDescription
nameTAlias name.
classConstructorOCInjectable<C>The class constructor or a factory function.
dependencies?any[]The dependencies to pass into the constructor or factory function.

Returns

ObjectContainer

This object container.

Defined in

packages/core/src/oc/ObjectContainer.ts:114


clear

clear(): ObjectContainer

Clears all entries from this object container and resets the locking mechanism of this object container.

Returns

ObjectContainer

This object container.

Defined in

packages/core/src/oc/ObjectContainer.ts:485


constant

constant<T, V>(name, value): ObjectContainer

Defines a new constant registered with this object container. Note that this is the only way of passing string values to constructors because the object container treats strings as class, interface, alias or constant names.

Type parameters

NameType
Textends keyof OCAliasMap
VV

Parameters

NameTypeDescription
nameTThe constant name.
valueVThe constant value.

Returns

ObjectContainer

This object container.

Defined in

packages/core/src/oc/ObjectContainer.ts:200


create

create<T>(name, dependencies?): OCInstance<T>

Creates a new instance of the class or retrieves the value generated by the factory function identified by the provided name, class, interface, or factory function, passing in the provided dependencies.

The method uses the dependencies specified when the class, interface or factory function has been registered with the object container if no custom dependencies are provided.

Type parameters

NameType
Textends OCInstanceConstrain<any>

Parameters

NameTypeDefault valueDescription
nameTundefinedThe name of the alias, class, interface, or the class, interface or a factory function to use.
dependenciesany[][]The dependencies to pass into the constructor or factory function.

Returns

OCInstance<T>

Created instance or generated value.

Defined in

packages/core/src/oc/ObjectContainer.ts:463


get

get<T>(name): OCInstance<T>

Retrieves the shared instance or value of the specified constant, alias, class or factory function, interface, or fully qualified namespace path (the method checks these in this order in case of a name clash).

The instance or value is created lazily the first time it is requested.

Type parameters

NameType
Textends OCInstanceConstrain<any>

Parameters

NameTypeDescription
nameTThe name of the alias, class, interface, or the class, interface or a factory function.

Returns

OCInstance<T>

The shared instance or value.

Defined in

packages/core/src/oc/ObjectContainer.ts:400


getConstructorOf

getConstructorOf<T>(name): null | Constructor<T>

Returns the class constructor function of the specified class.

Type parameters

NameType
Textends keyof OCAliasMap | Constructor<any>

Parameters

NameTypeDescription
nameTThe name by which the class is registered with this object container.

Returns

null | Constructor<T>

The constructor function.

Defined in

packages/core/src/oc/ObjectContainer.ts:418


has

has<T>(name): boolean

Returns true if the specified object, class or resource is registered with this object container.

Type parameters

Name
T

Parameters

NameTypeDescription
namekeyof OCAliasMap | OCInjectable<T>The resource name.

Returns

boolean

true if the specified object, class or resource is registered with this object container.

Defined in

packages/core/src/oc/ObjectContainer.ts:438


inject

inject<T>(classConstructor, dependencies): ObjectContainer

Configures the object loader with the specified default dependencies for the specified class.

New instances of the class created by this object container will receive the provided dependencies into constructor unless custom dependencies are provided.

Type parameters

Name
T

Parameters

NameTypeDescription
classConstructorConstructor<T>The class constructor.
dependenciesany[]The dependencies to pass into the constructor function.

Returns

ObjectContainer

This object container.

Defined in

packages/core/src/oc/ObjectContainer.ts:244


provide

provide<T, I>(interfaceConstructor, implementationConstructor, dependencies?): ObjectContainer

Configures the default implementation of the specified interface to use when an implementation provider of the specified interface is requested from this object container.

The implementation constructor will obtain the provided default dependencies or the dependencies provided to the () method.

Type parameters

Name
T
I

Parameters

NameTypeDescription
interfaceConstructorConstructor<I> | AbstractConstructor<I>The constructor of the interface representing the service.
implementationConstructorConstructor<T>The constructor of the class implementing the service interface.
dependencies?any[]The dependencies to pass into the constructor function.

Returns

ObjectContainer

This object container.

Defined in

packages/core/src/oc/ObjectContainer.ts:319


setBindingState

setBindingState(bindingState, bindingPluginName?): void

Parameters

NameType
bindingStateBindingState
bindingPluginName?string

Returns

void

Defined in

packages/core/src/oc/ObjectContainer.ts:493