Socket
Socket
Sign inDemoInstall

inversify

Package Overview
Dependencies
Maintainers
1
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inversify

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


Version published
Weekly downloads
795K
decreased by-13.97%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 26 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc