Was the angular2 dependency injection container designed for standalone use, and is it possible to use it for typescript/javascript server-side applications ?
I open
At the moment the Angular 2.0 DI code doesn't seem to be ready to be consumed by other libraries.
I would like to suggest an alternative. I have developed an IoC container called InversifyJS with advanced dependency injection features like contextual bindings. It works in both node and browsers and some parts of its API are based on the Angular 2 DI API.
You need to follow 3 basic steps to use it:
The annotation API is based on Angular 2.0:
import { injectable, inject } from "inversify";
@injectable()
class Katana implements IKatana {
public hit() {
return "cut!";
}
}
@injectable()
class Shuriken implements IShuriken {
public throw() {
return "hit!";
}
}
@injectable()
class Ninja implements INinja {
private _katana: IKatana;
private _shuriken: IShuriken;
public constructor(
@inject("IKatana") katana: IKatana,
@inject("IShuriken") shuriken: IShuriken
) {
this._katana = katana;
this._shuriken = shuriken;
}
public fight() { return this._katana.hit(); };
public sneak() { return this._shuriken.throw(); };
}
The binding API is based on Ninject:
import { Kernel } from "inversify";
import { Ninja } from "./entities/ninja";
import { Katana } from "./entities/katana";
import { Shuriken} from "./entities/shuriken";
var kernel = new Kernel();
kernel.bind("INinja").to(Ninja);
kernel.bind("IKatana").to(Katana);
kernel.bind("IShuriken").to(Shuriken);
export default kernel;
The resolution API is based on Ninject:
import kernel = from "./inversify.config";
var ninja = kernel.get("INinja");
expect(ninja.fight()).eql("cut!"); // true
expect(ninja.sneak()).eql("hit!"); // true
The latest release (2.0.0) supports many use cases:
You can learn more about it at https://github.com/inversify/InversifyJS