Inversify Binding Decorators
Inversify Binding Decorators is a utility library for InversifyJS, a powerful Inversion of Control (IoC) container for TypeScript and JavaScript applications. It simplifies the process of declaring dependency injection bindings by allowing developers to use ES2016 decorators directly on classes. This contrasts with InversifyJS's standard fluent API for binding. The current stable version is 4.0.0, which typically aligns with major versions of InversifyJS itself, indicating a release cadence tied to its core dependency. Its primary differentiator is the `@provide` decorator, enabling a more declarative and less verbose way to register components with the InversifyJS container, particularly useful in large applications with many services.
Common errors
-
Cannot apply @injectable decorator multiple times. Please use @provide(ID, true) if you are trying to declare multiple bindings!
cause Attempted to apply the `@provide` decorator more than once to a single class without explicitly allowing multiple bindings.fixIf you intend to provide the class under multiple identifiers, use `@provide(identifier, true)` for each additional binding. For example, `@provide("Logger", true)` and `@provide("ConsoleLogger", true)`. -
Error: Missing required @injectable annotation in: ClassName
cause A class intended for dependency injection via `@provide` was not also decorated with `@injectable` from `inversify`.fixEnsure that any class decorated with `@provide` also has the `@injectable()` decorator from `inversify` applied to it, e.g., `@injectable() @provide(MyService) class MyService { ... }`.
Warnings
- breaking Major versions of `inversify-binding-decorators` (e.g., 2.x, 3.x, 4.x) are tightly coupled to corresponding major versions of `inversify`. Upgrading `inversify` may necessitate upgrading this library and vice-versa, potentially involving breaking changes in the core InversifyJS API.
- gotcha Applying the `@provide` decorator multiple times to a single class without explicit permission will throw an error to prevent accidental duplicate bindings.
- gotcha The `reflect-metadata` polyfill must be imported exactly once at the entry point of your application to enable TypeScript's decorator metadata reflection, which is crucial for both InversifyJS and `inversify-binding-decorators` to function correctly.
Install
-
npm install inversify-binding-decorators -
yarn add inversify-binding-decorators -
pnpm add inversify-binding-decorators
Imports
- provide
import provide from 'inversify-binding-decorators';
import { provide } from 'inversify-binding-decorators'; - buildProviderModule
const { buildProviderModule } = require('inversify-binding-decorators');import { buildProviderModule } from 'inversify-binding-decorators'; - injectable
import { injectable, Container } from 'inversify';
Quickstart
import { injectable, Container } from "inversify";
import { provide, buildProviderModule } from "inversify-binding-decorators";
import "reflect-metadata"; // Must be imported once at the application entry point
interface Weapon { hit(): string; }
interface ThrowableWeapon { throw(): string; }
@injectable()
@provide(Katana)
class Katana implements Weapon {
public hit() {
return "cut!";
}
}
@injectable()
@provide(Shuriken)
class Shuriken implements ThrowableWeapon {
public throw() {
return "hit!";
}
}
const container = new Container();
// Load all bindings declared via @provide decorators
container.load(buildProviderModule());
// Resolve instances from the container
const katana = container.get<Weapon>(Katana);
const shuriken = container.get<ThrowableWeapon>(Shuriken);
console.log(`Katana: ${katana.hit()}`);
console.log(`Shuriken: ${shuriken.throw()}`);