@vladbasin/ts-dependencies
v1.0.5
Published
TypeScript dependency management without magic.
Downloads
6
Maintainers
Readme
ts-dependencies
TypeScript dependency management without magic.
Install
npm install @vladbasin/ts-dependencies --save
Getting Started
This library helps to inject dependencies which are singletons (useful for decoupling business services accross the app).
Given these classes
class Engine {
private _speed: number = 10;
public get speed() {
return this._speed;
}
}
class Car {
private _engine: Engine;
constructor(dep: { engine: Engine }) {
this._engine = dep.engine;
}
public printSpeed() {
console.log(this._engine.speed);
}
}
Register dependencies
import { ServiceCollectionBuilder } from "@vladbasin/ts-dependencies";
const serviceCollectionBuilder = new ServiceCollectionBuilder();
serviceCollectionBuilder.addPure("engine", Engine);
serviceCollectionBuilder.add("car", Car, ["engine"]);
const services = serviceCollectionBuilder.build();
Resolve dependency
const car = services.get<Car>("car");
car.printSpeed();