3

The problem relates to architectural design patterns. The case is I'm building a node.js tool that reuses several npm-published modules beneath. I want to provide a mechanism for extending those dependencies among all modules in my tool.

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

var dep = require('dependency1');

and they load dependency1 as-is from npm. And I want to provide a function that would extend the dependency, e.g.

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

and to have this overriden dependency available among all modules inside my tool.

Research

I have found this question, where some people claim that I don't need dependency injection in node.js and I'm not really convinced to this opinion, since I don't know how to achieve my goal without DI. So far I think I need some kind of IoC.

Solution draft

I was thinking of a factory module which would be called initially - it would load all raw dependencies, execute decorating/extending functions on the dependencies, store them and let them be available to other modules. And all other modules would ask the factory for the extended modules instead of loading raw dependencies.

Afaik, node.js stores loaded modules in the memory, so above solution should work, but I'm not sure whether it's the right way.

Solution draft worked (edit)

I have implemented above solution and it works perfectly. Node.js modules are re-used in memory.


Please suggest a solution you would use in this case and comment on "Dependency Injection in Node.js" topic.

Community
  • 1
  • 1
ducin
  • 25,621
  • 41
  • 157
  • 256
  • I have created a IoC container for JavaScript apps. Please check it out at http://blog.wolksoftware.com/introducing-inversifyjs – Remo H. Jansen May 11 '15 at 15:19

2 Answers2

1

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

in a nutsheel, the easier way, following the line of the solution you mention, is overriding the require function. I think is elegant and simple. This is an example form that post:

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 the concept

Community
  • 1
  • 1
pabloelustondo
  • 2,196
  • 3
  • 19
  • 23
-1

May be DI is something you want, i came across a nice module called Coffee Sweetener , the examples are in coffee-script but hardly matters , as you can compile them to plain javascript.

It makes you define all modules using a .map method and then you can get instance of those methods from same Object.

Infact other modules can define their own dependencies too without making require calls in same file. Let me know if you are also looking for an example for same.

Aman Virk
  • 3,909
  • 8
  • 40
  • 51
  • I think you didn't understand my question. I'm asking about a programming approach in node.js. And you're suggesting a library you're using. Two unrelated topics, sorry. – ducin May 15 '15 at 08:14