InversifyJS

repository·master·Indexed 11 days ago

https://github.com/inversify/inversifyjs

A powerful and lightweight inversion of control (IoC) container for JavaScript and Node.js applications powered by TypeScript. Version 7.10.0 provides tools for dependency injection using decorators like @injectable and @inject, and manages service lifecycles via the Container and ContainerModule APIs.

Tokens
842
Snippets
4
Records
6
Agent score
46%

What's inside InversifyJS

  1. What is InversifyJS

    master
    InversifyJS is a lightweight inversion of control (IoC) container designed for TypeScript and JavaScript applications. It uses class constructors to identify and inject dependencies, helping developers implement robust Object-Oriented (OO) designs such as SOLID principles and Composition over Inheritance. It is optimized to provide minimal runtime overhead and a high-quality development experience.
  2. Install and import reflect-metadata

    master

    InversifyJS relies on reflect-metadata to enable decorator-based dependency injection. You must import reflect-metadata at the very top of your application's entry point before any other InversifyJS imports or decorated classes are defined.

    import 'reflect-metadata';
    import { Container, injectable } from 'inversify';
    // ... rest of your application
  3. Core InversifyJS API surface

    master

    The inversify package exports the primary tools for managing Dependency Injection (DI). The API is organized into three main functional areas:

    1. Container Management: Using Container to manage bindings and ContainerModule to group related bindings.
    2. Binding & Lifecycle: Using fluent syntax (e.g., Bind, To, In) to define how services are instantiated and their scope (BindingScope).
    3. Decorators: Using @injectable to mark classes for DI, and @inject, @tagged, @optional, and others to define how dependencies are resolved.
  4. Use @inject to specify dependencies

    master

    The @inject decorator is used on constructor parameters to tell InversifyJS which identifier (a class, a string, or a symbol) should be used to resolve that specific dependency.

    import { injectable, inject, Container } from 'inversify';
    
    const TYPES = { MyService: Symbol.for('MyService') };
    
    @injectable()
    class Consumer {
      constructor(@inject(TYPES.MyService) myService: MyService) {}
    }
  5. Use @injectable to mark classes for Dependency Injection

    master

    The @injectable decorator marks a class as being eligible for injection by the InversifyJS container. This allows the container to manage the class's lifecycle and resolve its dependencies.

    import { injectable, Container } from 'inversify';
    
    @injectable()
    class MyService {}
    
    const container = new Container();
    container.bind(MyService).toSelf();
  6. Use @tagged and @multiInject for tagged dependencies

    master

    When multiple implementations of the same service are bound to the container, you can use @tagged to distinguish them. Use @multiInject on the constructor parameter to request all implementations associated with a specific tag.

    import { injectable, tagged, multiInject, Container } from 'inversify';
    
    const TAG = Symbol.for('Tag');
    
    @injectable()
    @tagged(TAG)
    class TaggedService {}
    
    @injectable()
    class Consumer {
      constructor(@multiInject(TAG) services: TaggedService[]) {}
    }