As you mentioned authentication as an example use case may I suggest you
look at http://passportjs.org
The way it integrates works really well in practice.

On Fri, Mar 23, 2012 at 10:29 PM, Eldar <[email protected]> wrote:

> For now I would refactor your module in this way:
>
> // authentication
> var repo = require('user-repo')
> module.exports = function authenticate (email, password, cb) {
> ...
> }
>
> // user repository
> var db = require('db')
> module.exports = {
>   get: function () {
>   },
>   ...
> }
>
> // database
> var MysqlClient = require('mysql')
> var connectionString = proccess.env.connection_string
> module.exports = new MysqlClient(connectionString)
>
> The idea behind is to never construct dependencies in the modules code. So
> if we require, for example, database client we make special "glue module"
> in the appropriate node_modules directory. Such module can perform actual
> construction (as it's the case in examples above) or be as simple as:
>
> var instance
> module.exports = {
>   get: function () { return instance },
>   set: function (i) { instance = i }
> }
>
> // and somewhere in the config
> require('modules-path').set('instance')
>
>
> On Saturday, March 24, 2012 2:45:50 AM UTC+4, Martin Lundberg 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?
>>
>
> On Saturday, March 24, 2012 2:45:50 AM UTC+4, Martin Lundberg 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?
>>
>
> On Saturday, March 24, 2012 2:45:50 AM UTC+4, Martin Lundberg 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?
>>
>
> On Saturday, March 24, 2012 2:45:50 AM UTC+4, Martin Lundberg 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?
>>
>
> On Saturday, March 24, 2012 2:45:50 AM UTC+4, Martin Lundberg 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