A powerful and lightweight inversion of control container for JavaScript and Node.js apps powered by TypeScript.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
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.
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;
}interface ContainerOptions {
skipBaseClassChecks?: boolean;
autoBindInjectable?: boolean;
defaultScope?: BindingScope;
}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"
});// 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());// Replace existing binding
container.rebind<IWarrior>("Warrior").to(Samurai);// 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
});// Remove all bindings for identifier
container.unbind("Warrior");
// Remove specific binding
container.unbind("Warrior");// 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");// Get all services bound to identifier
const weapons = container.getAll<IWeapon>("Weapon");
// Async resolution of all services
const weapons = await container.getAllAsync<IWeapon>("Weapon");// 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// 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();interface IsBoundOptions {
searchParent?: boolean;
}interface GetOptions {
skipBaseClassChecks?: boolean;
}
interface OptionalGetOptions extends GetOptions {
defaultValue?: any;
}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");
});Common container errors and their meanings: