InversifyJS
repository·master·Indexed 11 days ago
https://github.com/inversify/inversifyjsA 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.
What's inside InversifyJS
- 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.
Install and import reflect-metadata
masterInversifyJS relies on
reflect-metadatato enable decorator-based dependency injection. You must importreflect-metadataat 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 applicationCore InversifyJS API surface
masterThe
inversifypackage exports the primary tools for managing Dependency Injection (DI). The API is organized into three main functional areas:- Container Management: Using
Containerto manage bindings andContainerModuleto group related bindings. - Binding & Lifecycle: Using fluent syntax (e.g.,
Bind,To,In) to define how services are instantiated and their scope (BindingScope). - Decorators: Using
@injectableto mark classes for DI, and@inject,@tagged,@optional, and others to define how dependencies are resolved.
- Container Management: Using
Use @inject to specify dependencies
masterThe
@injectdecorator 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) {} }Use @injectable to mark classes for Dependency Injection
masterThe
@injectabledecorator 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();Use @tagged and @multiInject for tagged dependencies
masterWhen multiple implementations of the same service are bound to the container, you can use
@taggedto distinguish them. Use@multiInjecton 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[]) {} }