Getting started
Start by installing inversify
:
npm install inversify reflect-metadata
Initialize your first container and add some bindings:
warning
Experimental decorators and Emit Decorator Metadata options must be enabled in order to use this library.
import { Container, inject, injectable } from 'inversify';
@injectable()
class Katana {
public readonly damage: number = 10;
}
@injectable()
class Ninja {
constructor(
@inject(Katana)
public readonly katana: Katana,
) {}
}
const container: Container = new Container();
container.bind(Ninja).toSelf();
container.bind(Katana).toSelf();
const ninja: Ninja = container.get(Ninja);
console.log(ninja.katana.damage);
@injectable
allows both Katana
and Ninja
classes to be used as container bindings. @inject
provides metadata with Ninja
dependencies so the container is aware a Katana
should be provided as the first argument of Ninja
's constructor.
Bindings are provided through the Container
API.
Whith these two steps, we are ready to initialize our very first ninja!