Skip to main content

Pointers and Registrators

Pointers#

A pointer in Brandi terminology is a unique value that is used for relating entities.

token<T>(description)#

Creates a unique token with the type.

Arguments#

  1. description: string โ€” a description of the token to be used in logs and error messages.

Returns#

Token<T> โ€” a unique token with the type.

Type Safety#

The token mechanism in Brandi provides type safety when binding and getting dependencies.

import { Container, token } from 'brandi';
const TOKENS = {
apiKey: token<string>('API Key') /* โ† The token with `string` type. */,
};
const container = new Container();
/* โ†“ Binding the `string` type value. It's OK. */
container.bind(TOKENS.apiKey).toConstant('#key9428');
/**
* โ†“ Trying to bind the `string` type token to the `number` type value:
* TS Error: `Argument of type 'number' is not assignable
* to parameter of type 'string'. ts(2345)`.
*/
container.bind(TOKENS.apiKey).toConstant(9428);
const key = container.get(TOKENS.apiKey);
type Key = typeof key; /* โ† The type is derived from the token. `type Key = string;`. */
/* โ†“ TS Error: `Type 'string' is not assignable to type 'number'. ts(2322)` */
const numKey: number = container.get(TOKENS.apiKey);

tag(description)#

Creates a unique tag.

Arguments#

  1. description: string โ€” a description of the tag to be used in logs and error messages.

Returns#

Tag โ€” a unique tag.


Registrators#

injected(target, ...tokens)#

Registers target injections.

Arguments#

  1. target โ€” constructor or function whose dependencies will be injected.
  2. ...tokens: TokenValue[] โ€” dependency tokens.

Returns#

target โ€” the first argument.

Example#

ApiService.ts
import { injected } from 'brandi';
import { TOKENS } from './tokens';
import { Logger } from './Logger';
export class ApiService {
constructor(private apiKey: string, private logger?: Logger) {}
}
injected(ApiService, TOKENS.apiKey, TOKENS.logger.optional);

Type Safety#

Injections in Brandi are strictly typed.

tokens.ts
import { token } from 'brandi';
export const TOKENS = {
strKey: token<string>('String API Key'),
numKey: token<number>('Number API Key'),
};
ApiService.ts
import { injected } from 'brandi';
import { TOKENS } from './tokens';
import { Logger } from './Logger';
export class ApiService {
constructor(
private apiKey: string /* โ† The `string` type dependency. */,
private logger?: Logger /* โ† The optional dependency. */,
) {}
}
injected(
ApiService,
TOKENS.strKey /* โ† Injecting the `string` type dependency. It's OK. */,
TOKENS.logger.optional /* โ† Injecting the optional dependency. It's OK. */,
);
injected(
ApiService,
TOKENS.numKey, /* โ† Injecting the `number` type dependency.
* TS Error: `Type 'number' is not assignable to type 'string'. ts(2345)`.
*/
TOKENS.logger, /* โ† Injecting the required value instead of the optional one.
* TS Error: `Argument of type 'Token<Logger>'
* is not assignable to parameter of type 'OptionalToken<Logger>'`.
*/
);

tagged(target, ...tags)#

Tags target. For more information about tags, see the Conditional Bindings documentation section.

Arguments#

  1. target โ€” constructor or function that will be tagged.
  2. ...tags: Tag[] โ€” tags.

Returns#

target โ€” the first argument.

Example#

ApiService.ts
import { tagged } from 'brandi';
import { TAGS } from './tags';
export class ApiService {}
tagged(ApiService, TAGS.offline);