Container
Container is a Brandi class whose instances store information about dependencies,
resolve dependencies, and inject dependencies into targets.
Container Methods
bind(token)
Binds the token to an implementation.
Arguments
token:Token— a token to be bound.
Returns
Binding Type syntax:
use(...tokens).from(module)
Uses bindings from a dependency module.
Arguments
use(...tokens)
...tokens:Token[]— tokens to be used from a dependency module.
use(...tokens).from(module)
module:DependencyModule— a dependency module.
when(condition)
Creates a conditional binding.
Arguments
condition:Tag|UnknownCreator— a condition.
Returns
bind or use syntax:
extend(container)
Sets the parent container. For more information, see Hierarchical Containers section.
Arguments
container:Container | null— aContainerornullthat will be set as the parent container.
Returns
this — the container.
get(token)
Gets a dependency bound to the token.
Arguments
token:TokenValue— token for which a dependence will be got.
Returns
TokenType<TokenValue> — a dependency bound to the token.
clone()
Returns an unlinked clone of the container.
Returns
Container — new container.
Example
import { Container } from 'brandi';
const originalContainer = new Container();
const containerClone = originalContainer.clone();
expect(containerClone).toBeInstanceOf(Container);
expect(containerClone).not.toBe(originalContainer);
capture()
Captures (snapshots) the current container state.
The capture() method works only in development mode (process.env.NODE_ENV !== 'production').
Container.capture is undefined in production mode.
restore()
Restores the captured container state.
The restore() method works only in development mode (process.env.NODE_ENV !== 'production').
Container.restore is undefined in production mode.
Example
import { Container, token } from 'brandi';
const TOKENS = {
apiKey: token<string>('API Key'),
};
const container = new Container();
container.bind(TOKENS.apiKey).toConstant('#key9428');
container.capture();
container.bind(TOKENS.apiKey).toConstant('#testKey');
const testKey = container.get(TOKENS.apiKey);
container.restore();
const originalKey = container.get(TOKENS.apiKey);
expect(testKey).toBe('#testKey');
expect(originalKey).toBe('#key9428');
createContainer()
createContainer() — is alias for new Container().