@tecp/di 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -78,15 +78,50 @@ import { di, regSingleton, clearRegistry } from '@tecp/di';
|
|
|
78
78
|
clearRegistry(); // reset all registrations
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
##
|
|
81
|
+
## DiContainer
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
The top-level functions (`di`, `regSingleton`, etc.) operate on a global singleton instance of `DiContainer`. For isolated scopes (e.g. testing, multi-tenant), create your own instance:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { DiContainer } from '@tecp/di/container';
|
|
87
|
+
|
|
88
|
+
const container = new DiContainer();
|
|
89
|
+
|
|
90
|
+
container.regSingleton(Logger);
|
|
91
|
+
const logger = container.get(Logger);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { DiContainer } from '@tecp/di/container';
|
|
96
|
+
|
|
97
|
+
const container = new DiContainer();
|
|
98
|
+
|
|
99
|
+
container.regSingleton({ token: CONFIG, useValue: { db: 'sqlite' } });
|
|
100
|
+
container.regTransient(MyController);
|
|
101
|
+
|
|
102
|
+
const cfg = container.get(CONFIG);
|
|
103
|
+
const ctrl = container.get(MyController);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Instance methods
|
|
107
|
+
|
|
108
|
+
| Method | Description |
|
|
84
109
|
|---|---|
|
|
110
|
+
| `reg(provider)` | Register a `FullProvider`; returns the token |
|
|
85
111
|
| `regSingleton(provider)` | Register a singleton provider |
|
|
86
112
|
| `regTransient(provider)` | Register a transient provider |
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
89
|
-
|
|
90
|
-
|
|
113
|
+
| `get(token)` | Resolve a token to its instance |
|
|
114
|
+
| `clear()` | Reset all registrations and cached singletons |
|
|
115
|
+
|
|
116
|
+
## API
|
|
117
|
+
|
|
118
|
+
| Function | Description |
|
|
119
|
+
|---|---|
|
|
120
|
+
| `regSingleton(provider)` | Register a singleton provider on the global container |
|
|
121
|
+
| `regTransient(provider)` | Register a transient provider on the global container |
|
|
122
|
+
| `di(token)` | Resolve a token to its instance from the global container |
|
|
123
|
+
| `singleton()` | Decorator — registers class as singleton on the global container |
|
|
124
|
+
| `transient()` | Decorator — registers class as transient on the global container |
|
|
125
|
+
| `clearRegistry()` | Reset all registrations on the global container |
|
|
91
126
|
|
|
92
127
|
A provider can be a class constructor or a `FullProvider` object with `token`, `useClass`, `useValue`, `useFactory`, and `isSingleton` fields.
|