Skip to content

RafaelJCamara/xInjecTS

Repository files navigation

xInjecTS

Lightweight and simple dependency injection container for TypeScript.

About

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.

Install Package

npm i @rafaeljcamara/xinjects

Usage

xInjecTs supports two ways of doing dependency injection:

  1. Using our decorators: @Injectable and @Inject.
  2. Directly interacting with our dependency injection container xContainer.

Example 1: Using Decorators

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

Example 2: Using xContainer Directly

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.

Contributing

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.

License

xInjecTs is a free and open-source software licensed under the MIT License.

See LICENSE for more details.

⚠️ Known limitations

  • No support Symbols as dependency keys.
  • Factories can only be applied to Singleton dependencies.