|
3 | 3 | TypeDI is a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) tool for JavaScript and TypeScript.
|
4 | 4 | Using TypeDI you can build well-structured and easily tested applications.
|
5 | 5 |
|
6 |
| -- [Usage with JavaScript](#usage-with-javascript) |
7 |
| -- [Usage with TypeScript](#usage-with-typescript) |
8 |
| - |
9 |
| -## Usage with JavaScript |
10 |
| - |
11 |
| -Install the module: |
12 |
| - |
13 |
| -`npm install typedi --save` |
14 |
| - |
15 |
| -Now you can use TypeDI. |
16 |
| -The most simple usage example is: |
17 |
| - |
18 |
| -```javascript |
19 |
| -class SomeClass { |
20 |
| - someMethod() {} |
21 |
| -} |
22 |
| - |
23 |
| -var Container = require('typedi').Container; |
24 |
| -var someClass = Container.get(SomeClass); |
25 |
| -someClass.someMethod(); |
26 |
| -``` |
27 |
| - |
28 |
| -Then you can call `Container.get(SomeClass)` from anywhere in your application |
29 |
| -and you'll always have the same instance of `SomeClass`. |
30 |
| - |
31 |
| -In your class's constructor you always receive as a last argument a container which you can use to get other dependencies. |
32 |
| - |
33 |
| -```javascript |
34 |
| -class BeanFactory { |
35 |
| - create() {} |
36 |
| -} |
37 |
| - |
38 |
| -class SugarFactory { |
39 |
| - create() {} |
40 |
| -} |
41 |
| - |
42 |
| -class WaterFactory { |
43 |
| - create() {} |
44 |
| -} |
45 |
| - |
46 |
| -class CoffeeMaker { |
47 |
| - constructor(container) { |
48 |
| - this.beanFactory = container.get(BeanFactory); |
49 |
| - this.sugarFactory = container.get(SugarFactory); |
50 |
| - this.waterFactory = container.get(WaterFactory); |
51 |
| - } |
52 |
| - |
53 |
| - make() { |
54 |
| - this.beanFactory.create(); |
55 |
| - this.sugarFactory.create(); |
56 |
| - this.waterFactory.create(); |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 |
| -var Container = require('typedi').Container; |
61 |
| -var coffeeMaker = Container.get(CoffeeMaker); |
62 |
| -coffeeMaker.make(); |
63 |
| -``` |
64 |
| - |
65 |
| -With TypeDI you can use a named services. Example: |
66 |
| - |
67 |
| -```javascript |
68 |
| -var Container = require('typedi').Container; |
69 |
| - |
70 |
| -class BeanFactory implements Factory { |
71 |
| - create() {} |
72 |
| -} |
73 |
| - |
74 |
| -class SugarFactory implements Factory { |
75 |
| - create() {} |
76 |
| -} |
77 |
| - |
78 |
| -class WaterFactory implements Factory { |
79 |
| - create() {} |
80 |
| -} |
81 |
| - |
82 |
| -class CoffeeMaker { |
83 |
| - beanFactory: Factory; |
84 |
| - sugarFactory: Factory; |
85 |
| - waterFactory: Factory; |
86 |
| - |
87 |
| - constructor(container) { |
88 |
| - this.beanFactory = container.get('bean.factory'); |
89 |
| - this.sugarFactory = container.get('sugar.factory'); |
90 |
| - this.waterFactory = container.get('water.factory'); |
91 |
| - } |
92 |
| - |
93 |
| - make() { |
94 |
| - this.beanFactory.create(); |
95 |
| - this.sugarFactory.create(); |
96 |
| - this.waterFactory.create(); |
97 |
| - } |
98 |
| -} |
99 |
| - |
100 |
| -Container.set('bean.factory', new BeanFactory(Container)); |
101 |
| -Container.set('sugar.factory', new SugarFactory(Container)); |
102 |
| -Container.set('water.factory', new WaterFactory(Container)); |
103 |
| -Container.set('coffee.maker', new CoffeeMaker(Container)); |
104 |
| - |
105 |
| -var coffeeMaker = Container.get('coffee.maker'); |
106 |
| -coffeeMaker.make(); |
107 |
| -``` |
108 |
| - |
109 |
| -This feature especially useful if you want to store (and inject later on) some settings or configuration options. |
110 |
| -For example: |
111 |
| - |
112 |
| -```javascript |
113 |
| -var Container = require('typedi').Container; |
114 |
| - |
115 |
| -// somewhere in your global app parameters |
116 |
| -Container.set('authorization-token', 'RVT9rVjSVN'); |
117 |
| - |
118 |
| -class UserRepository { |
119 |
| - constructor(container) { |
120 |
| - this.authorizationToken = container.get('authorization-token'); |
121 |
| - } |
122 |
| -} |
123 |
| -``` |
124 |
| - |
125 |
| -When you write tests you can easily provide your own "fake" dependencies to classes you are testing using `set` method: |
126 |
| - |
127 |
| -```javascript |
128 |
| -Container.set(CoffeeMaker, new FakeCoffeeMaker()); |
129 |
| - |
130 |
| -// or for named services |
131 |
| - |
132 |
| -Container.set([ |
133 |
| - { id: 'bean.factory', value: new FakeBeanFactory() }, |
134 |
| - { id: 'sugar.factory', value: new FakeSugarFactory() }, |
135 |
| - { id: 'water.factory', value: new FakeWaterFactory() }, |
136 |
| -]); |
137 |
| -``` |
138 |
| - |
139 |
| -TypeDI also supports a function dependency injection. Here is how it looks like: |
140 |
| - |
141 |
| -```javascript |
142 |
| -var Service = require('typedi').Service; |
143 |
| -var Container = require('typedi').Container; |
144 |
| - |
145 |
| -var PostRepository = Service(() => ({ |
146 |
| - getName() { |
147 |
| - return 'hello from post repository'; |
148 |
| - }, |
149 |
| -})); |
150 |
| - |
151 |
| -var PostManager = Service(() => ({ |
152 |
| - getId() { |
153 |
| - return 'some post id'; |
154 |
| - }, |
155 |
| -})); |
156 |
| - |
157 |
| -class PostQueryBuilder { |
158 |
| - build() { |
159 |
| - return 'SUPER * QUERY'; |
160 |
| - } |
161 |
| -} |
162 |
| - |
163 |
| -var PostController = Service([PostManager, PostRepository, PostQueryBuilder], (manager, repository, queryBuilder) => { |
164 |
| - return { |
165 |
| - id: manager.getId(), |
166 |
| - name: repository.getName(), |
167 |
| - query: queryBuilder.build(), |
168 |
| - }; |
169 |
| -}); |
170 |
| - |
171 |
| -var postController = Container.get(PostController); |
172 |
| -console.log(postController); |
173 |
| -``` |
174 |
| - |
175 |
| -## Usage with TypeScript |
176 |
| - |
177 |
| -1. Install module: |
178 |
| - |
179 |
| - `npm install typedi --save` |
180 |
| - |
181 |
| -2. Install [reflect-metadata](https://www.npmjs.com/package/reflect-metadata) package: |
182 |
| - |
183 |
| - `npm install reflect-metadata --save` |
184 |
| - |
185 |
| - and import it somewhere in the global place of your app before any service declaration or import (for example in `app.ts`): |
186 |
| - |
187 |
| - `import "reflect-metadata";` |
188 |
| - |
189 |
| -3. You may need to install node typings: |
190 |
| - |
191 |
| - `npm install @types/node --save-dev` |
192 |
| - |
193 |
| -4) Enabled following settings in `tsconfig.json`: |
194 |
| - |
195 |
| -```json |
196 |
| -"emitDecoratorMetadata": true, |
197 |
| -"experimentalDecorators": true, |
198 |
| -``` |
199 |
| - |
200 |
| -Now you can use TypeDI. |
201 |
| -The most simple usage example is: |
202 |
| - |
203 |
| -```typescript |
204 |
| -import 'reflect-metadata'; |
205 |
| -import { Service, Container } from 'typedi'; |
206 |
| - |
207 |
| -@Service() |
208 |
| -class SomeClass { |
209 |
| - someMethod() {} |
210 |
| -} |
211 |
| - |
212 |
| -let someClass = Container.get(SomeClass); |
213 |
| -someClass.someMethod(); |
214 |
| -``` |
215 |
| - |
216 |
| -Then you can call `Container.get(SomeClass)` from anywhere in your application |
217 |
| -and you'll always have the same instance of `SomeClass`. |
| 6 | +## Typescript Usage |
218 | 7 |
|
219 | 8 | You can use **property injection** and inject services into your class using `@Inject` decorator:
|
220 | 9 |
|
@@ -717,23 +506,3 @@ console.log(postController);
|
717 | 506 | If you need to remove registered service from container simply use `Container.remove(...)` method.
|
718 | 507 | Also you can completely reset the container by calling `Container.reset()` method.
|
719 | 508 | This will effectively remove all registered services from the container.
|
720 |
| - |
721 |
| -## Troubleshooting |
722 |
| - |
723 |
| -### Use TypeDI with routing-controllers and/or TypeORM |
724 |
| - |
725 |
| -In order to use typedi with routing-controllers and/or typeorm, it's **necessary** to tell these libs to use the typedi container. |
726 |
| -Otherwise you may face [this kind of issue](https://github.com/pleerock/typedi/issues/4). |
727 |
| - |
728 |
| -```Typescript |
729 |
| -import {useContainer as routingUseContainer} from "routing-controllers"; |
730 |
| -import {useContainer as ormUseContainer} from "typeorm"; |
731 |
| -import {Container} from "typedi"; |
732 |
| - |
733 |
| -routingUseContainer(Container); |
734 |
| -ormUseContainer(Container); |
735 |
| -``` |
736 |
| - |
737 |
| -## Samples |
738 |
| - |
739 |
| -Take a look on samples in [./sample](https://github.com/pleerock/typedi/tree/master/sample) for examples of usage. |
0 commit comments