Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Dependency injection for Node.js. What do you think?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
mixu  
View profile  
 More options Jul 28 2012, 1:25 am
From: mixu <mikito.tak...@gmail.com>
Date: Fri, 27 Jul 2012 22:25:53 -0700 (PDT)
Local: Sat, Jul 28 2012 1:25 am
Subject: Re: Dependency injection for Node.js. What do you think?

I've never really needed to dependency inject into third party modules. If
I did, I'd rather fork the third party dependency than do some sort of
dynamic solution.

However, I do use dependency injection for (unit) testing my own stuff.
There are a ton of ways to do this but my favorite way of doing this is:

var fs = require('fs');
function Foo() { };
Foo._setBackend = function(backend) {
  fs = backend;

};

module.exports = Foo;

Then in the test setup:

before: function() {
  Foo._setBackend(require('./smart-fs'));

},

after: function() {
  Foo._setBackend(require('fs'));

}

The nice part is that the mechanism is trivial and doesn't depend on any
external modules or require() patching, without forcing me to make "fs" a
part of the interface/params to Foo(). It doesn't work on 3rd party
modules, but I haven't needed to do that thus far. Simplest thing that
works and all that..
On Friday, July 27, 2012 7:45:48 AM UTC-7, Eldar wrote:

> Do we need this in Node?

> My answer is yes we need some (simple) way to specify the app level
> dependencies  at runtime. Here is my take on this<https://github.com/eldargab/node-runtime>.
> Please checkout and let me know how do you feel about.

> But the idea is very simple:

> // inside any index.jsvar R = require('runtime').patchNative()var use = R(module).use
> use('fs', 'node_modules/third/party', require('./smart-fs'))

> That's it. Third party module just uses our smart file system


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.