DEV Community

FOLASAYO SAMUEL OLAYEMI
FOLASAYO SAMUEL OLAYEMI

Posted on • Originally published at innovatewithfolasayo.com on

InversifyJS: Managing Dependencies in Node.js Applications

Managing dependencies effectively is a key factor in maintaining clean, scalable, and maintainable code. In Node.js applications, as projects grow in complexity, so does the need for a structured approach to dependency management. This is where InversifyJS comes into play, offering a powerful solution to manage dependencies using Dependency Injection (DI).

In this article, we will explore how InversifyJS can help you manage dependencies in your Node.js applications, with a detailed walkthrough of the key concepts and a practical example to get you started.

What is InversifyJS?

InversifyJS is a lightweight and flexible Inversion of Control (IoC) container for TypeScript and JavaScript applications. It helps you manage your application’s dependencies by leveraging the principles of Dependency Injection. With InversifyJS, you can decouple your application’s components, making your code more modular, testable, and easier to maintain.

Why Use InversifyJS?

Before diving into how to use InversifyJS, let’s understand why it’s beneficial:

  • Decoupling: InversifyJS helps you decouple your components, allowing them to depend on abstractions rather than concrete implementations. This leads to a more flexible codebase where you can easily swap out implementations without affecting the rest of the application.
  • Testability: By managing dependencies through Dependency Injection, InversifyJS makes it easier to write unit tests for your components. You can easily mock dependencies and isolate the functionality you want to test.
  • Maintainability: InversifyJS enforces a structured approach to managing dependencies, making your codebase easier to navigate and maintain as it grows in complexity.

Getting Started with InversifyJS

Let’s get hands-on and learn how to set up InversifyJS in a Node.js application.

Step 1: Setting Up the Project

First, create a new Node.js project and navigate to the project directory:

mkdir inversifyjs-demo
cd inversifyjs-demo
npm init -y
Enter fullscreen mode Exit fullscreen mode

Next, install the necessary dependencies:

npm install inversify reflect-metadata
npm install typescript --save-dev
npm install @types/node --save-dev
npm install @types/inversify --save-dev
Enter fullscreen mode Exit fullscreen mode

Here’s a breakdown of the packages:

  • inversify: The core library that provides the Inversion of Control container.
  • reflect-metadata: A library that allows InversifyJS to retrieve metadata about your classes and functions at runtime, which is essential for DI.
  • typescript: TypeScript is a strongly typed superset of JavaScript that enhances your development experience.
  • @types/node: TypeScript type definitions for Node.js.
  • @types/inversify: TypeScript type definitions for InversifyJS.

Step 2: Configuring TypeScript

Create a tsconfig.json file in the root of your project to configure TypeScript:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "outDir": "./dist"
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}
Enter fullscreen mode Exit fullscreen mode

This configuration enables necessary TypeScript features like decorators and metadata reflection, which are essential for InversifyJS to function correctly.

Step 3: Creating the Application Structure

Now, create a basic structure for your application. Start by creating the following folders and files:

mkdir src
cd src
touch index.ts types.ts
Enter fullscreen mode Exit fullscreen mode

The src directory will contain all your TypeScript source files.

Step 4: Defining Types and Interfaces

In a typical Node.js application, you’ll have services that provide specific functionality. Let’s define some interfaces for these services in types.ts:

export const TYPES = {
  Warrior: Symbol.for("Warrior"),
  Weapon: Symbol.for("Weapon"),
  ThrowableWeapon: Symbol.for("ThrowableWeapon")
};

export interface Weapon {
  use(): string;
}

export interface ThrowableWeapon {
  throw(): string;
}

export interface Warrior {
  fight(): string;
  sneak(): string;
}
Enter fullscreen mode Exit fullscreen mode

Here, we define interfaces for Weapon, ThrowableWeapon, and Warrior. We also create a TYPES object that holds symbols representing our services, which will be used later for dependency injection.

Step 5: Implementing the Services

Next, let’s implement the services that conform to these interfaces. Create a new file src/services.ts and add the following code:

import { injectable } from "inversify";
import { Weapon, ThrowableWeapon } from "./types";

