Skip to content

A simple and clean dependency injection (DI) library for usage with classes in TypeScript, the library utilises decorators.

License

Notifications You must be signed in to change notification settings

Symbux/Injector

Repository files navigation

Dependecy Injection

Codecov GitHub Workflow Status GitHub issues NPM npm (scoped) npm

The injector package is a dependecy injection tool built on top of TypeScript decorators for use with Node.JS/TypeScript applications. The original design is for a framework that is soon to come out, this is a prerequisite library.

Getting Started

Standard usage.

import { Inject, Provide } from '@symbux/injector';

@Provide() // You can optionally give it a name.
export class NumberHelper {
	public multiply(num1: number, num2: number): number {
		return num1 * num2;
	}
}

export class BusinessLogic {

	@Inject() helper!: NumberHelper;

	public main(): void {
		console.log(this.helper.multiply(5, 5));
	}
}

Custom usage.

import { Injector, Inject } from '@symbux/injector';

// You can register variables specifically.
Injector.register('my_special_var', 12345);

// You can also resolve them manually.
const mySpecialVar = Injector.resolve('my_special_var');

// You can also inject with a name.
export class BusinessLogic {

	@Inject('my_special_var')
	public specialVariable!: number;
}