Lightweight and simple dependency injection container for TypeScript.
xInjecTS is designed to provide a lightweight and easy-to-use dependency injection container for TypeScript applications. It aims to simplify the management of dependencies and promote better code organization and testability.
npm i @rafaeljcamara/xinjects
xInjecTs supports two ways of doing dependency injection:
- Using our decorators:
@Injectable
and@Inject
. - Directly interacting with our dependency injection container
xContainer
.
import { Injectable, Inject, xContainer } from '@rafaeljcamara/xinjects';
@Injectable()
class ServiceA {
sayHello() {
return 'Hello from ServiceA';
}
}
@Injectable()
class ServiceB {
constructor(@Inject(ServiceA) private serviceA: ServiceA) {}
greet() {
return this.serviceA.sayHello();
}
}
const serviceB = xContainer.resolve(ServiceB);
console.log(serviceB.greet()); // Output: Hello from ServiceA
import { xContainer } from '@rafaeljcamara/xinjects';
class ServiceA {
sayHello() {
return 'Hello from ServiceA';
}
}
class ServiceB {
constructor(private serviceA: ServiceA) {}
greet() {
return this.serviceA.sayHello();
}
}
xContainer.register(ServiceA);
const serviceA = xContainer.resolve(ServiceA);
const serviceB = new ServiceB(serviceA);
console.log(serviceB.greet()); // Output: Hello from ServiceA
For more samples, please refer to our samples.
To understand in-depth what you can with our library, please also consider looking at our docs.
This project welcomes and appreciates any contributions made.
There are several ways you can contribute, namely:
- Report any bug found.
- Suggest some features or improvements.
- Creating pull requests.
xInjecTs is a free and open-source software licensed under the MIT License.
See LICENSE for more details.
- No support
Symbols
as dependency keys. - Factories can only be applied to
Singleton
dependencies.