Skip to main content

Binding

A binding represents the relationship between a service identifier and its resolution. Bindings are added to a container to configure it to provide services.

Binding properties

A binding has the following properties:

Service identifier

The identifier of the service for which a resolution is provided.

Scope

The scope determines the caching strategy used to decide whether the service should be resolved or a cached value should be provided.

Request

When the service is resolved within the same container.get request, the same resolved value will be used.

export class LegendaryWarrior {
constructor(
@inject('Weapon') public readonly firstWeapon: Weapon,
@inject('Weapon') public readonly secondWeapon: Weapon,
@inject('Weapon') public readonly thirdWeapon: Weapon,
) {}
}

const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana).inRequestScope();
container.bind(LegendaryWarrior).toSelf();

const firstKatana: Weapon = container.get<Weapon>('Weapon');
const secondKatana: Weapon = container.get<Weapon>('Weapon');

const legendaryWarrior: LegendaryWarrior = container.get(LegendaryWarrior);

// Returns false
const isSameKatana: boolean = firstKatana === secondKatana;

// Returns true
const warriorHasSameKatana: boolean =
legendaryWarrior.firstWeapon === legendaryWarrior.secondWeapon &&
legendaryWarrior.secondWeapon === legendaryWarrior.thirdWeapon;

Singleton

When the service is resolved, the same cached resolved value will be used.

const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana).inSingletonScope();

const firstKatana: Weapon = container.get<Weapon>('Weapon');
const secondKatana: Weapon = container.get<Weapon>('Weapon');

// Returns true
const isSameKatana: boolean = firstKatana === secondKatana;

Transient

When the service is resolved, a new resolved value will be used each time.

const container: Container = new Container();
container.bind<Weapon>('Weapon').to(Katana).inTransientScope();

const firstKatana: Weapon = container.get<Weapon>('Weapon');
const secondKatana: Weapon = container.get<Weapon>('Weapon');

// Returns false
const isSameKatana: boolean = firstKatana === secondKatana;

Constraint

Specifies whether the binding is used to provide a resolved value for the given service identifier. Refer to the API docs for more information.

Lifecycle handlers

Handlers that are called after a resolved value is provided or a singleton-scoped binding is deactivated.