Skip to content

Commit 24e9ea4

Browse files
committed
docs: add strings as dependency key examples
1 parent b9a453a commit 24e9ea4

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

README.md

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@
1010

1111
## Introduction
1212
An IOC (Inversion of Control) container for Typescript.
13+
1314
The idea behind is to create a simple container that can be used to register and resolve dependencies working with classes & functions but without reflect metadata.
15+
1416
It is using simple Typescript code, so it can be used in any project without any dependency.
15-
Works in NextJS middleware and edge runtime.
17+
18+
Works also in NextJS middleware and node+edge runtimes.
1619

1720
## Installation
1821
```npm i @evyweb/ioctopus```
1922

2023
## How to use
2124

22-
### List your injection tokens
23-
Create a symbol for each dependency you want to register. It will be used to identify the dependency.
25+
To use the container, you need to create a container and bind your dependencies.
26+
To do so you need to create an id for each dependency you want to register.
27+
28+
This id that we call an "injection token" can be a string or a symbol.
29+
(Please note that you have to be consistent and use always strings for binding and resolving dependencies or always symbols, you can't mix them).
30+
31+
Then you can bind the dependency to a value, a function, a class, a factory, a higher order function, or a curried function.
32+
33+
### Using symbols as injection tokens
34+
(You can skip "step a" if you prefer to use strings as injection tokens).
35+
36+
a) Create a symbol for each dependency you want to register. It will be used to identify the dependency.
2437

2538
```typescript
2639
export const DI: InjectionTokens = {
@@ -36,7 +49,8 @@ export const DI: InjectionTokens = {
3649
HIGHER_ORDER_FUNCTION_WITHOUT_DEPENDENCIES: Symbol('HIGHER_ORDER_FUNCTION_WITHOUT_DEPENDENCIES')
3750
} ;
3851
```
39-
Then create your container.
52+
53+
b) Then create your container.
4054

4155
```typescript
4256
import { DI } from './di';
@@ -51,6 +65,10 @@ You can register primitives
5165
```typescript
5266
container.bind(DI.DEP1).toValue('dependency1');
5367
container.bind(DI.DEP2).toValue(42);
68+
69+
// or using strings
70+
container.bind('DEP1').toValue('dependency1');
71+
container.bind('DEP2').toValue(42);
5472
```
5573

5674
#### Functions
@@ -59,6 +77,9 @@ container.bind(DI.DEP2).toValue(42);
5977
const sayHelloWorld = () => console.log('Hello World');
6078

6179
container.bind(DI.SIMPLE_FUNCTION).toFunction(sayHelloWorld);
80+
81+
// or using strings
82+
container.bind('SIMPLE_FUNCTION').toFunction(sayHelloWorld);
6283
```
6384

6485
#### Currying
@@ -69,6 +90,10 @@ const myFunction = (dep1: string, dep2: number) => (name: string) => console.log
6990

7091
container.bind(DI.CURRIED_FUNCTION_WITH_DEPENDENCIES)
7192
.toCurry(myFunction, [DI.DEP1, DI.DEP2]);
93+
94+
// or using strings
95+
container.bind('CURRIED_FUNCTION_WITH_DEPENDENCIES')
96+
.toCurry(myFunction, ['DEP1', 'DEP2']);
7297
```
7398

7499
- You can also use a dependency object
@@ -84,6 +109,10 @@ const myFunction = (dependencies: Dependencies) => (name: string) => console.log
84109
// The dependencies will be listed in an object in the second parameter
85110
container.bind(DI.CURRIED_FUNCTION_WITH_DEPENDENCIES)
86111
.toCurry(myFunction, {dep1: DI.DEP1, dep2: DI.DEP2});
112+
113+
// or using strings
114+
container.bind('CURRIED_FUNCTION_WITH_DEPENDENCIES')
115+
.toCurry(myFunction, {dep1: 'DEP1', dep2: 'DEP2'});
87116
```
88117

