2.0.1 • Published 3 years ago

injectant v2.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

injectant

Dependency injection implementation in TypeScript, based on nehalist.io/dependency-injection-in-typescript.

Install

npm install injectant --save

Peer Dependencies

Reflection (any of):

Usage

import '@abraham/reflection';
import { Service, Injectable, Injector, Inject } from 'injectant';

@Service()
class Foo {}

@Injectable() // Injectable is alias of Service
class Bar {}

@Service()
class Foobar {
    constructor(public foo: Foo, public bar: Bar) {}
}

@Service()
class Baz {
    constructor(public foobar: Foobar) {}
}

@Injectable()
class Engine {}

class Car {
    @Inject(Engine) engine; // Property injection
}

let baz = Injector.resolve(Baz);
console.log(baz.foobar); // instance of Foobar
console.log(baz.foobar.foo); // instance of Foo
console.log(baz.foobar.bar); // instance of Bar

const car = Injector.resolve(Car);
console.log(car.engine); // instance of Engine

Usage in unit tests

describe('Suite', () => {
    it('test baz', () => {
        Injector.provide(Foobar, MockFooBar);
        let baz = Injector.resolve(Baz);
        console.log(baz.foobar); // instance of MockFooBar
    });
});