|
| 1 | +# @coderspirit/lambda-ioc |
| 2 | + |
| 3 | +> Pure functional (λ) dependency injection 💉 for TypeScript (inspired by Diddly) |
| 4 | +
|
| 5 | +**NOTE:** This is a "fork" of Tom Sherman's |
| 6 | +**[Diddly library](https://github.com/tom-sherman/diddly)**, who deserves most |
| 7 | +credit for this work. |
| 8 | + |
| 9 | +## Benefits |
| 10 | + |
| 11 | +- 100% type safe: |
| 12 | + - The type checker will complain if we try to resolve unregistered |
| 13 | + dependencies. |
| 14 | + - The type checker will complain if we try to register new dependencies that |
| 15 | + depend on unregistered dependencies, or if there is any kind of type |
| 16 | + mismatch. |
| 17 | +- Purely functional |
| 18 | +- Immutable |
| 19 | +- Circular dependencies are impossible |
| 20 | + |
| 21 | +## Drawbacks |
| 22 | + |
| 23 | +- All dependencies must be declared "in order". |
| 24 | + - This implies that this IoC container cannot be used in combination with some |
| 25 | + auto-wiring solutions, such as IoC decorators. |
| 26 | +- The involved types are a bit convoluted: |
| 27 | + - They might cause the type checker to be slow. |
| 28 | + - In some situations, the type checker might be unable to infer the involved |
| 29 | + types due to excessive "nested types" depth. |
| 30 | + |
| 31 | +## Example |
| 32 | + |
| 33 | +```ts |
| 34 | +import { createContainer } from '@coderspirit/lambda-ioc'; |
| 35 | + |
| 36 | +function printNameAndAge(name: string, age: number) { |
| 37 | + console.log(`${name} is aged ${age}`); |
| 38 | +} |
| 39 | + |
| 40 | +const container = createContainer() |
| 41 | + .register('someAge', value(5)) |
| 42 | + .register('someName', value('Timmy')) |
| 43 | + .register('fn', func(printNameAndAge, 'someName', 'someAge')); |
| 44 | + |
| 45 | +const print = container.resolve('fn'); |
| 46 | +print(); // Prints "Timmy is aged 5" |
| 47 | +``` |
0 commit comments