Node.js dependent add-on or injection or IoC

The problem has to do with architectural design patterns. The thing is, I am creating a node.js tool that reuses several published modules published to it. I want to provide a mechanism for extending these dependencies between all modules in my tool.

Currently the problem is that all modules in my tool are talking to each other, so there are several files having:

var dep = require('dependency1');

      

and they download dependency1

as -from npm. And I want to provide a function that extends the dependency, eg.

function (dependency) {
    dependency.customFeature = ...;
    dependency.customizeSettings(...);
    return dependency;
}

      

and have this redundant dependency available across all modules inside my tool.

Study

I found this question where some people are claiming that I don't need dependency injection in node.js and I'm not very convinced of this opinion since I don't know how to achieve my goal without DI. For now I think I need some kind of IoC.

Draft decision

I was thinking of a module factory

that will be called initially - it will load all the raw dependencies, decorate / expand functions on the dependencies, save them, and let them be available to other modules. And all other modules will ask for a factory for extended modules instead of loading the original dependencies.

Afaik, node.js keeps loaded modules in memory, so the above solution should work, but I'm not sure if it's correct.

The draft decision has been processed (edited)

I have implemented the above solution and it works fine. Js modules are reusable in memory.


Please suggest a solution you would like to use in this case and comment on the topic "Dependency Injection in node.js theme".

+3


source to share


2 answers


I think this question is very helpful, but was very well received in another post Do I need dependency injection in NodeJS or how do I deal with ...?

in the nut, the easier it is, following the line of the solution you mentioned, overrides the required function. I think it's elegant and simple. This is an example of a form where the message:



var oldrequire = require
require = function(module) {
    if (module === 'fs') {
        return {
            readdirSync: function(dir) { 
                return ['somefile.txt', 'error.txt', 'anotherfile.txt']; 
            };
        };
    } else
        return oldrequire(module);

}

      

Of course you can find many variations of this idea, but this is a concept

+1


source


Maybe DI is what you want, I came across a nice module called Coffee Sweetener , examples are in the coffee script, but hardly matters since you can compile them to plain javascript.

It allows you to define all modules using a method .map

, and then you can get an instance of those methods from the same object.



Infact other modules can also define their own dependencies without requiring calls in the same file. Let me know if you are looking for an example for this as well.

-1


source







All Articles