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): 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.
unbind
unbind: (serviceIdentifier: 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');
},
);
await container.load(warriorsModule, weaponsModule);
const ninja: Ninja = container.get('Ninja');