Skip to content

Commit

Permalink
feat(inversify-site): add container module API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Dec 31, 2024
1 parent 5c241d8 commit ee58b26
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
sidebar_position: 2
title: Container Module
---
import containerModuleApiExampleSource from '@inversifyjs/code-examples/generated/examples/containerModuleApiExample.ts.txt';
import CodeBlock from '@theme/CodeBlock';

# ContainerModule

Container modules can help you to manage the complexity of your bindings in large applications.

## Constructor

```ts
constructor(registry: interfaces.ContainerModuleCallBack)
```

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

### bind

```ts
bind: interfaces.Bind
```

Consider [docs](/docs/api/container#bind) as reference.

### unbind

```
unbind: interfaces.Unbind
```

Consider [docs](/docs/api/container#unbind) as reference.

### isBound

```ts
isBound: interfaces.IsBound
```

Consider [docs](/docs/api/container#isbound) as reference.

### rebind

```ts
rebind: interfaces.Rebind
```

Consider [docs](/docs/api/container#rebind) as reference.

### unbindAsync

```ts
unbindAsync: interfaces.UnbindAsync
```

Consider [docs](/docs/api/container#unbindasync) as reference.

### onActivation

```ts
onActivation: interfaces.Container['onActivation']
```

Consider [docs](/docs/api/container#onactivation) as reference.

### onDeactivation

```ts
onDeactivation: interfaces.Container['onDeactivation']
```

Consider [docs](/docs/api/container#ondeactivation) as reference.

## 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](https://github.com/inversify/InversifyJS/blob/master/wiki/deactivation_handler.md) will occur for each of them. Container deactivation and [activation handlers](https://github.com/inversify/InversifyJS/blob/master/wiki/activation_handler.md) will also be removed.
Use the unloadAsync method to unload when there will be an async deactivation handler or async [pre destroy](https://github.com/inversify/InversifyJS/blob/master/wiki/pre_destroy.md)

<CodeBlock language="ts">{containerModuleApiExampleSource}</CodeBlock>
13 changes: 6 additions & 7 deletions packages/docs/services/inversify-site/docs/api/container.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import containerApiRebindSource from '@inversifyjs/code-examples/generated/examp
import containerApiResolveSource from '@inversifyjs/code-examples/generated/examples/containerApiResolve.ts.txt';
import CodeBlock from '@theme/CodeBlock';

# The Container API
# Container

The InversifyJS container is where dependencies are first configured through bind and, possibly later, reconfigured and removed. The container can be worked on directly in this regard or container modules can be utilized.
You can query the configuration and resolve configured dependencies with resolved and the 'get' methods.
Expand Down Expand Up @@ -292,7 +292,7 @@ You can use the `isBoundTagged` method to check if there are registered bindings
load(...modules: interfaces.ContainerModule[]): void;
```

Calls the registration method of each module. See [container modules](https://github.com/inversify/InversifyJS/blob/master/wiki/container_modules.md)
Calls the registration method of each module. See [ContainerModule API docs](/docs/api/container-module)

## loadAsync

Expand All @@ -308,7 +308,7 @@ As per load but for asynchronous registration.
rebind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): interfaces.BindingToSyntax<T>;
```

You can use the `rebind` method to replace all the existing bindings for a given `serviceIdentifier`.
Replaces all the existing bindings for a given `serviceIdentifier`.
The function returns an instance of `BindingToSyntax` which allows to create the replacement binding.

<CodeBlock language="ts">{containerApiRebindSource}</CodeBlock>
Expand Down Expand Up @@ -478,8 +478,7 @@ Remove all bindings binded in this container to the service identifier. This wi
unbindAsync(serviceIdentifier: interfaces.ServiceIdentifier<unknown>): Promise<void>;
```

This is the asynchronous version of unbind. If you know deactivation is asynchronous then this should be used.
If you are not sure then use this method !
This is the asynchronous version of unbind. If any deactivation realated to this service identifier is asynchronous then this method should be used instead of `container.unbind`.

## unbindAll

Expand All @@ -503,8 +502,8 @@ This is the asynchronous version of unbindAll. If any of the container's deactiv
unload(...modules: interfaces.ContainerModuleBase[]): void;
```

Removes bindings and handlers added by the modules. This will result in the [deactivation process](https://github.com/inversify/InversifyJS/blob/master/wiki/deactivation_handler.md).
See [container modules](https://github.com/inversify/InversifyJS/blob/master/wiki/container_modules.md)
Removes bindings and handlers added by the modules. This will result in the [deactivation process](https://github.com/inversify/InversifyJS/blob/master/wiki/deactivation_handler.md).
See [ContainerModule API docs](/docs/api/container-module).

## unloadAsync

Expand Down

0 comments on commit ee58b26

Please sign in to comment.