README
sloth-ts-injection
Sloth Dependency Injection For Typescript
Installation
You can get the latest release and the type definitions using npm:
$ npm install sloth-ts-injection reflect-metadata --save
/!\ sloth-ts-injection requires TypeScript >= 2.0, your tsconfig should look similar to the one below (experimentalDecorators, emitDecoratorMetadata are important)
{
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
"types": ["reflect-metadata"],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
Getting Started
You can declare dependencies with @slothInject() decorator.
import { slothInject } from 'sloth-ts-injection';
@slothInject()
export class DependencyA {
public data: number = 0;
}
You can then require this dependency.
import { slothInject } from 'sloth-ts-injection';
import { DependencyA } from "./dependencyA";
@slothInject()
export class DependencyD {
constructor(public a: DependencyA ) { }
public update() {
this.a.data++;
}
}
sloth-ts-injection will resolve the dependencies needed for you
import { Injector } from 'sloth-ts-injection';
const injector = new Injector();
const dep: DependencyD = injector.inject(DependencyD);
dep.update();