Free AI web copilot to create summaries, insights and extended knowledge, download it at here
1959
Abstract
it’s coming from! <i>We don’t care!</i></li></ul><p id="af84">Moving on, let’s get ourselves an implementation of that sweet sweet todos repository!</p><h2 id="da4c">todosRepository.js</h2>
<figure id="fa2a">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jeffijoe/8e8a4fcb7d2fd252ce0d84e91414095b.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="7d29">This is just an in-memory implementation of the todos repository used by the service. Whenever we are ready, we can swap it out for a MySQL, Rethink, MongoDB, <i>WhateverDB you want</i>, as long as the API is the same. Tools like TypeScript and Flow really shine here.</p><h1 id="8783">Stitching the system together</h1><p id="ad72">Before going into writing the actual REST API, let’s just try and stitch things together in a test. This approach is called <i>Poor Man’s DI — </i>don’t worry, I’ll fill you in later!</p>
<figure id="7c6d">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jeffijoe/cac274a4b793f82787c8a9feb5144114.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="4bc3">You might be thinking: <i>“But Jeff! That code now knows about both modules!” </i>— that’s true, and in a real app (which we will get to in a minute), there needs to be a single source of truth that knows about all the modules being used — those of us donning the DI fancy pants have a name for this holy place: <b>The Composition Root. </b>It is the single place in the application that ties the whole thing together — it composes your modules! Want to swap out that in-memory <i>todosRepository </i>for a RethinkDB implementation? Thi
Options
s is where you go to do the swippety swappety.</p><p id="b35a">The composition root could look like this.</p><h2 id="f130">compositionRoot.js</h2>
<figure id="ba33">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jeffijoe/ae315059a5329c5077f17514e0fb0454.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="90f6"><b>I know, I know what you’re thinking! </b><i>“I don’t know who the user is yet, I’m building a web app! This is useless!”</i> You are absolutely right. There are 2 ways to solve this manually:</p><ul><li>Pass in the <i>currentUser </i>to all the methods that need it — <i>this sucks.</i></li><li>Defer instantiating things until you have all the data you need — <i>this also sucks, because now you need to instantiate things in multiple places.</i></li></ul><p id="e899">To give you an idea of how the second approach would work, <i>and just how much it sucks</i>, here’s how we could do it. This example is using Koa Router.</p>
<figure id="e8ad">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jeffijoe/ad0a4247e4925310f64d893bdca1d483.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="bd1d">And this is just with 2 modules! Imagine if there were 10 (and that’s just for a small app). Yep, <i>sucks.</i></p><p id="9386">So we can all agree that doing DI <i>manually</i> in a large scale app like our amazing multi-tenant todo app simply isn’t going to work for us. Let’s just give up and go back to require. Have a great day.</p><p id="6549"><a href="https://readmedium.com/c01471c09c6d/"><b>… or not! Read part 3 and fix this mess!</b></a></p></article></body>