Tuesday, 5 April 2016

To DI or not to DI?

Coming from a C# background, where a web app without DI would basically be shooting yourself in the foot.Looking at node though, it seems the general consensus is to just require things and be done with it - basically tight coupling (not in a type-based way, but API-based).Example: let's say you're using Mongoose, and defining some models and using them in your express routes.. 68 handlers later you realize you actually want to use a different DB. You can do 2 things:Re-implement every single API call on your models to call your new DBRewrite all your handlers (yaaaay, fun...)I see this pattern too often and I keep asking myself is this really how it's done, or do node devs just not bother?I really like the higher-order functions approach, where you pass in an object that contains all the things a function might need, and returns that function preconfigured with it's dependencies (db, email), but that would require that we either pass down all dependencies through the call tree, and call the makeSomeFunction(db, someOtherFunc, etc), which seems pretty cumbersome to me.TL;DR: For serious node-based apps, how are you guys handling this without thinking too much about it, while maintaining testability?

Submitted April 05, 2016 at 05:31PM by jeffijoe

No comments:

Post a Comment