Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do effective dependency injection in Nodejs?

Let's start with some reference code

var express = require('express');  
var app = express();  
var session = require('express-session');

app.use(session({  
  store: require('connect-session-knex')()
}));

I have a couple questions here that I would like to answer in case you know the answer:

Everytime a require is call in Nodejs, is that named dependency injection? Or what is the real meaning of dependency injection?

The reason why I am asking this, is because I've been reading about Node, and I see people people talking about the module or module.export pattern, and I am confuse, the module is the same as dependency?

So, all I need is a clear explanation about dependency injection, and when/where you need to inject a dependency . . .

like image 874
Reacting Avatar asked Dec 02 '22 13:12

Reacting


1 Answers

Dependency injection is somewhat the opposite of normal module design. In normal module design, a module uses require() to load in all the other modules that it needs with the goal of making it simple for the caller to use your module. The caller can just require() in your module and your module will load all the other things it needs.

With dependency injection, rather than the module loading the things it needs, the caller is required to pass in things (usually objects) that the module needs. This can make certain types of testing easier and it can make mocking certain things for testing purposes easier.

Everytime a require is call in Nodejs, is that named dependency injection? Or what is the real meaning of dependency injection?

No. When a module does a require() to load it's own dependencies that is not dependency injection.

The reason why I am asking this, is because I've been reading about Node, and I see people people talking about the module or module.export pattern, and I am confuse, the module is the same as dependency?

A module is not the same as a dependency. Normal module design allows you to require() just the module and get back a series of exports that you can use. The module itself handles the loading of its own dependencies (usually using require() internal to the module).

Here are a couple of articles that discuss some of the pros/cons of using dependency injection. As best I can tell the main advantage is to simplify unit testing by allowing dependent objects (like databases) to be more easily mocked.

When to use Dependency Injection

When is it not appropriate to use dependency injection

Why should we use dependency injection


A classic case for using a dependency injection is when a module depends upon a database interface. If the module loads it's own database, then that module is essentially hard-wired to that particular database. There is no architecture built into the module for the caller to specify what type of storage should be used.

If, however, the module is set up so that when a caller loads and initializes the module, it must pass in an object that implements a specific database API, then the caller is free to decide what type of database should be used. Any database that meets the contract of the API can be used. But, the burden is on the caller to pick and load a specific database. There can also be hybrid circumstances where a module has a built-in database that will be used by default, but the caller can supply their own object that will be used instead if it is provided in the module constructor or module initialization.

like image 93
jfriend00 Avatar answered Dec 04 '22 02:12

jfriend00