@injectable()
export class Katana implements Weapon {
  public use(): string {
    return "A sharp katana slice!";
  }
}

@injectable()
export class Shuriken implements ThrowableWeapon {
  public throw(): string {
    return "A deadly shuriken throw!";
  }
}
Enter fullscreen mode Exit fullscreen mode

In this code, we have two classes: Katana and Shuriken, which implement the Weapon and ThrowableWeapon interfaces, respectively. The @injectable() decorator marks these classes as injectable, meaning they can be managed by the InversifyJS container.

Step 6: Implementing the Warrior

Now, let’s create the Warrior implementation. Add the following code to a new file src/warrior.ts:

import { inject, injectable } from "inversify";
import { Warrior, Weapon, ThrowableWeapon } from "./types";
import { TYPES } from "./types";

@injectable()
export class Ninja implements Warrior {
  private _katana: Weapon;
  private _shuriken: ThrowableWeapon;

  public constructor(
    @inject(TYPES.Weapon) katana: Weapon,
    @inject(TYPES.ThrowableWeapon) shuriken: ThrowableWeapon
  ) {
    this._katana = katana;
    this._shuriken = shuriken;
  }

  public fight(): string {
    return this._katana.use();
  }

  public sneak(): string {
    return this._shuriken.throw();
  }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Ninja class implements the Warrior interface and uses the Katana and Shuriken services. The @inject() decorator is used to inject the dependencies, ensuring that the Ninja receives instances of Katana and Shuriken.

Step 7: Setting Up the InversifyJS Container

To manage dependencies, we need to set up an InversifyJS container. Create a new file src/inversify.config.ts:

import { Container } from "inversify";
import { TYPES } from "./types";
import { Warrior, Weapon, ThrowableWeapon } from "./types";
import { Ninja } from "./warrior";
import { Katana, Shuriken } from "./services";

const container = new Container();

container.bind<Warrior>(TYPES.Warrior).to(Ninja);
container.bind<Weapon>(TYPES.Weapon).to(Katana);
container.bind<ThrowableWeapon>(TYPES.ThrowableWeapon).to(Shuriken);

export { container };
Enter fullscreen mode Exit fullscreen mode

In this file, we create a new Container instance and bind our interfaces to their respective implementations. The container.bind<TYPE>(TYPE).to(Implementation) method tells InversifyJS how to resolve dependencies when they are requested.

Step 8: Putting It All Together

Finally, let’s use the container to resolve dependencies and run our application. Add the following code to src/index.ts:

import "reflect-metadata";
import { container } from "./inversify.config";
import { TYPES } from "./types";
import { Warrior } from "./types";

const ninja = container.get<Warrior>(TYPES.Warrior);

console.log(ninja.fight());
console.log(ninja.sneak());
Enter fullscreen mode Exit fullscreen mode

This is the entry point of our application. Here, we resolve the Warrior dependency from the container and call its methods to demonstrate that dependency injection works as expected.

Step 9: Compiling and Running the Application

To run your application, first, compile the TypeScript code to JavaScript:

npx tsc
Enter fullscreen mode Exit fullscreen mode

This will generate the dist directory with compiled JavaScript files. To execute the program, run:

node dist/index.js
Enter fullscreen mode Exit fullscreen mode

You should see the following output in the terminal:

A sharp katana slice!
A deadly shuriken throw!
Enter fullscreen mode Exit fullscreen mode

You can also download the source code here

Conclusion

InversifyJS provides a powerful way to manage dependencies in your Node.js applications using Dependency Injection. You can create modular, testable, and maintainable codebases by decoupling your components and managing their dependencies through an IoC container.

In this article, we walked through the steps of setting up InversifyJS in a Node.js application, from defining interfaces to binding them in a container and resolving them at runtime. With these skills, you’re now equipped to manage dependencies effectively in your Node.js projects, making your codebase more robust and easier to maintain.

Feel free to explore InversifyJS further and consider how it can enhance the quality of your Node.js applications.

Thanks for reading…

Happy coding!

The post InversifyJS: Managing Dependencies in Node.js Applications first appeared on Innovate With Folasayo.

The post InversifyJS: Managing Dependencies in Node.js Applications appeared first on Innovate With Folasayo.

Top comments (0)