What is @inversifyjs/core?
@inversifyjs/core is a powerful and flexible inversion of control (IoC) container for JavaScript and TypeScript applications. It helps in managing dependencies and promoting a clean architecture by using dependency injection.
What are @inversifyjs/core's main functionalities?
Dependency Injection
This feature allows you to define and inject dependencies into your classes. The code sample demonstrates how to use @inversifyjs/core to inject a Warrior instance into a Samurai class, promoting loose coupling and easier testing.
const { Container, injectable, inject } = require('inversify');
@injectable()
class Warrior {
fight() {
return 'Fight!';
}
}
@injectable()
class Samurai {
constructor(@inject(Warrior) warrior) {
this.warrior = warrior;
}
battle() {
return this.warrior.fight();
}
}
const container = new Container();
container.bind(Warrior).toSelf();
container.bind(Samurai).toSelf();
const samurai = container.get(Samurai);
console.log(samurai.battle()); // Output: Fight!
Binding and Scoping
This feature allows you to define the lifecycle of your dependencies. The code sample shows how to bind a Ninja class in a singleton scope, ensuring that only one instance of Ninja is created and shared across the application.
const { Container, injectable } = require('inversify');
@injectable()
class Ninja {
fight() {
return 'Ninja fight!';
}
}
const container = new Container();
container.bind(Ninja).toSelf().inSingletonScope();
const ninja1 = container.get(Ninja);
const ninja2 = container.get(Ninja);
console.log(ninja1 === ninja2); // Output: true
Middleware
Middleware in @inversifyjs/core allows you to intercept and modify the behavior of dependency resolution. The code sample demonstrates how to apply a middleware that logs messages before and after the execution of a dependency resolution.
const { Container, injectable, interfaces } = require('inversify');
@injectable()
class LoggerMiddleware {
handler(next) {
return (args) => {
console.log('Before execution');
const result = next(args);
console.log('After execution');
return result;
};
}
}
const container = new Container();
container.applyMiddleware((planAndResolve) => {
return (args) => {
console.log('Middleware applied');
return planAndResolve(args);
};
});
Other packages similar to @inversifyjs/core
tsyringe
Tsyringe is a lightweight dependency injection container for TypeScript and JavaScript. It offers similar functionality to @inversifyjs/core but is designed to be more lightweight and easier to use, with a focus on TypeScript decorators.
awilix
Awilix is a powerful dependency injection container for JavaScript and Node.js. It provides a similar feature set to @inversifyjs/core, including support for dependency injection and lifecycle management, but with a focus on simplicity and ease of use.
bottlejs
BottleJS is a simple and lightweight dependency injection micro-container. It offers basic dependency injection capabilities similar to @inversifyjs/core but is more minimalistic and easier to integrate into small projects.