ContainerModule
Container modules help manage the complexity of bindings in large applications.
Constructorâ
constructor(load: (options: ContainerModuleLoadOptions) => void | Promise<void>)
The constructor argument for ContainerModule is a load callback with function parameters to manage bindings within the scope of the container module.
Methodsâ
loadâ
load(options: ContainerModuleLoadOptions): void | Promise<void>
Loads the container module with the provided options.
ContainerModuleLoadOptionsâ
Options to be passed to the load callback. It contains the following properties:
bindâ
bind: <T>(serviceIdentifier: ServiceIdentifier<T>) => BindToFluentSyntax<T>
Refer to the docs for more information.
isBoundâ
isBound: (serviceIdentifier: ServiceIdentifier, options?: IsBoundOptions) => boolean
Refer to the docs for more information.
onActivationâ
onActivation: <T>(serviceIdentifier: ServiceIdentifier<T>, activation: BindingActivation<T>) => void
Refer to the docs for more information.
onDeactivationâ
onDeactivation: <T>(serviceIdentifier: ServiceIdentifier<T>, deactivation: BindingDeactivation<T>) => void
Refer to the docs for more information.
rebindâ
rebind: <T>(serviceIdentifier: ServiceIdentifier<T>) => BindToFluentSyntax<T>
Refer to the docs for more information.
rebindAsyncâ
rebindAsync: <T>(serviceIdentifier: ServiceIdentifier<T>) => Promise<BindToFluentSyntax<T>>
Refer to the docs for more information.
unbindâ
unbind: (identifier: BindingIdentifier | ServiceIdentifier) => void
Refer to the docs for more information.
unbindAsyncâ
unbindAsync: (identifier: BindingIdentifier | ServiceIdentifier) => Promise<void>
Refer to the docs for more information.
Example: binding services through ContainerModule APIâ
When a container module is loaded into a Container, the load callback is invoked. This is the opportunity for the container module to register bindings and handlers.
When a container module is unloaded from a Container, the bindings added by that container will be removed, and the deactivation process will occur for each of them. Container deactivation and activation handlers will also be removed.
const warriorsModule: ContainerModule = new ContainerModule(
(options: ContainerModuleLoadOptions) => {
options.bind<Ninja>('Ninja').to(Ninja);
},
);
const weaponsModule: ContainerModule = new ContainerModule(
(options: ContainerModuleLoadOptions) => {
options.bind<Katana>('Weapon').to(Katana).whenNamed('Melee');
options.bind<Shuriken>('Weapon').to(Shuriken).whenNamed('Ranged');
},
);
container.load(warriorsModule, weaponsModule);
const ninja: Ninja = container.get('Ninja');