https://github.com/rootsher/di-resolver
Typed Dependency Injection Container
https://github.com/rootsher/di-resolver
container dependency di injection typed typescript weak-map
Last synced: about 2 months ago
JSON representation
Typed Dependency Injection Container
- Host: GitHub
- URL: https://github.com/rootsher/di-resolver
- Owner: rootsher
- Created: 2020-06-14T17:53:51.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-28T11:58:04.000Z (almost 5 years ago)
- Last Synced: 2025-04-16T03:11:44.114Z (about 2 months ago)
- Topics: container, dependency, di, injection, typed, typescript, weak-map
- Language: TypeScript
- Homepage:
- Size: 1.95 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @rootsher/di-resolver
## installation
```sh
$ npm install @rootsher/di-resolver
```## example usage
* define class A:
```ts
// [A]
class A {
private value: number = 1;method(): number {
return this.value;
}
}
```* define class B:
```ts
// [B, { deps: [A] }]
class B {
private a: A;constructor(a: A) {
this.a = a;
}callAMethod(): number {
return this.a.method();
}
}
```* define class C:
```ts
// [C, { deps: [B, A] }]
class C {
public value: number = 2;constructor(private b: B, a: A) {}
method({ value }: { value: number }) {
this.value = value;
}
}
```* create dependency injection container definitions:
```ts
import { Resolver } from 'typed-di-container';const resolver = new Resolver([
[A],
[
B,
{ deps: [A] }
],
[
C,
{
deps: [B, A],
calls: [
(c: C) => c.method({ value: 15 })
]
}
]
]);
```* simple access to instances (`resolver.get([class])`):
```ts
console.log(resolver.get(B).callAMethod()); // 1
console.log(resolver.get(C).value); // 15
```## IDE support
```ts
// IDE will help with types ("value" property, "method" method etc.)
"resolver.get(C).[value/method]"
```