This is yet another simple dependency injection tool that will help you resolve dependencies to focus on bisness logic instead of how and when provide dependencies.
Main features:
- property based injection
- support for multiple DI containers
- constructor based injection
- Add constructor based injections
- Add auto resolve constructor dependencies (no need to use @Inject on Injectables dependencies)
- Fix issue with resolving non-injectable dependencies
This project was built to understand how decorators and Reflect working in TypeScript and how Dependecy Injection can be impletented.
To start using package you need to run following commands via NPM:
npm install @mificot/inject-it reflect-metadata
Import reflect-metadata
package in the your entry source file
import 'reflect-metadata'
// your source code
Also you need to enable emitting decorator metadata in tsconfig.json
file under compilerOptions
key:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
That it. Now you ready to go 😊
import { Container, Inject, Injectable } from '@mificot/inject-it'
const container = new Container()
@Injectable()
class InjectableClass {
public action(): void {
console.log('hello there')
}
}
@Injectable()
class BaseClass {
@Inject()
public readonly injectableClass: InjectableClass
}
container.resolve(BaseClass).injectableClass.action() // logs: "hello there"
- Constructor based injection
- Global container
- More docs