简体   繁体   English

node.js模块依赖性覆盖或注入或IoC

[英]node.js module dependency overriding or injection or IoC

The problem relates to architectural design patterns . 该问题涉及建筑设计模式 The case is I'm building a node.js tool that reuses several npm-published modules beneath. 我正在构建一个node.js工具,它可以重用下面几个npm发布的模块。 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. 并且它们从npm开始按原样加载dependency1 And I want to provide a function that would extend the dependency, eg 我想提供一个可以扩展依赖性的函数,例如

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. 我发现了这个问题 ,有些人声称我不需要在node.js中进行依赖注入,而我真的不相信这个观点,因为我不知道如何在没有DI的情况下实现我的目标。 So far I think I need some kind of IoC. 到目前为止,我认为我需要某种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. 我在考虑一个最初会被调用的factory模块 - 它将加载所有原始依赖项,在依赖项上执行装饰/扩展函数,存储它们并让它们可供其他模块使用。 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. Afaik,node.js将加载的模块存储在内存中,因此上面的解决方案应该可行,但我不确定它是否正确。

Solution draft worked ( edit ) 解决方案草案工作( 编辑

I have implemented above solution and it works perfectly. 我已经实现了上面的解决方案,它完美无缺。 Node.js modules are re-used in memory. Node.js模块在内存中重用。


Please suggest a solution you would use in this case and comment on "Dependency Injection in Node.js" topic. 请建议您在此案例中使用的解决方案,并评论“Node.js中的依赖注入”主题。

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 ...? 我认为这个问题非常有用,但在另一篇文章中得到了很好的回答我是否需要NodeJS中的依赖注入,或者如何处理...?

in a nutsheel, the easier way, following the line of the solution you mention, is overriding the require function. 在坚实的基础上,按照你提到的解决方案,更简单的方法是覆盖require函数。 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 当然,你可以找到这个想法的许多变化,但这是概念

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. 可能是DI你想要的东西,我遇到了一个名为Coffee Sweetener的漂亮模块,这个例子是咖啡脚本,但几乎不重要,因为你可以将它们编译成普通的javascript。

It makes you define all modules using a .map method and then you can get instance of those methods from same Object. 它使您使用.map方法定义所有模块,然后您可以从同一个Object获取这些方法的实例。

Infact other modules can define their own dependencies too without making require calls in same file. 事实上,其他模块也可以定义自己的依赖关系,而无需在同一文件中进行require调用。 Let me know if you are also looking for an example for same. 如果您也在寻找相同的示例,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM