File tree Expand file tree Collapse file tree 2 files changed +2
-84
lines changed Expand file tree Collapse file tree 2 files changed +2
-84
lines changed Original file line number Diff line number Diff line change 1
1
# Documentation
2
2
3
- TypeDI is a [ dependency injection] ( https://en.wikipedia.org/wiki/Dependency_injection ) tool for JavaScript and TypeScript.
4
- Using TypeDI you can build well-structured and easily tested applications.
5
-
6
3
## Typescript Usage
7
4
8
- You can use ** property injection** and inject services into your class using ` @Inject ` decorator:
9
-
10
- ``` typescript
11
- import { Container , Inject , Service } from ' typedi' ;
12
-
13
- @Service ()
14
- class BeanFactory {
15
- create() {}
16
- }
17
-
18
- @Service ()
19
- class SugarFactory {
20
- create() {}
21
- }
22
-
23
- @Service ()
24
- class WaterFactory {
25
- create() {}
26
- }
27
-
28
- @Service ()
29
- class CoffeeMaker {
30
- @Inject ()
31
- beanFactory: BeanFactory ;
32
-
33
- @Inject ()
34
- sugarFactory: SugarFactory ;
35
-
36
- @Inject ()
37
- waterFactory: WaterFactory ;
38
-
39
- make() {
40
- this .beanFactory .create ();
41
- this .sugarFactory .create ();
42
- this .waterFactory .create ();
43
- }
44
- }
45
-
46
- let coffeeMaker = Container .get (CoffeeMaker );
47
- coffeeMaker .make ();
48
- ```
49
-
50
- You can also use a constructor injection:
51
-
52
- ``` typescript
53
- import { Container , Service } from ' typedi' ;
54
-
55
- @Service ()
56
- class BeanFactory {
57
- create() {}
58
- }
59
-
60
- @Service ()
61
- class SugarFactory {
62
- create() {}
63
- }
64
-
65
- @Service ()
66
- class WaterFactory {
67
- create() {}
68
- }
69
-
70
- @Service ()
71
- class CoffeeMaker {
72
- constructor (
73
- private beanFactory : BeanFactory ,
74
- private sugarFactory : SugarFactory ,
75
- private waterFactory : WaterFactory
76
- ) {}
77
-
78
- make() {
79
- this .beanFactory .create ();
80
- this .sugarFactory .create ();
81
- this .waterFactory .create ();
82
- }
83
- }
84
-
85
- let coffeeMaker = Container .get (CoffeeMaker );
86
- coffeeMaker .make ();
87
- ```
88
-
89
5
With TypeDI you can use a named services. Example:
90
6
91
7
``` typescript
Original file line number Diff line number Diff line change 1
1
# Getting Started
2
2
3
+ TypeDI is a [ dependency injection] ( https://en.wikipedia.org/wiki/Dependency_injection ) library for TypeScript and JavaScript.
4
+
3
5
## Installation
4
6
5
7
> Note: This installation guide is for usage with TypeScript, if you wish to use TypeDI without Typescript
You can’t perform that action at this time.
0 commit comments