jeudi 19 février 2015

Proper way to handle library dependencies

I'm having trouble deciding/determining what the standard/best approach for passing dependencies over to my Express routes would be. The problem I'm encountering is that some of my libraries have bindings associated with them ie. my session manager is hooked up to my redis queue client. My logs have configuration settings mapping them accordingly etc.


So here are my routes:



// Include routes and endpoints
app.use(require('./routes/account')(queue, models, log));
app.use(require('./routes/message')(queue, models, log));
app.use(require('./routes/admin') (queue, models, log));


As you can see I'm passing in my dependencies as parameters. I haven't started writing my unit tests yet, but I suspect this will bring more painful when I get there.


The other thing I've thought of doing is attaching the libraries to my request object as such:



req.log = log;
req.nconf = nconf;
req.sessions = sessions;


What I'm unsure of is: a) what the best/standard practice is, b) do any of these methods impact performance/memory usage, c) how will this impact my future unit testing.


Any insights on this would be great!


Thanks.


Aucun commentaire:

Enregistrer un commentaire