You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can also use decorators to achieve features that aren't explicitly implemented in this library, such as service tagging, which we can do by defining a service as a key-value object (actually, a simple array might do for some cases but using key-values prevents accidental duplicates if modules are reused):
176
+
We can also use decorators to achieve features that aren't explicitly implemented in this library, such as service tagging, which we can do by defining a service as an array:
// `TaggedServiceConsumer` depends on an array, but using an object here prevents accidental duplicates.
197
-
[serviceTag]:Record<ContainerKey, TaggedService>
198
-
[taggedServiceConsumer]:TaggedServiceConsumer
185
+
typeServiceMap= {
186
+
[serviceTag]:TaggedServiceType[]
199
187
}
200
188
201
-
constparentModule:ContainerModule<
202
-
ParentModuleServiceMap
189
+
constmyModule:ContainerModule<
190
+
ServiceMap
203
191
> =builder=>builder
204
-
// Define an empty object of tagged services
205
192
.define(
206
193
serviceTag,
207
-
() =>({})
194
+
() =>[]
208
195
)
209
-
.define(
210
-
taggedServiceConsumer,
211
-
container=>newTaggedServiceConsumer(
212
-
// This utility method converts the tagged service object to an array
213
-
containerKeyValues(
214
-
container.get(serviceTag)
215
-
)
216
-
)
217
-
)
218
-
219
-
// Then define child modules that will provide tagged services
220
-
221
-
const taggedService =Symbol('taggedService')
222
196
223
-
typeChildModuleServiceMap= {
224
-
[taggedService]:TaggedService
225
-
}
226
-
227
-
const childModule:ContainerModule<
228
-
ParentModuleServiceMap&
229
-
ChildModuleServiceMap
197
+
const myOtherModule:ContainerModule<
198
+
ServiceMap
230
199
> =builder=>builder
231
-
.use(parentModule)
232
-
.define(
233
-
taggedService,
234
-
() =>newTaggedService()
235
-
)
200
+
.use(myModule)
236
201
.decorate(
237
202
serviceTag,
238
-
// Add a service to the already tagged services
239
-
factory=>container=>({
203
+
// Add a service to the array of already defined services
204
+
factory=>container=>[
240
205
...factory(container),
241
-
[taggedService]: container.get(taggedService)
242
-
})
206
+
{ foo: 'bar' }
207
+
]
243
208
)
244
209
```
245
210
246
-
---
247
-
248
211
And that's it - unlike some other DI containers that claim to be lightweight, tsinject really is tiny and has a simple API, allowing large and complex but loosely coupled applications to be built from small, simple and easily testable components.
249
212
250
213
See the [examples](https://github.com/mgdigital/tsinject/tree/main/examples) folder for a more complete application. It includes a simple tasks service with a REST API that can be started by cloning this repository and running `yarn install`, `yarn build` then `yarn example:start`.
0 commit comments