<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alfredo Gerardo Narvaez Docimo on Medium]]></title>
        <description><![CDATA[Stories by Alfredo Gerardo Narvaez Docimo on Medium]]></description>
        <link>https://medium.com/@alfredonado?source=rss-e8e002401b9f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*sh1BTnXxmUObXr4cOMrl3g.png</url>
            <title>Stories by Alfredo Gerardo Narvaez Docimo on Medium</title>
            <link>https://medium.com/@alfredonado?source=rss-e8e002401b9f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 13:29:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alfredonado/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A functional approach to Dependency Injection in Javascript]]></title>
            <link>https://medium.com/@alfredonado/a-functional-approach-to-dependency-injection-in-javascript-63848569c984?source=rss-e8e002401b9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/63848569c984</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alfredo Gerardo Narvaez Docimo]]></dc:creator>
            <pubDate>Mon, 27 Jan 2020 08:32:06 GMT</pubDate>
            <atom:updated>2020-01-27T08:32:06.205Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d8PvZ8YcflfbPyPJ3l4MOg.jpeg" /></figure><blockquote>— Oh, you mean this? This is a function that invokes another function with all registered dependencies passed in as parameters. Its basic function composition. Don’t worry, you’ll get the gist of it eventually!</blockquote><p>Boy, that was a rough day for me. I remember this was my first Software Engineering position at a big international company. And there I was, armed with some <strong>solid </strong>foundations in spaghetti code. Notice how I made solid, bolder? Yeah, that’s because for me <strong>SOLID </strong>design principles were mostly something that you either hear from guys like headhunters which usually throw this as the <em>cherry-on-top-of-the-pie</em> for most job offers that land in your LinkedIn inbox, or some skilled Software Engineers that actually know their stuff.</p><p>Okay, moving along. The first thing I noticed when I cloned my first repo was this weird pattern of using two functions for defining things. It was odd, and — at the same time — mesmerizing.</p><h3>Let&#39;s look at this functional approach:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GzkBlTasvCEcXQDTh-1k6Q.png" /><figcaption>This kind of function composition is also known as Higher-Order Functions.</figcaption></figure><p>Yeah, I know. Who would do such a thing, right? does this even make sense? Well to my head at the time <strong>not really. </strong>However, I still really wanted to understand it.</p><p>Lemme share an example with you:</p><p>This is the kind of things we can do with HOF (Higher-Order Functions)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w4GlzzMH7GCXIInL_eSRSA.png" /><figcaption>Basic addition to a number using function composition</figcaption></figure><p>Alright, let’s break down what has happened here.</p><ol><li>We’re creating a <em>function </em>called <em>addToNumber</em></li><li>This <em>function </em>accepts as <em>parameter </em>a number</li><li>The returning value of this <em>function </em>is an <em>anonymous function</em></li><li>This resulting <em>anonymous</em> <em>function </em>accepts as parameter another number</li><li>Finally, we return the addition of these two numbers.</li></ol><p>Let this sink in. I know for a fact that this blew my mind when I first tried to understand it.</p><p>We basically created a <em>function </em>that is able to add <strong>10 </strong>to any number passed in as a parameter. But this feels like… <em>Overkill</em>, right? why not pass in two parameters at once, return the addition, and go enjoy an <em>Iced White Chocolate Mocha</em> while celebrating your hard work? Well to answer this, I’ll share with you how you can use <strong>function composition as an approach to Inversion of Control or Dependency Injection.</strong></p><p>Remember how in school they teach us that <em>mitochondria is the powerhouse of the cell</em>? Well, this guy here will act as our powerhouse:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q4coT4gnAa6WVGa8rPCOdA.png" /></figure><p>What’s happening here? We’re creating a function called <em>injectDependencies </em>and passing in as argument a <em>function </em>that will be <em>invoked </em>with a <strong>dependency object. </strong>I’ll cover this last part in a second, just stick with me for a moment.</p><p>Now, that we have our <em>injectDependencies function </em>created, and our <strong>dependency object </strong>we can go ahead and compose a <em>function </em>that uses injected dependencies like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UZ0E_8xbvxrRmOZaSgkEfA.png" /></figure><p>So, what we’re doing here is:</p><ol><li>Creating a <em>function </em>that gets whatever it needs from the <strong>dependency object</strong> and returning another <em>function</em> that would actually have the logic we want to implement once all dependencies are injected.</li><li>Storing the result from injecting the dependencies to our created <em>function</em> into a constant.</li><li>Calling that new <em>function</em>.</li></ol><p>But we can do better, and here lays the real power of this: what if we want to customize this <em>function </em>with something outside from the <strong>dependency object </strong>scope? We can do something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sS9PmHBNgMCkxrYwRSwrjw.png" /></figure><p>Ah, there. Now we’re having a healthier dinner!</p><p>See, what we did here is allowing the invoked <em>function</em> to use whatever it is being passed as a parameter when it is called.</p><h3>Let’s now combine this by using classes:</h3><p>We can start by creating a class that will handle everything that has to do with our dependencies and expose methods to register new ones:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I4A5cG_u_RjRb8oYny7xrw.png" /></figure><p>Okay, I hope that’s not too different from what we’ve been looking at so far. Are you still with me? Cool. Let’s use this with a more concrete example. We’re going to instantiate a new Injector object and use it to delegate all logic that has to do with the injection of dependencies like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hKJSbM7LsGJZUE5BKAe0jg.png" /></figure><p>Y’all are going to have to forgive my <strong>extravagant </strong>example as my favorite food is <a href="https://en.wikipedia.org/wiki/Arepa">arepas </a>(they are delicious, please do yourself a favor and go have one!).</p><p>Okay, moving on! So far, I hope that all of this starts making sense, but wait — there’s more.</p><p>Let’s go ahead and connect classes through dependency injection. Let’s first create an <strong>Ingredients class:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T2wtT2fizuwg3IOOcn0-tQ.png" /></figure><p>We’re delegating on this class the sole responsibility of telling whether an ingredient is healthy or not, in order to prepare it later on.</p><p>Two important things are happening here, please refer to the <em>init </em>method:</p><ol><li>I am registering a simple array into the <strong>dependency object</strong></li><li>I am registering a <em>function </em>that has dependencies, which will have to be injected.</li></ol><p>Alright, let’s go ahead and create another class in which we can actually include some logic to prepare a dish!</p><p>We’ll call this class <strong>Menu:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cnFHxAsbAP9p6YH5toSGNw.png" /></figure><p>Okay, so two things I want to highlight here:</p><ol><li>Notice how I am registering prepareDish into the <strong>dependency object </strong>but also into our exposedMethods.</li><li>I am consuming a dependency registered by another class directly into the prepareDish <em>function</em></li></ol><p>Alright, now comes the interesting part. Let’s make use of these classes!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*haWUHw9HHcPn0Y5tEgQ2ng.png" /></figure><p>But wait there’s more! Now the real power of this kicks in. Let’s create a simple function that consumes all of our registered dependencies:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N5b8uLtbswDLU8x12iMheg.png" /></figure><p>This function takes two params. One main ingredient and a drink, and uses our registered functions inside the <strong>dependency object </strong>to tell if its healthy or not.</p><blockquote>Notice that we’re able to consume the prepareDish <em>function </em>and the isHealty <em>function </em>because they are registered into our <strong>dependency object. </strong>If we were to comment them out as a dependency in our init method, they would go out of scope, which would lead to an error<em>⚠️</em></blockquote><p>So now, let’s make use of it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FVAnfnyC7_3vYJqtqbEvlg.png" /></figure><p>And just like that, we have basic dependency injection through function composition!</p><p>I really hope you enjoy learning this paradigm, whether you chose to use it or now as it expanded the way I look at Inversion of Control (Dependency Injection) now.</p><p>Happy hacking!🔥🙌</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63848569c984" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>