Tag: dependency injection

在Nodejs中dependency injection的模型

将依赖关系注入模型的最佳做法是什么? 特别是,如果他们的getter是asynchronous的,就像mongodb.getCollection() ? 重点是一次注入依赖关系 var model = require('./model')({dep1: foo, dep2: bar}); 并调用所有成员方法而不必将它们作为parameter passing。 我也不希望每个方法都以asynchronous获取者的瀑布开始。 我结束了一个专用的exports包装,代理所有的调用,并传递asynchronous依赖。 然而,这会造成很多开销,这是重复的,我通常不喜欢它。 var Entity = require('./entity'); function findById(id, callback, collection) { // … // callback(null, Entity(…)); }; module.exports = function(di) { function getCollection(callback) { di.database.collection('users', callback); }; return { findById: function(id, callback) { getCollection(function(err, collection) { findById(id, callback, collection); }); }, […]

具有许多导入的节点中的dependency injection

我试图找出哪里dependency injection有它在节点的地方。 即使我知道Java是如何工作的,而且我一直在阅读无数的博客,我似乎无法理解它。 networking上的例子是微不足道的 。 他们并没有真正显示为什么需要DI。 我更喜欢一个复杂的例子。 我看了下面的框架: https://github.com/young-steveo/bottlejs http://inversify.io/ 现在,Node使用模块模式。 当我做一个导入它接受一个单身人士,因为这是什么节点,它caching模块,除非工厂模式用于返回一个新的实例(返回新的MyThing())。 现在dependency injection的主要function是解耦所有的东西。 当人们说,我得到的目标是…要从模块的顶部删除所有import的概念。 我今天写的是: 'use strict'; // node modules import os from 'os'; …8 more modules here import fs from 'fs'; // npm modules import express from 'express'; …8 more modules here import _ from 'lodash'; // local modules import moduleOne from './moduleOne'; […]

注入npm依赖的吞咽

我使用gulp compilatorangular度。 我使用了bower依赖,我把它们全部移到了npm。 我有注入依赖脚本的问题。 这是我目前的注入代码。 如何更改wiredep到另一个库来处理npm注入,因为我有Uncaught ReferenceError:angular没有定义,var angular = require(angular)不起作用。 是否有任何简单的解决scheme注入npm的依赖脚本没有要求等? 如果不是这样,要求如何? var injectStyles = gulp.src([ paths.tmp + '/serve/{app,components,lib}/**/*.css', paths.src + '/{app,components,lib}/**/*.css', '!' + paths.tmp + '/serve/app/vendor.css' ], {read: false}); var injectScripts = gulp.src([ paths.src + '/{app,components,lib}/**/*.js', paths.tmp + '/serve/build/*.js', '!' + paths.src + '/{app,components}/**/*.spec.js', '!' + paths.src + '/{app,components}/**/*.mock.js' ]).pipe($.angularFilesort()); var injectOptions = { […]

有没有类似于angular度注射器的nod​​ejs的任何dependency injection解决scheme?

angular度注射器非常棒,在后端使用相同的概念会很棒。 有类似的东西吗?

如何提供诸如数据库,logging器等依赖关系到底层库?

build立 我正在为分布式应用程序构build基础库。 结构如下: core_project/ lib/ index.js module1.js module2.js … package.json 我的应用程序的许多其他部分将使用这样的库: consumer_project/ node_modules/ core_project/ … app.js package.json 问题 我想要一些常见的依赖关系,比如消费者应用程序提供的DB处理程序。 一旦初始化,我希望所有的核心应用程序模块能够使用这些依赖关系。 尝试解决scheme 我试图在每个核心应用程序模块中创build一个模块初始化函数: core_project / lib / module1.js var cfg = {db: null}; module.exports = { // … 'initModule': function (db) { cfg.db = db; } } 这是我的核心模块索引文件: core_project / lib / index.js var modules = […]

密码保护REDIS与dependency injectionnodejs

嘿,这是怎么回事,我有我的Redis的设置dependency injection问题。 我们有一个用于开发的室外中央Redis服务器,所以在我的主文件( app.js )中,我试图将其连接到服务器。 现在,这是密码保护,无法findisue。 我正在使用这个模块redis app.js let redis = require('redis'); let redisSettings = { host: settings.redis.ip, port: settings.redis.port, parser: settings.redis.parser, password: settings.redis.password }; let redisClient = redis.createClient(redisSettings); redisClient.auth(settings.redis.password, function(){ }); let routes = require('./routes')( redisClient); settings.js module.exports = { redis: { ip: 'domainname.example', port: '6379', parser: 'javascript', password: 'our_very_secure_password' } } 错误页面 events.js:182扔呃; […]

使用dependency injection的Node.js应用程序示例

我真的很喜欢dependency injection是如何完成的,所以我想在节点中使用类似的模式。 有很多模块往往是DI容器,不容易select。 在节点中使用DI有真实世界的例子吗? 在这种情况下如何构build应用程序?

在node.js中使用全局variables进行dependency injection

我正在开始一个基于node.js的长期项目,所以我正在build立一个坚实的DI系统。 尽pipenode.js的核心意味着使用简单模块require()来连接组件,但是我发现这种方法并不适合于大型项目(例如,要求每个文件中的模块不是可维护,可testing或dynamic的)。 现在,在发布这个问题之前,我已经完成了一些研究工作,并且发现了一些有趣的用于node.js的DI库(参见wire.js , dependable.js ) 但是,为了最大限度地简化和最小的重复,我提出了自己的实现DI的build议: 你有一个模块,di.js,作为容器,并通过指向一个json文件来存储一个依赖名称和他们各自的.js文件的地图初始化。 这已经为DI提供了dynamic性,因为您可以轻松地交换testing/ dev依赖关系。 容器可以通过使用一个inject()函数来返回依赖关系,该函数find依赖关系映射并用它调用require()。 为了简单起见,模块被分配到一个全局variables,即global。$ di,这样项目中的任何文件都可以通过调用$ di.inject()来使用容器/注入器。 这是实现的要点: di.js: module.exports = function(path) { this.deps = require(path); return { inject: function(name) { if (!deps[name]) throw new Error('dependency "' + name + '" isn\'t registered'); return require(deps[name]); } }; }; 依赖关系图json文件: { "vehicle": "lib/jetpack", "fuel": "lib/benzine", "octane": "lib/octane98" } 根据开发/testing模式,初始化主js文件中的$ […]

这是在Node中做dependency injection的正确方法吗?

我最近启动了一个节点项目,作为一名testing驱动开发人员,我用我的全新模块很快遇到了dependency injection问题。 以下是我如何计算出我应该做dependency injection。 注意我使用誓言作为BDD框架并且用Sinon扩展它是重要的。 我的模块: exports.myMethod = function () { var crypto = exports.cryptoLib || require('ezcrypto').Crypto; crypto.HMAC( crypto.SHA256, 'I want to encrypt this', 'with this very tasty salt' ); }; 我的testing: var vows = require('vows'), sinon = require('sinon'); vows.describe('myObject').addBatch({ 'myMethod':{ 'topic':true, 'calls ezcrypto.HMAC':function () { var myObject = require('../playground.js'); var mock = sinon.mock(require('ezcrypto').Crypto); myObject.cryptoLib […]

如何在Nodejs中进行有效的dependency injection?

我们从一些参考代码开始 var express = require('express'); var app = express(); var session = require('express-session'); app.use(session({ store: require('connect-session-knex')() })); 我在这里有几个问题,如果你知道答案,我想回答: 每次在Nodejs中调用一个require ,是不是被称为dependency injection? 或者dependency injection的真正含义是什么? 我之所以问这个问题,是因为我一直在阅读关于Node的内容,而且我看到有人在谈论module或者module.export模式,我很迷惑, module和dependency相同吗? 所以,我所需要的只是关于dependency injection的一个清晰的解释,以及何时何地需要注入依赖关系。 。 。