Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

inversify

Package Overview
Dependencies
Maintainers
1
Versions
88
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.

  • 4.9.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
591K
decreased by-41.37%
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 18 Dec 2017

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