CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-inversify

A powerful and lightweight inversion of control container for JavaScript and Node.js apps powered by TypeScript.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

container.mddocs/

Container Management

The Container class is the core of InversifyJS, providing dependency injection, service resolution, and binding management functionality. It maintains a registry of service bindings and handles the complete lifecycle of dependency resolution.

Container Class

class Container {
  constructor(containerOptions?: ContainerOptions);
  
  // Binding management
  bind<T>(serviceIdentifier: ServiceIdentifier<T>): BindInFluentSyntax<T>;
  rebind<T>(serviceIdentifier: ServiceIdentifier<T>): BindInFluentSyntax<T>;
  unbind(serviceIdentifier: ServiceIdentifier): void;
  unbindAll(): void;
  isBound(serviceIdentifier: ServiceIdentifier, options?: IsBoundOptions): boolean;
  
  // Service resolution
  get<T>(serviceIdentifier: ServiceIdentifier<T>, options?: GetOptions): T;
  getAll<T>(serviceIdentifier: ServiceIdentifier<T>, options?: GetAllOptions): T[];
  getNamed<T>(serviceIdentifier: ServiceIdentifier<T>, name: string): T;
  getAllNamed<T>(serviceIdentifier: ServiceIdentifier<T>, name: string): T[];
  getTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string, value: any): T;
  getAllTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string, value: any): T[];
  
  // Module management
  load(...modules: ContainerModule[]): void;
  unload(...modules: ContainerModule[]): void;
  
  // Container hierarchy
  createChild(containerOptions?: ContainerOptions): Container;
  
  // State management
  snapshot(): void;
  restore(): void;
}

Container Options

interface ContainerOptions {
  skipBaseClassChecks?: boolean;
  autoBindInjectable?: boolean;
  defaultScope?: BindingScope;
}

Basic Container Usage

import { Container } from "inversify";

// Create container with default options
const container = new Container();

// Create container with custom options
const customContainer = new Container({
  skipBaseClassChecks: true,
  autoBindInjectable: true,
  defaultScope: "Singleton"
});

Service Binding

Basic Binding

// Bind interface to implementation
container.bind<IWarrior>("Warrior").to(Ninja);

// Bind class to itself
container.bind(Katana).toSelf();

// Bind to constant value
container.bind<string>("DatabaseUrl").toConstantValue("mongodb://localhost");

// Bind to dynamic value
container.bind<Date>("CurrentTime").toDynamicValue(() => new Date());

Rebinding Services

// Replace existing binding
container.rebind<IWarrior>("Warrior").to(Samurai);

Checking Bindings

// Check if service is bound
if (container.isBound("Warrior")) {
  const warrior = container.get<IWarrior>("Warrior");
}

// Check with options
const isBound = container.isBound("Warrior", {
  searchParent: false
});

Removing Bindings

// Remove all bindings for identifier
container.unbind("Warrior");

// Remove specific binding
container.unbind("Warrior");

Service Resolution

Single Service Resolution

// Get single service instance
const warrior = container.get<IWarrior>("Warrior");

// Get with options
const warrior = container.get<IWarrior>("Warrior", {
  skipBaseClassChecks: true
});

// Async resolution
const warrior = await container.getAsync<IWarrior>("Warrior");

Multiple Service Resolution

// Get all services bound to identifier
const weapons = container.getAll<IWeapon>("Weapon");

// Async resolution of all services
const weapons = await container.getAllAsync<IWeapon>("Weapon");

Container Hierarchy

// Create child container
const childContainer = container.createChild();

// Child inherits parent bindings but can override
childContainer.bind<IWarrior>("Warrior").to(Samurai);

// Child-specific binding doesn't affect parent
const parentWarrior = container.get<IWarrior>("Warrior"); // Ninja
const childWarrior = childContainer.get<IWarrior>("Warrior"); // Samurai

Container Snapshots

// Save current container state
container.snapshot();

// Make changes
container.bind<IWeapon>("Weapon").to(Sword);
container.bind<IShield>("Shield").to(Shield);

// Restore to saved state
container.restore();

IsBound Options

interface IsBoundOptions {
  searchParent?: boolean;
}

Resolution Options

interface GetOptions {
  skipBaseClassChecks?: boolean;
}

interface OptionalGetOptions extends GetOptions {
  defaultValue?: any;
}

Container Events

The container provides hooks for monitoring binding and resolution events:

// Binding created
container.onActivation<IWarrior>("Warrior", (context, warrior) => {
  console.log("Warrior activated");
  return warrior;
});

// Binding destroyed  
container.onDeactivation<IWarrior>("Warrior", (warrior) => {
  console.log("Warrior deactivated");
});

Error Handling

Common container errors and their meanings:

  • No matching bindings found: Service identifier not bound
  • Ambiguous match: Multiple bindings found for identifier without disambiguation
  • Circular dependency: Dependency cycle detected during resolution
  • Invalid binding: Binding configuration is invalid or incomplete

docs

binding.md

conditional.md

container.md

decorators.md

index.md

lifecycle.md

modules.md

tile.json