Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eliasdarruda/ts-di-container
Simple DI Framework with the possibility of IoC using containers in Typescript.
https://github.com/eliasdarruda/ts-di-container
Last synced: about 2 months ago
JSON representation
Simple DI Framework with the possibility of IoC using containers in Typescript.
- Host: GitHub
- URL: https://github.com/eliasdarruda/ts-di-container
- Owner: eliasdarruda
- Created: 2020-04-19T08:14:52.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T13:11:16.000Z (almost 2 years ago)
- Last Synced: 2024-08-16T11:21:25.310Z (5 months ago)
- Language: TypeScript
- Size: 149 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Super simple DI framework and IoC with containers
npm install ts-di-container --save
## Usage example
```TYPESCRIPT
import { Container, injectable, dep } from 'simple-di-container';const container = new Container();
container.use(TestClass);
container.use(SomeOtherClass);const subject = container.get(TestClass);
subject.some.printf(); // 'SOME OTHER CLASS INSTANCE'// Classes
@injectable
class SomeOtherClass {
constructor() {}printf(): string {
return 'SOME OTHER CLASS INSTANCE';
}
}@injectable
class TestClass {
constructor(@dep() public some: SomeOtherClass) {}
}
```## Usage example with an alias interface for dependency inversion.
```TYPESCRIPT
import { Container, injectable, dep } from 'simple-di-container';const container = new Container();
container.use(TestClass);
container.use(Foo, 'FOO_BAR');
container.use(Bar, 'FOO_BAR');// "FOO_BAR" is an alias for whatever injection you will use
// Your dependencies will inherit last added "FOO_BAR" reference, in this case "Bar"const subject = container.get(TestClass);
subject.some.printf(); // 'BAR CLASS INTERFACE'// Classes
interface AliasInterface {
printf(): string;
}@injectable
class Foo implements AliasInterface {
constructor() {}printf(): string {
return 'FOO CLASS INSTANCE';
}
}@injectable
class Bar implements AliasInterface {
constructor() {}printf(): string {
return 'BAR CLASS INSTANCE';
}
}@injectable
class TestClass {
constructor(@dep('FOO_BAR') public some: AliasInterface) {}
}
```