How to upgrade from 4.x to 5.x

How to upgrade from 2.x to 3.x

  • The 2.x Kernel is named Container in 3.x

  • The 2.x Kernel method getServiceIdentifierAsString is not a method of Container in 3.x.

  • The 2.x PlanAndResolveArgs interface is named NextArgs in 3.0 and some of its properties have changed.

  • The Provider signature has been modified.

  • In 3.x, strictNullChecks is enabled.

  • The resolution logic in 2.0 and 3.0 is slightly different in order to support new features like optional dependencies and defaults contextual injections.

How to upgrade from 1.x to 2.x

Version 2.x introduces some changes in the API.

Naming changes

The 1.x TypeBinding is named Binding in 2.x

The 1.x BindingScopeEnum is named BindingScope in 2.x

Fluent binding syntax

The 1.x binding syntax looks as follows:

container.bind(new TypeBinding<FooInterface>("FooInterface", Foo, BindingScopeEnum.Transient));

The 2.x binding syntax looks as follows:

container.bind<FooInterface>("FooInterface").to(Foo).inTransientScope()

Resolution syntax

The 1.x container.resolve<T>(identifier: string) method is now container.get<T>(identifier: string) 2.x.

The 1.x resolution syntax looks as follows:

var foobar = container.resolve<FooBarInterface>("FooBarInterface");

The 2.x resolution syntax looks as follows:

var foobar = container.get<FooBarInterface>("FooBarInterface");

@injectable & @inject

All your classes must be decorated with the @injectable() decorator. If your class has a dependency in a class that's enough:

@injectable()
class Katana {
    public hit() {
        return "cut!";
    }
}

@injectable()
class Shuriken {
    public throw() {
        return "hit!";
    }
}

@injectable()
class Ninja implements Ninja {

    private _katana: Katana;
    private _shuriken: Shuriken;

    public constructor(
        katana: Katana,
        shuriken: Shuriken
    ) {
        this._katana = katana;
        this._shuriken = shuriken;
    }

    public fight() { return this._katana.hit(); };
    public sneak() { return this._shuriken.throw(); };

}

But if your class has a dependency on an interface you will also need to use the @inject decorator.

@injectable()
class Ninja implements Ninja {

    private _katana: Katana;
    private _shuriken: Shuriken;

    public constructor(
        @inject("Katana") katana: Katana,
        @inject("Shuriken") shuriken: Shuriken
    ) {
        this._katana = katana;
        this._shuriken = shuriken;
    }

    public fight() { return this._katana.hit(); };
    public sneak() { return this._shuriken.throw(); };

}

results matching ""

    No results matching ""