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.