Skip to content

Commit 320a2d3

Browse files
authored
Update README (#33)
1 parent ebfc3af commit 320a2d3

File tree

2 files changed

+17
-55
lines changed

2 files changed

+17
-55
lines changed

README.md

+15-52
Original file line numberDiff line numberDiff line change
@@ -173,78 +173,41 @@ const myCustomLoggingModule: ContainerModule<
173173
)
174174
```
175175

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 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:
177177

178178
```typescript
179-
import type { ContainerKey, ContainerModule } from '@mgdigital/tsinject'
180-
import { containerKeyValues } from '@mgdigital/tsinject'
181-
182-
// Assume a type of service that we wish to tag, along with a sevice that depends on the tagged services.
179+
import type { ContainerModule } from '@mgdigital/tsinject'
183180

184-
class TaggedService {}
185-
186-
class TaggedServiceConsumer {
187-
constructor (
188-
public readonly taggedServices: TaggedService[]
189-
) {}
190-
}
181+
type TaggedServiceType = { foo: string }
191182

192183
const serviceTag = Symbol('serviceTag')
193-
const taggedServiceConsumer = Symbol('taggedServiceConsumer')
194184

195-
type ParentModuleServiceMap = {
196-
// `TaggedServiceConsumer` depends on an array, but using an object here prevents accidental duplicates.
197-
[serviceTag]: Record<ContainerKey, TaggedService>
198-
[taggedServiceConsumer]: TaggedServiceConsumer
185+
type ServiceMap = {
186+
[serviceTag]: TaggedServiceType[]
199187
}
200188

201-
const parentModule: ContainerModule<
202-
ParentModuleServiceMap
189+
const myModule: ContainerModule<
190+
ServiceMap
203191
> = builder => builder
204-
// Define an empty object of tagged services
205192
.define(
206193
serviceTag,
207-
() => ({})
194+
() => []
208195
)
209-
.define(
210-
taggedServiceConsumer,
211-
container => new TaggedServiceConsumer(
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')
222196

223-
type ChildModuleServiceMap = {
224-
[taggedService]: TaggedService
225-
}
226-
227-
const childModule: ContainerModule<
228-
ParentModuleServiceMap &
229-
ChildModuleServiceMap
197+
const myOtherModule: ContainerModule<
198+
ServiceMap
230199
> = builder => builder
231-
.use(parentModule)
232-
.define(
233-
taggedService,
234-
() => new TaggedService()
235-
)
200+
.use(myModule)
236201
.decorate(
237202
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 => [
240205
...factory(container),
241-
[taggedService]: container.get(taggedService)
242-
})
206+
{ foo: 'bar' }
207+
]
243208
)
244209
```
245210

246-
---
247-
248211
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.
249212

250213
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

Comments
 (0)