Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/symbux/injector

A simple and clean dependency injection (DI) library for usage with classes in TypeScript, the library utilises decorators.
https://github.com/symbux/injector

decorator-pattern decorators dependecy-injection dependency-injection injection ioc typescript typescript-library

Last synced: 2 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# Dependecy Injection

[![Codecov](https://img.shields.io/codecov/c/github/Symbux/Injector)](https://codecov.io/github/Symbux/Injector)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Symbux/Injector/Build)](https://github.com/Symbux/Injector/actions)
[![GitHub issues](https://img.shields.io/github/issues/Symbux/Injector)](https://github.com/Symbux/Injector/issues)
[![NPM](https://img.shields.io/npm/l/@symbux/injector)](https://www.npmjs.com/package/@symbux/injector)
[![npm (scoped)](https://img.shields.io/npm/v/@symbux/injector)](https://www.npmjs.com/package/@symbux/injector)
[![npm](https://img.shields.io/npm/dw/@symbux/injector)](https://www.npmjs.com/package/@symbux/injector)

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.

```typescript
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.

```typescript
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;
}
```