89118
#### Higher order functions
@@ -100,6 +129,10 @@ const MyServiceWithDependencies = (dep1: string, dep2: number): MyServiceWithDep
100129
// The dependencies will be listed in an array in the second parameter
101130
container.bind(DI.HIGHER_ORDER_FUNCTION_WITH_DEPENDENCIES)
102131
.toHigherOrderFunction(MyServiceWithDependencies, [DI.DEP1, DI.DEP2]);
132+
133+
// or using strings
134+
container.bind('HIGHER_ORDER_FUNCTION_WITH_DEPENDENCIES')
135+
.toHigherOrderFunction(MyServiceWithDependencies, ['DEP1', 'DEP2']);
103136
```
104137

105138
But if you prefer, you can also use a dependency object
@@ -121,6 +154,10 @@ const MyService = (dependencies: Dependencies): MyServiceInterface => {
121154
// The dependencies will be listed in an object in the second parameter
122155
container.bind(DI.HIGHER_ORDER_FUNCTION_WITH_DEPENDENCIES)
123156
.toHigherOrderFunction(MyService, {dep1: DI.DEP1, dep2: DI.DEP2});
157+
158+
// or using strings
159+
container.bind('HIGHER_ORDER_FUNCTION_WITH_DEPENDENCIES')
160+
.toHigherOrderFunction(MyService, {dep1: 'DEP1', dep2: 'DEP2'});
124161
```
125162

126163
#### Factories
@@ -135,6 +172,16 @@ container.bind(DI.MY_USE_CASE).toFactory(() => {
135172
myService: container.get<MyService>(DI.MY_SERVICE)
136173
});
137174
});
175+
176+
// or using strings
177+
container.bind('MY_USE_CASE').toFactory(() => {
178+
// Do something before creating the instance
179+
180+
// Then return the instance
181+
return MyUseCase({
182+
myService: container.get<MyService>('MY_SERVICE')
183+
});
184+
});
138185
```
139186

140187
#### Classes
@@ -153,6 +200,9 @@ class MyServiceClass implements MyServiceClassInterface {
153200
}
154201

155202
container.bind(DI.CLASS_WITH_DEPENDENCIES).toClass(MyServiceClass, [DI.DEP1, DI.DEP2]);
203+
204+
// or using strings
205+
container.bind('CLASS_WITH_DEPENDENCIES').toClass(MyServiceClass, ['DEP1', 'DEP2']);
156206
```
157207

158208
But if you prefer, you can also use a dependency object:
@@ -172,6 +222,9 @@ class MyServiceClass implements MyServiceClassInterface {
172222
}
173223

174224
container.bind(DI.CLASS_WITH_DEPENDENCIES).toClass(MyServiceClass, {dep1: DI.DEP1, dep2: DI.DEP2});
225+
226+
// or using strings
227+
container.bind('CLASS_WITH_DEPENDENCIES').toClass(MyServiceClass, {dep1: 'DEP1', dep2: 'DEP2'});
175228
```
176229

177230
- You can register classes without dependencies:
@@ -183,6 +236,9 @@ class MyServiceClassWithoutDependencies implements MyServiceClassInterface {
183236
}
184237

185238
container.bind(DI.CLASS_WITHOUT_DEPENDENCIES).toClass(MyServiceClassWithoutDependencies);
239+
240+
// or using strings
241+
container.bind('CLASS_WITHOUT_DEPENDENCIES').toClass(MyServiceClassWithoutDependencies);
186242
```
187243

188244
### Resolve the dependencies
@@ -195,17 +251,26 @@ import { DI } from './di';
195251
// Primitive
196252
const dep1 = container.get<string>(DI.DEP1); // 'dependency1'
197253
const dep2 = container.get<number>(DI.DEP2); // 42
254+
// or using strings
255+
const dep1 = container.get<string>('DEP1'); // 'dependency1'
256+
const dep2 = container.get<number>('DEP2'); // 42
198257

199258
// Higher order function and class
200259
const myUseCase = container.get<MyUseCaseInterface>(DI.MY_USE_CASE);
260+
// or using strings
261+
const myUseCase = container.get<MyUseCaseInterface>('MY_USE_CASE');
201262
myUseCase.execute();
202263

203264
// Simple function
204265
const simpleFunction = container.get<SimpleFunctionType>(DI.SIMPLE_FUNCTION);
266+
// or using strings
267+
const simpleFunction = container.get<SimpleFunctionType>('SIMPLE_FUNCTION');
205268
simpleFunction('Hello World');
206269

207270
// Curried function
208271
const callMe = container.get<CurriedFunction>(DI.CURRIED_FUNCTION_WITH_DEPENDENCIES);
272+
// or using strings
273+
const callMe = container.get<CurriedFunction>('CURRIED_FUNCTION_WITH_DEPENDENCIES');
209274
callMe('John Doe');
210275
```
211276

@@ -236,7 +301,7 @@ container.load(Symbol('module3'), module3);
236301
const myService = container.get<MyServiceInterface>(DI.MY_SERVICE);
237302
```
238303
The dependencies do not need to be registered in the same module as the one that is using them.
239-
Note that the module name used as a key is a symbol.
304+
Note that the module name used as a key can be a symbol or a string.
240305

241306
#### Modules override
242307

0 commit comments

Comments
 (0)