Category Archives: Pythonista

NodeJS: Inversion of Control

via Node.js: Inversion of Control — Medium.

I consider myself a polyglot developer. In my (6 year) career I’ve been paid to code C++, Python, PHP, ActionScript, Ruby, plus the whole menagerie of tools and languages that make up front-end web development. I’ve dabbled in many more unpaid. Picking up a new language is pretty straight forward, picking up the idioms of that language (and in-depth knowledge of the frameworks/standard library of that language) is much harder. I write Java like a PHP dev and JavaScript like a Pythonista.

In my most recent project we’ve taken the decision to break apart our monolithic app into microservices (partly to address some scaling concerns, and partly as a premature optimisation for some future features which would have been more challenging under our previous architecture), and we’ve decided to build those microservices in Node.js.

JavaScript is a famously unopinionated language, this makes life hard as a dev. As a Python dev I could follow PEP-8, as a Java dev I used Spring, which forces you into a certain style of design; JavaScript has many ways of doing things with no clear one that’s best. I’ve written before about how I structure my JavaScript using AMDs, but back-end code has different behaviours, different requirements, so I started writing my JS in an approach I felt was tried and tested, as classes using Inversion of Controlwhere the dependencies are passed into the constructor. My top-level JavaScript module behaved as the IoC container, instantiating objects and passing them around where they were needed. It made each module easy to test (I could just pass mocks into the constructor) although without a DI framework it did make that top-level module a bit messy.