Old documentation
NOTE: This page is a direct copy of the old documentation. It will be reworked.
In your class's constructor you always receive as a last argument a container which you can use to get other dependencies.
class BeanFactory {
create() {}
}
class SugarFactory {
create() {}
}
class WaterFactory {
create() {}
}
class CoffeeMaker {
constructor(container) {
this.beanFactory = container.get(BeanFactory);
this.sugarFactory = container.get(SugarFactory);
this.waterFactory = container.get(WaterFactory);
}
make() {
this.beanFactory.create();
this.sugarFactory.create();
this.waterFactory.create();
}
}
var Container = require('typedi').Container;
var coffeeMaker = Container.get(CoffeeMaker);
coffeeMaker.make();With TypeDI you can use a named services. Example:
This feature especially useful if you want to store (and inject later on) some settings or configuration options. For example:
When you write tests you can easily provide your own "fake" dependencies to classes you are testing using set method:
TypeDI also supports a function dependency injection. Here is how it looks like:
Last updated
Was this helpful?