That's actually not a bad model.  The connect middleware system I designed
works like that.  Each middleware is a standalone module.  It may depend on
some other modules for functionality (the static file server module depends
on the mime database module).  But as far as runtime configuration and
app-level data, I pass it in at the app level.  Each connect plugin looks
like.

    // A static library that all instances of this middleware can share
    var getMime = require('mime').getMime;

    // Factory function to create an instance of this middleware.
    module.exports = function setup(config, args, go, here) {
        // This closure level contains any instance level state and logic.
 It's executed
        // once at server startup for each instance of this middleware.

        // Return the middleware object (in connect it's a request handler
function)
        return function handleRequest(req, res, next) {
            // This code is executed on every http request.
        };
    };

You don't have to use the factory pattern to create your objects, but what
I want to showcase is that you can separate your logic and state into
different levels.  There can be state shared by all instances of this
module, there is state per instance (this usually has the business logic in
it.).  More layers can be nested in deeper if it's natural to your app.  If
the nesting gets too deep, then start fresh in a new file or a new function
and remember to pass in all the needed state in the new layer.

On Fri, Mar 23, 2012 at 5:45 PM, Martin Lundberg
<[email protected]>wrote:

> Hi,
>
> I'm curious convention on how to design modules which has dependencies.
> For example if I've got an auth module with a function called authenticate
> which in turn uses a UserRepository object (which in turn uses a database
> library object like node-mysql) which communicates with the database, how
> should I design the auth module? I'm really interested in ways which makes
> the code easier to unit test.
>
> Is this a good way? (Pseudo code):
>
> Index.js:
>
> var db = new MysqlClient(...)
> var users = new UserRepository(db)
> var auth = require('./lib/auth')(users)
>
> auth.authenticate(...)
>
> Auth.js:
>
> module.exports = function(users) {
>   return {
>     authenticat: function(email, password, callback) {
>       user = users.get({ email: email}, function(user) {
>         if (user) callback(null, user)
>         else callback(new Error('Authentication failed'))
>       }
>     },
>     register: function(...) {}, etc...
>   }
> }
>
> Is there a better, cleaner way of doing it?
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to