Skip to content
This repository was archived by the owner on Apr 21, 2024. It is now read-only.

Commit e4eab17

Browse files
committed
docs: explain dependency groups
1 parent 3442bed commit e4eab17

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,26 @@ const container = createContainer()
8282
.register('fn', func(printNameAndAge, 'someName', 'someAge'))
8383
// And constructors too
8484
.register('Person', constructor(Person, 'someAge', 'someName'))
85+
// We can "define groups" by using `:` as an infix, the group's name will be
86+
// the first part of the string before `:`.
87+
// Groups can be used in all "register" methods.
88+
.registerValue('group1:a', 1) // group == 'group1'
89+
.registerValue('group1:b', 2)
90+
.registerValue('group2:a', 3) // group == 'group2'
91+
.registerValue('group2:b', 4)
8592
93+
// We can resolve registered functions
8694
const print = container.resolve('fn')
8795
print() // Prints "Timmy is aged 5"
8896

97+
// We can resolve registered constructors
8998
const person = container.resolve('Person')
9099
console.print(person.age) // Prints "5"
91100
console.print(person.name) // Prints "Timmy"
101+
102+
// We can resolve registered "groups"
103+
container.resolveGroup('group1') // ~ [1, 2], not necessarily in the same order
104+
container.resolveGroup('group2') // ~ [3, 4], not necessarily in the same order
92105
```
93106

94107
It is also possible to register and resolve asynchronous factories and
@@ -103,6 +116,9 @@ If you are curious, just try out:
103116

104117
- First-class support for Deno.
105118
- First-class support for asynchronous dependency resolution.
119+
- Stricter types for dependencies re-registration.
120+
- Groups registration and resolution: very useful when we need to resolve all
121+
dependencies belonging to a same category.
106122
- The container interface has been split into `ReaderContainer` and
107123
`WriterContainer`, making it easier to use precise types.
108124
- More extense documentation.

lambda-ioc/README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,67 @@ import { ... } from 'https://deno.land/x/lambda_ioc@[VERSION]/lambda-ioc/deno/in
5858
## Example
5959

6060
```ts
61-
import { createContainer } from '@coderspirit/lambda-ioc'
61+
import {
62+
constructor,
63+
createContainer,
64+
func
65+
} from '@coderspirit/lambda-ioc'
6266

6367
function printNameAndAge(name: string, age: number) {
6468
console.log(`${name} is aged ${age}`)
6569
}
70+
71+
class Person {
72+
constructor(
73+
public readonly age: number,
74+
public readonly name: string
75+
) {}
76+
}
6677
6778
const container = createContainer()
68-
.register('someAge', value(5))
69-
.register('someName', value('Timmy'))
79+
.registerValue('someAge', 5)
80+
.registerValue('someName', 'Timmy')
81+
// We can register functions
7082
.register('fn', func(printNameAndAge, 'someName', 'someAge'))
83+
// And constructors too
84+
.register('Person', constructor(Person, 'someAge', 'someName'))
85+
// We can "define groups" by using `:` as an infix, the group's name will be
86+
// the first part of the string before `:`.
87+
// Groups can be used in all "register" methods.
88+
.registerValue('group1:a', 1) // group == 'group1'
89+
.registerValue('group1:b', 2)
90+
.registerValue('group2:a', 3) // group == 'group2'
91+
.registerValue('group2:b', 4)
7192
93+
// We can resolve registered functions
7294
const print = container.resolve('fn')
7395
print() // Prints "Timmy is aged 5"
96+
97+
// We can resolve registered constructors
98+
const person = container.resolve('Person')
99+
console.print(person.age) // Prints "5"
100+
console.print(person.name) // Prints "Timmy"
101+
102+
// We can resolve registered "groups"
103+
container.resolveGroup('group1') // ~ [1, 2], not necessarily in the same order
104+
container.resolveGroup('group2') // ~ [3, 4], not necessarily in the same order
74105
```
106+
107+
It is also possible to register and resolve asynchronous factories and
108+
dependencies. They are not documented yet because some "helpers" are missing,
109+
and therefore it's a bit more annoying to take advantage of that feature.
110+
111+
If you are curious, just try out:
112+
- `registerAsync`
113+
- `resolveAsync`
114+
115+
## Differences respect to Diddly
116+
117+
- First-class support for Deno.
118+
- First-class support for asynchronous dependency resolution.
119+
- Stricter types for dependencies re-registration.
120+
- Groups registration and resolution: very useful when we need to resolve all
121+
dependencies belonging to a same category.
122+
- The container interface has been split into `ReaderContainer` and
123+
`WriterContainer`, making it easier to use precise types.
124+
- More extense documentation.

0 commit comments

Comments
 (0)