Yet another dependency injection
container for typescript
# install libs
npm i -S reflect-metadata @newdash/inject
// tsconfig.json
// remember enable decorator related flags
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
// import reflect lib firstly
import "reflect-metadata";
import { inject } from "@newdash/inject"
// a really simple example
it('should support deep constructor injection', async () => {
class A {
v: number
constructor(@inject("v") v) {
this.v = v;
}
}
class B {
a: InjectWrappedInstance<A>
constructor(@inject(A) a) {
this.a = a;
}
}
const ic = InjectContainer.New();
ic.registerInstance("v", 999); // define an instance provider in simple way
const b = await ic.getInstance(B);
expect(b.a.v).toBe(999);
});