guran-injector
v0.0.1
Published
Typescript DI lib
Downloads
2
Readme
Guran Injector
Typescript DI lib
Install
npm install guran-injector --save
Config tsconfig.json
{
"compilerOptions":{
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
}
}
Usage
Decorators
Injectable - enable DI for class
@Injectable([provider: Object])
export class ClassOne {
one() {
return 1;
}
}
Inject - manual mechanism for letting Guran know that a parameter must be injected
constructor(@Inject([class | {useValue: ...} | {useFactory: ()=>...}]) varitable: ClassOne){}
export class ClassOne {
one() {
return 1;
}
}
export class ClassOneMock extends ClassOne{
one() {
return 1000;
}
}
@Injectable()
export class ClassTwo {
constructor(@Inject(ClassOneMock) private classOne: ClassOne) {
}
two() {
return this.classOne.one() * 2;
}
}
Core
Injector.invoke() - get instance class with DI
Injector.invoke<ClassTwo>(ClassTwo).two() => 2000
Injector.addProvider() - add provider to Guran registry
Injector.addProvider(ClassOne, {useValue: new ClassOneMock()});
Injector.invoke<ClassOne>(ClassOne).one() => 1000
Injector.removeProvider() - remove provider from Guran registry
Injector.invoke<ClassOne>(ClassOne).one() => 1000;
Injector.removeProvider(ClassOne);
Injector.invoke<ClassOne>(ClassOne).one() => 1
Injector.getProvider() - get provider from Guran registry
Injector.getProvider(ClassOne) => {useValue: obj=>ClassOne}
or
Injector.getProvider(ClassOne) => {useFactory: ()=>new ClassOne()}