Skip to content

Commit

Permalink
Merge pull request #320 from inversify/feat/update-next-container-mod…
Browse files Browse the repository at this point in the history
…ule-api-docs

Update next container module api docs
  • Loading branch information
notaphplover authored Jan 17, 2025
2 parents 58b2520 + 9ba3418 commit f9724dc
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 29 deletions.
56 changes: 27 additions & 29 deletions packages/docs/services/inversify-site/docs/api/container-module.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<void>)
```

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<void>
```

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: <T>(serviceIdentifier: ServiceIdentifier<T>) => BindToFluentSyntax<T>
```

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: <T>(serviceIdentifier: ServiceIdentifier<T>, activation: BindingActivation<T>) => 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: <T>(serviceIdentifier: ServiceIdentifier<T>, deactivation: BindingDeactivation<T>) => 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<void>
```

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.

<CodeBlock language="ts">{containerModuleApiExampleSource}</CodeBlock>
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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<void> = (async () => {
// Begin-example
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');
// End-example
})();

const ninjaPromise: Promise<Ninja> = scriptExecution.then(() =>
container.get('Ninja'),
);

export { ninjaPromise };

0 comments on commit f9724dc

Please sign in to comment.