What is inversify?
Inversify is a powerful and flexible inversion of control (IoC) container for JavaScript and TypeScript applications. It helps in managing dependencies and promoting decoupling in your codebase by using dependency injection (DI).
What are inversify's main functionalities?
Dependency Injection
This code demonstrates how to use Inversify for dependency injection. It defines a `Katana` class and a `Ninja` class, where `Ninja` depends on `Katana`. The `Container` is used to bind these classes to their respective types, and the `Ninja` instance is resolved from the container.
const { Container, injectable, inject } = require('inversify');
const TYPES = { Warrior: Symbol.for('Warrior'), Weapon: Symbol.for('Weapon') };
@injectable()
class Katana {
hit() {
return 'cut!';
}
}
@injectable()
class Ninja {
constructor(@inject(TYPES.Weapon) weapon) {
this._weapon = weapon;
}
fight() {
return this._weapon.hit();
}
}
const container = new Container();
container.bind(TYPES.Weapon).to(Katana);
container.bind(TYPES.Warrior).to(Ninja);
const ninja = container.get(TYPES.Warrior);
console.log(ninja.fight()); // Output: cut!
Auto-Wiring
This code demonstrates the auto-wiring feature of Inversify. By setting `autoBindInjectable` to true, you can automatically bind classes marked with `@injectable()` without explicitly binding them in the container.
const { Container, injectable, inject } = require('inversify');
const TYPES = { Warrior: Symbol.for('Warrior'), Weapon: Symbol.for('Weapon') };
@injectable()
class Shuriken {
throw() {
return 'thrown!';
}
}
@injectable()
class Samurai {
constructor(@inject(TYPES.Weapon) weapon) {
this._weapon = weapon;
}
attack() {
return this._weapon.throw();
}
}
const container = new Container({ autoBindInjectable: true });
container.bind(TYPES.Weapon).to(Shuriken);
const samurai = container.get(Samurai);
console.log(samurai.attack()); // Output: thrown!
Middleware
This code demonstrates how to use middleware in Inversify. Middleware can be applied to the container to intercept and log actions during the resolution of dependencies.
const { Container, injectable, inject } = require('inversify');
const TYPES = { Warrior: Symbol.for('Warrior'), Weapon: Symbol.for('Weapon') };
@injectable()
class Bow {
shoot() {
return 'arrow shot!';
}
}
@injectable()
class Archer {
constructor(@inject(TYPES.Weapon) weapon) {
this._weapon = weapon;
}
attack() {
return this._weapon.shoot();
}
}
const container = new Container();
container.applyMiddleware((planAndResolve) => {
return (args) => {
console.log('Middleware: Resolving dependencies...');
return planAndResolve(args);
};
});
container.bind(TYPES.Weapon).to(Bow);
container.bind(TYPES.Warrior).to(Archer);
const archer = container.get(TYPES.Warrior);
console.log(archer.attack()); // Output: Middleware: Resolving dependencies... arrow shot!
Other packages similar to inversify
awilix
Awilix is a lightweight dependency injection container for JavaScript and TypeScript. It offers a similar feature set to Inversify, including support for class-based and function-based dependency injection. Awilix is known for its simplicity and ease of use, making it a good alternative for smaller projects or those who prefer a less verbose API.
typedi
TypeDI is a dependency injection tool for TypeScript and JavaScript. It provides decorators and a service container to manage dependencies. TypeDI is similar to Inversify in its use of decorators and TypeScript support, but it is generally considered to be more lightweight and easier to set up.
tsyringe
TSyringe is a lightweight dependency injection container for TypeScript. It uses decorators and reflection to manage dependencies, similar to Inversify. TSyringe is known for its minimalistic design and ease of integration with existing TypeScript projects.
[!NOTE]
New docs are temporary available here. We would love to have your feedback in this discussion.
![](https://raw.githubusercontent.com/inversify/inversify.github.io/master/img/cover.jpg)
InversifyJS
A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
About
InversifyJS is a lightweight inversion of control (IoC) container for TypeScript and JavaScript apps.
An IoC container uses a class constructor to identify and inject its dependencies.
InversifyJS has a friendly API and encourages the usage of the best OOP and IoC practices.
Motivation
JavaScript now supports object oriented (OO) programming with class based inheritance. These features are great but the truth is that they are also
dangerous.
We need a good OO design (SOLID, Composite Reuse, etc.) to protect ourselves from these threats. The problem is that OO design is difficult and that is exactly why we created InversifyJS.
InversifyJS is a tool that helps JavaScript developers write code with good OO design.
Philosophy
InversifyJS has been developed with 4 main goals:
-
Allow JavaScript developers to write code that adheres to the SOLID principles.
-
Facilitate and encourage the adherence to the best OOP and IoC practices.
-
Add as little runtime overhead as possible.
-
Provide a state of the art development experience.
Testimonies
Nate Kohari - Author of Ninject
"Nice work! I've taken a couple shots at creating DI frameworks for JavaScript and TypeScript, but the lack of RTTI really hinders things.
The ES7 metadata gets us part of the way there (as you've discovered). Keep up the great work!"
Michel Weststrate - Author of MobX
Dependency injection like InversifyJS works nicely
Some companies using InversifyJS
![](https://avatars0.githubusercontent.com/u/6154722?s=200&v=4)
![](https://avatars2.githubusercontent.com/u/69631?s=200&v=4)
![](https://avatars0.githubusercontent.com/u/2232217?s=200&v=4)
![](https://avatars0.githubusercontent.com/u/1520648?s=200&v=4)
![](https://avatars3.githubusercontent.com/u/6962987?s=200&v=4)
![](https://avatars1.githubusercontent.com/u/864482?s=200&v=4)
📕 Documentation
Documentation is available at https://inversify.io
Acknowledgements
Thanks a lot to all the contributors, all the developers out there using InversifyJS and all those that help us to spread the word by sharing content about InversifyJS online. Without your feedback and support this project would not be possible.
License
License under the MIT License (MIT)
Copyright © 2015-2017 Remo H. Jansen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.