Skip to main content

Basic Examples

Here are just basic examples.

Getting Instance#

Binding types and scopes are detailed in Binding Types and Binding Scopes sections of the documentation.

import { Container, token } from 'brandi';
class ApiService {}
const TOKENS = {
/* ↓ Creates a typed token. */
apiService: token<ApiService>('apiService'),
};
const container = new Container();
container
.bind(TOKENS.apiService)
.toInstance(ApiService) /* ← Binds the token to an instance */
.inTransientScope(); /* ← in transient scope. */
/* ↓ Gets the instance from the container. */
const apiService = container.get(TOKENS.apiService);
expect(apiService).toBeInstanceOf(ApiService);

Snapshoting#

import { Container, token } from 'brandi';
const TOKENS = {
apiKey: token<string>('API Key'),
};
const container = new Container();
container
.bind(TOKENS.apiKey)
.toConstant('#key9428'); /* ← Binds the token to some string. */
/* ↓ Captures (snapshots) the current container state. */
container.capture();
container
.bind(TOKENS.apiKey)
.toConstant('#testKey'); /* ← Binds the same token to another value. */
/* For example, this can be used in testing. */
const testKey = container.get(TOKENS.apiKey);
/* ↓ Restores the captured container state. */
container.restore();
const originalKey = container.get(TOKENS.apiKey);
expect(testKey).toBe('#testKey');
expect(originalKey).toBe('#key9428');

Other Container methods are detailed in Container section of the documentation.

Hierarchical Containers#

Hierarchical containers are detailed in Hierarchical Containers section of the documentation.

import { Container, token } from 'brandi';
class ApiService {}
const TOKENS = {
apiService: token<ApiService>('apiService'),
};
const parentContainer = new Container();
parentContainer
.bind(TOKENS.apiService)
.toInstance(ApiService)
.inTransientScope();
/* ↓ Creates a container with the parent. */
const childContainer = new Container().extend(parentContainer);
/** ↓ That container can't satisfy the getting request,
* it passes it along to its parent container.
* The intsance will be gotten from the parent container.
*/
const apiService = childContainer.get(TOKENS.apiService);
expect(apiService).toBeInstanceOf(ApiService);