Skip to main content
Version: 6.x

ContainerModule

Container modules help manage the complexity of bindings in large applications.

Constructor​

constructor(registry: interfaces.ContainerModuleCallBack)

The constructor argument for ContainerModule is a registration callback with function parameters to manage bindings within the scope of the container module.

bind​

bind: interfaces.Bind

Refer to the docs for more information.

unbind​

unbind: interfaces.Unbind

Refer to the docs for more information.

isBound​

isBound: interfaces.IsBound

Refer to the docs for more information.

rebind​

rebind: interfaces.Rebind

Refer to the docs for more information.

unbindAsync​

unbindAsync: interfaces.UnbindAsync

Refer to the docs for more information.

onActivation​

onActivation: interfaces.Container['onActivation']

Refer to the docs for more information.

onDeactivation​

onDeactivation: interfaces.Container['onDeactivation']

Refer to the docs for more information.

Example: binding services through ContainerModule API​

When a container module is loaded into a Container, the registration callback is invoked. This is the opportunity for the container module to register bindings and handlers. Use the Container load method for ContainerModule instances and the Container loadAsync method for AsyncContainerModule instances.

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. Use the unloadAsync method to unload when there will be an async deactivation handler or async preDestroy.

const warriorsModule: ContainerModule = new ContainerModule(
(bind: interfaces.Bind) => {
bind<Ninja>('Ninja').to(Ninja);
},
);

const weaponsModule: ContainerModule = new ContainerModule(
(
bind: interfaces.Bind,
_unbind: interfaces.Unbind,
_isBound: interfaces.IsBound,
_rebind: interfaces.Rebind,
_unbindAsync: interfaces.UnbindAsync,
_onActivation: interfaces.Container['onActivation'],
_onDeactivation: interfaces.Container['onDeactivation'],
) => {
bind<Katana>('Weapon').to(Katana).whenTargetNamed('Melee');
bind<Shuriken>('Weapon').to(Shuriken).whenTargetNamed('Ranged');
},
);

const container: Container = new Container();
container.load(warriorsModule, weaponsModule);

const ninja: Ninja = container.get('Ninja');