Skip to content

Commit

Permalink
fix: change the way container options are injected
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Aug 14, 2023
1 parent 7c40402 commit fcd2c7a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
41 changes: 35 additions & 6 deletions docs/overview/app-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,51 @@ Taking advantage of InversifyJS we created a wrapper to reduce complexity on how

## Creating the container

Upon creating the application container it is possible to define the default scope of the container. The default scope is `RequestScope` which means that all the dependencies will be created once per request. This is the common scope for most web applications used in other frameworks such as Spring Boot or .NET Core as well.
Upon creating the application container it is possible to define the default scope of the container as well as to set to skip base class check. The default scope is `RequestScope` which means that all the dependencies will be created once per request. This is the common scope for most web applications used in other frameworks such as Spring Boot or .NET Core as well.

Here is the interface options definition:

```typescript
interface ContainerOptions {
/**
* The default scope for bindings in the container.
* It can be set to Request (default), Singleton, or Transient.
*/
defaultScope?: interfaces.BindingScope;

/**
* Allows skipping of base class checks when working with derived classes.
*/
skipBaseClassChecks?: boolean;
}
```

Here is an example on how to create a container:

```typescript
// Adding options to the container
const appContainer = new AppContainer({
defaultScope: BindingScopeEnum.Singleton,
skipBaseClassChecks: true,
});

// Creating a container without options
const appContainer = new AppContainer();

// Creating a container module manager
const container = appContainer.create([
// Register all the modules
// Add your modules here
AppModule,
]);

console.log(appContainer.getContainerOptions());

export { container };
```

## Defining the container scope

As mentioned above, if the `defaultScope` is not provided, the default is set to RequestScope. However, it is possible to change the default scope by passing the `defaultScope` as a second argument to the module injection. The BindingScopeEnum is an enum that contains the following values:
As mentioned above, if the `defaultScope` is not provided, the default is set to RequestScope. However, it is possible to change the default scope by passing the `defaultScope` as a an option in the container constructor. The BindingScopeEnum is an enum that contains the following values:

- `BindingScopeEnum.Singleton` - The dependency will be created once and will be shared across all requests.
- `BindingScopeEnum.Request` - The dependency will be created once per request.
Expand All @@ -38,13 +68,12 @@ const appContainer = new AppContainer();
const container = appContainer.create(
[
// Add your modules here
],
BindingScopeEnum.Singleton,
]
);
```

:::tip
If you don't pass the `defaultScope` as a second argument, the default scope is set to `RequestScope`.
If you don't pass the `defaultScope` the default scope is set to `RequestScope`.
:::

## Registering modules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,51 @@ Aproveitando o InversifyJS, criamos um wrapper para reduzir a complexidade sobre

## Creating the container

Ao criar o container da aplicação é possível definir o escopo padrão do container. O escopo padrão é `RequestScope`, o que significa que todas as dependências serão criadas uma vez por solicitação. Esse é o escopo comum para a maioria dos aplicativos da Web usados em outras estruturas, como Spring Boot ou .NET Core.
Ao criar o contêiner da aplicação, é possível definir o escopo padrão do contêiner e também configurar para ignorar a verificação da classe base. O escopo padrão é `RequestScope`, o que significa que todas as dependências serão criadas uma vez por solicitação. Esse é o escopo comum para a maioria das aplicações web usadas em outros frameworks, como Spring Boot ou .NET Core.

Aqui está a definição das opções de interface:

```typescript
interface ContainerOptions {
/**
* O escopo padrão para as ligações no contêiner.
* Pode ser definido como Request (padrão), Singleton ou Transient.
*/
defaultScope?: interfaces.BindingScope;

/**
* Permite ignorar as verificações da classe base ao trabalhar com classes derivadas.
*/
skipBaseClassChecks?: boolean;
}
```

Aqui está um exemplo de como criar um container:

```typescript
// Adicionando opções ao contêiner
const appContainer = new AppContainer({
defaultScope: BindingScopeEnum.Singleton,
skipBaseClassChecks: true,
});

// Criando um contêiner sem opções
const appContainer = new AppContainer();

// Criando um gerenciador de módulos de contêiner
const container = appContainer.create([
// Registrar todos os módulos
// Adicione seus módulos aqui
AppModule,
]);

console.log(appContainer.getContainerOptions());

export { container };
```

## Defining the container scope

Conforme mencionado acima, se o `defaultScope` não for fornecido, o padrão será definido como RequestScope. No entanto, é possível alterar o escopo padrão passando o `defaultScope` como um segundo argumento para a injeção do módulo. O BindingScopeEnum é uma enumeração que contém os seguintes valores:
Como mencionado acima, se o `defaultScope` não for fornecido, o padrão é definido como RequestScope. No entanto, é possível alterar o escopo padrão passando o `defaultScope` como uma opção no construtor do contêiner. Isso permite flexibilidade na configuração do contêiner, de modo que ele possa ser personalizado para atender às necessidades específicas da aplicação.

- `BindingScopeEnum.Singleton` - A dependência será criada uma vez e será compartilhada entre todas as solicitações.
- `BindingScopeEnum.Request` - A dependência será criada uma vez por solicitação.
Expand All @@ -38,13 +68,12 @@ const appContainer = new AppContainer();
const container = appContainer.create(
[
// Registrar todos os módulos
],
BindingScopeEnum.Singleton,
]
);
```

:::tip
Se você não passar o `defaultScope` como um segundo argumento, o escopo padrão será definido como `RequestScope`.
Se você não passar o `defaultScope` o escopo padrão será definido como `RequestScope`.
:::

## Registering modules
Expand Down

0 comments on commit fcd2c7a

Please sign in to comment.