classic-di
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

classic-di

English | 简体中文


Auto Test CI Publish CI npm version

Classic dependency injection implementation with ECMA Decorators.

⚠ This library is experimental. There may be breaking changes to the APIs before the official release.

Requirements

  • TypeScript 5.2

Example

import { Container, Injectable, token } from "classic-di";

// 1. Declare services with types.
interface Dep1 {
  foo(): number;
}
interface Dep2 {
  bar(): string;
}
interface Dep3 {
  baz(): boolean;
}

// 2. Create tokens for services.
const dep1 = token<Dep1>("dep1");
const dep2 = token<Dep2>("dep2");
const dep3 = token<Dep3>("dep3");

// 3. Decorate with `Injectable` for classes.
@Injectable({
  // Declare what service to implement.
  implements: dep1,
})
class A implements Dep1 {
  foo() {
    return 666;
  }
}

@Injectable({
  implements: dep2,
  // Declare dependencies with token.
  requires: [dep1] as const,
})
class B implements Dep2 {
  // Consume dependencies in constructor. Dependencies are injected in declared order.
  constructor(public dep1: Dep1) {}
  bar() {
    return this.dep1.foo().toString();
  }
}

@Injectable({
  implements: dep3,
  requires: [dep2] as const,
})
class C implements Dep3 {
  constructor(public dep2: Dep2) {}
  baz(): boolean {
    return this.dep2.bar() === "666";
  }
}

// 4. Create IoC container and register implementations.

const container = new Container();
container.register(A);
container.register(B);
container.register(C);

// 5. Create service instances with `get` method.
const dep3Impl = container.get(dep3); // an instance of C

License

 __________________
< The MIT license! >
 ------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Readme

Keywords

none

Package Sidebar

Install

npm i classic-di

Weekly Downloads

0

Version

0.3.0

License

MIT

Unpacked Size

61.3 kB

Total Files

18

Last publish

Collaborators

  • darrendanielday