diff --git a/packages/docs/services/inversify-site/docs/api/container-module.mdx b/packages/docs/services/inversify-site/docs/api/container-module.mdx index ebcb553a..15f5ef5e 100644 --- a/packages/docs/services/inversify-site/docs/api/container-module.mdx +++ b/packages/docs/services/inversify-site/docs/api/container-module.mdx @@ -2,7 +2,7 @@ sidebar_position: 3 title: Container Module --- -import containerModuleApiExampleSource from '@inversifyjs/code-examples/generated/examples/containerModuleApiExample.ts.txt'; +import containerModuleApiExampleSource from '@inversifyjs/code-examples/generated/examples/v7/containerModuleApiExample.ts.txt'; import CodeBlock from '@theme/CodeBlock'; # ContainerModule @@ -12,71 +12,69 @@ Container modules help manage the complexity of bindings in large applications. ## Constructor ```ts -constructor(registry: interfaces.ContainerModuleCallBack) +constructor(load: (options: ContainerModuleLoadOptions) => void | Promise) ``` -The constructor argument for `ContainerModule` is a registration callback with function parameters to manage bindings within the scope of the container module. +The constructor argument for `ContainerModule` is a load callback with function parameters to manage bindings within the scope of the container module. -### bind +### Methods + +#### load ```ts -bind: interfaces.Bind +load(options: ContainerModuleLoadOptions): Promise ``` -Refer to the [docs](/docs/api/container#bind) for more information. - -### unbind +Loads the container module with the provided options. -``` -unbind: interfaces.Unbind -``` +## ContainerModuleLoadOptions -Refer to the [docs](/docs/api/container#unbind) for more information. +Options to be passed to the load callback. It contains the following properties: -### isBound +### bind ```ts -isBound: interfaces.IsBound +bind: (serviceIdentifier: ServiceIdentifier) => BindToFluentSyntax ``` -Refer to the [docs](/docs/api/container#isbound) for more information. +Refer to the [docs](/docs/api/container#bind) for more information. -### rebind +### isBound ```ts -rebind: interfaces.Rebind +isBound: (serviceIdentifier: ServiceIdentifier, options?: IsBoundOptions) => boolean ``` -Refer to the [docs](/docs/api/container#rebind) for more information. +Refer to the [docs](/docs/api/container#isbound) for more information. -### unbindAsync +### onActivation ```ts -unbindAsync: interfaces.UnbindAsync +onActivation: (serviceIdentifier: ServiceIdentifier, activation: BindingActivation) => void ``` -Refer to the [docs](/docs/api/container#unbindasync) for more information. +Refer to the [docs](/docs/api/container#onactivation) for more information. -### onActivation +### onDeactivation ```ts -onActivation: interfaces.Container['onActivation'] +onDeactivation: (serviceIdentifier: ServiceIdentifier, deactivation: BindingDeactivation) => void ``` -Refer to the [docs](/docs/api/container#onactivation) for more information. +Refer to the [docs](/docs/api/container#ondeactivation) for more information. -### onDeactivation +### unbind ```ts -onDeactivation: interfaces.Container['onDeactivation'] +unbind: (serviceIdentifier: ServiceIdentifier) => Promise ``` -Refer to the [docs](/docs/api/container#ondeactivation) for more information. +Refer to the [docs](/docs/api/container#unbind) 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 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](/docs/fundamentals/lifecycle/deactivation) will occur for each of them. Container deactivation and [activation handlers](/docs/fundamentals/lifecycle/activation) will also be removed. Use the `unloadAsync` method to unload when there will be an async deactivation handler or async [preDestroy](/docs/api/decorator#predestroy). +When a container module is unloaded from a Container, the bindings added by that container will be removed, and the [deactivation process](/docs/fundamentals/lifecycle/deactivation) will occur for each of them. Container deactivation and [activation handlers](/docs/fundamentals/lifecycle/activation) will also be removed. {containerModuleApiExampleSource} diff --git a/packages/docs/tools/inversify-code-examples/src/examples/v7/containerModuleApiExample.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/v7/containerModuleApiExample.int.spec.ts new file mode 100644 index 00000000..5969aece --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/v7/containerModuleApiExample.int.spec.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from '@jest/globals'; + +import { Ninja, ninjaPromise, Shuriken } from './containerModuleApiExample'; + +describe('ContainerModule API', () => { + it('should provide expected service', async () => { + const ninja: Ninja = await ninjaPromise; + + expect(ninja).toBeInstanceOf(Ninja); + expect(ninja.weapon).toBeInstanceOf(Shuriken); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/v7/containerModuleApiExample.ts b/packages/docs/tools/inversify-code-examples/src/examples/v7/containerModuleApiExample.ts new file mode 100644 index 00000000..5706a59d --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/v7/containerModuleApiExample.ts @@ -0,0 +1,59 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import { + Container, + ContainerModule, + ContainerModuleLoadOptions, + inject, + injectable, + named, +} from 'inversify7'; + +interface Weapon { + damage: number; +} + +@injectable() +export class Katana implements Weapon { + public readonly damage: number = 10; +} + +@injectable() +export class Shuriken implements Weapon { + public readonly damage: number = 5; +} + +@injectable() +export class Ninja { + @inject('Weapon') + @named('Ranged') + public readonly weapon!: Weapon; +} + +const container: Container = new Container(); + +const scriptExecution: Promise = (async () => { + // Begin-example + const warriorsModule: ContainerModule = new ContainerModule( + (options: ContainerModuleLoadOptions) => { + options.bind('Ninja').to(Ninja); + }, + ); + + const weaponsModule: ContainerModule = new ContainerModule( + (options: ContainerModuleLoadOptions) => { + options.bind('Weapon').to(Katana).whenNamed('Melee'); + options.bind('Weapon').to(Shuriken).whenNamed('Ranged'); + }, + ); + + await container.load(warriorsModule, weaponsModule); + + const ninja: Ninja = container.get('Ninja'); + // End-example +})(); + +const ninjaPromise: Promise = scriptExecution.then(() => + container.get('Ninja'), +); + +export { ninjaPromise };