TypeDI
Search…
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.
1
class BeanFactory {
2
create() {}
3
}
4
5
class SugarFactory {
6
create() {}
7
}
8
9
class WaterFactory {
10
create() {}
11
}
12
13
class CoffeeMaker {
14
constructor(container) {
15
this.beanFactory = container.get(BeanFactory);
16
this.sugarFactory = container.get(SugarFactory);
17
this.waterFactory = container.get(WaterFactory);
18
}
19
20
make() {
21
this.beanFactory.create();
22
this.sugarFactory.create();
23
this.waterFactory.create();
24
}
25
}
26
27
var Container = require('typedi').Container;
28
var coffeeMaker = Container.get(CoffeeMaker);
29
coffeeMaker.make();
Copied!
With TypeDI you can use a named services. Example:
1
var Container = require('typedi').Container;
2
3
class BeanFactory implements Factory {
4
create() {}
5
}
6
7
class SugarFactory implements Factory {
8
create() {}
9
}
10
11
class WaterFactory implements Factory {
12
create() {}
13
}
14
15
class CoffeeMaker {
16
beanFactory: Factory;
17
sugarFactory: Factory;
18
waterFactory: Factory;
19
20
constructor(container) {
21
this.beanFactory = container.get('bean.factory');
22
this.sugarFactory = container.get('sugar.factory');
23
this.waterFactory = container.get('water.factory');
24
}
25
26
make() {
27
this.beanFactory.create();
28
this.sugarFactory.create();
29
this.waterFactory.create();
30
}
31
}
32
33
Container.set('bean.factory', new BeanFactory(Container));
34
Container.set('sugar.factory', new SugarFactory(Container));
35
Container.set('water.factory', new WaterFactory(Container));
36
Container.set('coffee.maker', new CoffeeMaker(Container));
37
38
var coffeeMaker = Container.get('coffee.maker');
39
coffeeMaker.make();
Copied!
This feature especially useful if you want to store (and inject later on) some settings or configuration options. For example:
1
var Container = require('typedi').Container;
2
3
// somewhere in your global app parameters
4
Container.set('authorization-token', 'RVT9rVjSVN');
5
6
class UserRepository {
7
constructor(container) {
8
this.authorizationToken = container.get('authorization-token');
9
}
10
}
Copied!
When you write tests you can easily provide your own "fake" dependencies to classes you are testing using set method:
1
Container.set(CoffeeMaker, new FakeCoffeeMaker());
2
3
// or for named services
4
5
Container.set([
6
{ id: 'bean.factory', value: new FakeBeanFactory() },
7
{ id: 'sugar.factory', value: new FakeSugarFactory() },
8
{ id: 'water.factory', value: new FakeWaterFactory() },
9
]);
Copied!
TypeDI also supports a function dependency injection. Here is how it looks like:
1
var Service = require('typedi').Service;
2
var Container = require('typedi').Container;
3
4
var PostRepository = Service(() => ({
5
getName() {
6
return 'hello from post repository';
7
},
8
}));
9
10
var PostManager = Service(() => ({
11
getId() {
12
return 'some post id';
13
},
14
}));
15
16
class PostQueryBuilder {
17
build() {
18
return 'SUPER * QUERY';
19
}
20
}
21
22
var PostController = Service([PostManager, PostRepository, PostQueryBuilder], (manager, repository, queryBuilder) => {
23
return {
24
id: manager.getId(),
25
name: repository.getName(),
26
query: queryBuilder.build(),
27
};
28
});
29
30
var postController = Container.get(PostController);
31
console.log(postController);
Copied!
Last modified 1mo ago
Copy link