This library implements dependency injection for javascript and typescript.
- Does not use decorators.
- Designed to minimize functionality and be simple to use
- Cached - Creates and caches once by default.
- Cache can be turned off directly
- Built with unit testing in mind
- Support for dependency rebinding and container snapshot and restore
- Lightweight - Only 1.5kb without compression.
- Reflection metadata, which is approximately 50kb in size, is not needed.
- 100% written in Typescript.
npm install @trinity/container
A container is where all dependencies are bound. A project can use multiple containers.
import { Container } from '@trinity/container'
const container = new Container();
Keys used for binding can be classes, functions, symbols, or strings.
const ServiceKey = () => new Service()
const ServiceToken = token('ServiceToken')
container.bind<ServiceInterface>(Service, () => new Service())
container.bind<ServiceInterface>(ServiceKey, () => new Service())
container.bind<ServiceInterface>(Symbol.for('Service'), () => new Service())
container.bind<ServiceInterface>('Service', () => new Service())
container.bind<ServiceInterface>(ServiceToken, () => new Service())
Bindings are based on factory functions that return a value. All dependencies are made within factory functions.
container.bind<ServiceInterface>(Service, () => new Service())
container.bind<ServiceInterface>(Service, () => 'just a string')
This is the way how we can rebind a dependency while unit tests. We should not need to rebind in production code.
container.rebind<ServiceMock>(symbol, () => new ServiceMock())
Normally this function is not used in production code. This will remove the dependency from the container.
container.remove(symbol)
If you need to create it every time rather than as a singleton, you can specify meta data.
container.bind<ServiceInterface>(Service, () => new Service())
container.meta(Service, { transient: true })
const ServiceKey = () => new Service()
container.get(Service)
container.get<ServiceInterface>(ServiceKey)
container.get<ServiceInterface>(Symbol.for('Service'))
container.get<ServiceInterface>('Service')
Dependencies are created explicitly.
// service.ts
class Service {
}
container.bind(Service, () => new Service())
// module.ts
class Module {
constructor(private service: Service) {}
}
container.bind(Module, () => new Module(
container.get(Service)
))
// main.ts
const module = container.get(Module)
This creates a snapshot of the bound dependencies. After this we can rebind dependencies and can restore it back to its old state after we made some unit tests.
container.snapshot();
container.restore();
npm install @trinitytime/container
File app-container.ts
const container = new Container()
File services/my-service.ts
import { container } from '../app-container.ts'
export interface MyServiceInterface {
hello: string;
}
export class MyService implements MyServiceInterface {
hello = "world";
}
container.bind(MyService, () => new MyService())
File services/my-other-service.ts
export interface MyOtherServiceInterface {
random: number;
}
export class MyOtherService implements MyOtherServiceInterface {
random = Math.random();
constructor(private service: MyServiceInterface) {}
}
container.bind(MyOtherService, () => new MyOtherService(
container.get<MyServiceInterface>(MyService)
))
const app = container.get<MyOtherServiceInterface>(MyOtherService)
MIT License under the MIT License (MIT)
Copyright © 2024 Trinitytime