1.0.10 • Published 10 months ago

injexius v1.0.10

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

Injexius

A simple dependency injection container and inversion of control library for TypeScript.

Installation

> npm install injexius
> yarn add injexius
> pnpm add injexius

Usage

U can use symbols or strings as keys of the container.

Simple container
import { Container } from "injexius";

const container = new Container();

container.registerSingleton("logger", () => console);
container.register("database", () => new Database());
container.register(
  "userRepository",
  () => new UserRepository(container.resolve("database"))
);

const userRepository = container.resolve("userRepository");
Inversion of control
import { Container, Inject, Injectable } from "injexius";

const TYPES = {
  ServiceA: Symbol("ServiceA"),
  ServiceB: Symbol("ServiceB"),
};

@Injectable()
class ServiceA {
  sayHello() {
    console.log("Hello from ServiceA");
  }
}

@Injectable()
class ServiceB {
  @Inject(TYPES.ServiceA)
  private serviceA!: ServiceA;

  greet() {
    this.serviceA.sayHello();
  }
}

// Container configuration
const container = new Container();
container.register<ServiceA>(TYPES.ServiceA, () => new ServiceA());
container.register<ServiceB>(TYPES.ServiceB, () => {
  const serviceB = new ServiceB();
  container.injectDependencies(serviceB);
  return serviceB;
});

// Resolve service
const serviceB = container.resolve<ServiceB>(TYPES.ServiceB);
serviceB.greet();
// It must be return "Hello from ServiceA"

License

MIT

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago