https://github.com/doomsower/di-at-home
Simple dependency injection using ECMAscript decorators
https://github.com/doomsower/di-at-home
Last synced: 4 months ago
JSON representation
Simple dependency injection using ECMAscript decorators
- Host: GitHub
- URL: https://github.com/doomsower/di-at-home
- Owner: doomsower
- License: mit
- Created: 2024-06-03T20:50:18.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-12T15:58:01.000Z (about 2 years ago)
- Last Synced: 2025-01-17T19:08:48.460Z (over 1 year ago)
- Language: TypeScript
- Size: 66.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dependency Injection at Home
Simple dependency injection using typescript ECMA decorators
## Usage
### Simple Public Field Injection
```typescript
import { ContainerInstance } from "./container";
const C = new ContainerInstance();
@C.Injectable("door")
class Door {
public name = "door";
}
class House {
@C.Inject("door")
public door!: Door;
}
const house = new House();
console.log(house.door.name); // "door"
```
### Singleton Injection
```typescript
const C = new ContainerInstance();
@C.Injectable("door")
class Door {
private static index = 0;
constructor() {
Door.index += 1;
}
public get name(): string {
return `door ${Door.index}`;
}
}
class House {
@C.Inject("door")
public door!: Door;
}
const house = new House();
console.log(house.door.name); // "door 1"
```
### Transient Instances Injection
```typescript
const C = new ContainerInstance<{
door: [string, string];
}>();
@C.Factory("door")
class DoorFactory {
public produce(size: string, color: string): IDoor {
return { name: `${size} ${color} door` };
}
}
class House {
@C.Transient("door", "large", "red")
public red!: IDoor;
@C.Transient("door", "large", "blue")
public blue!: IDoor;
}
const house = new House();
console.log(house.red.name); // "large red door"
console.log(house.blue.name); // "large blue door"
```
Please refer to the test cases in `src/container.test.ts` for more examples.