<?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 Matan Sar-Shalom on Medium]]></title>
        <description><![CDATA[Stories by Matan Sar-Shalom on Medium]]></description>
        <link>https://medium.com/@matansarshalom?source=rss-88ca2b663cbc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Uj4iQY1IlTAq_lDYVJvwvQ.jpeg</url>
            <title>Stories by Matan Sar-Shalom on Medium</title>
            <link>https://medium.com/@matansarshalom?source=rss-88ca2b663cbc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 03:44:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matansarshalom/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[An elegant alternative to switch\case using Angular’s dependency injection.]]></title>
            <link>https://matansarshalom.medium.com/elegant-alternative-to-switch-case-using-angulars-dependency-injection-935afd199d91?source=rss-88ca2b663cbc------2</link>
            <guid isPermaLink="false">https://medium.com/p/935afd199d91</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Matan Sar-Shalom]]></dc:creator>
            <pubDate>Thu, 22 Feb 2018 21:28:39 GMT</pubDate>
            <atom:updated>2020-10-22T07:18:28.635Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*roedigbmFjRYkZobdZWuKg.jpeg" /></figure><p>Switch statements tend to grow and become ugly and nasty beasts, no matter how well you take care of them.</p><p>For example, imagine you have this piece of code in your Angular service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df51eb155b6d9cfaee6477353ee8c3c1/href">https://medium.com/media/df51eb155b6d9cfaee6477353ee8c3c1/href</a></iframe><p>We can leverage Angular’s dependency injection to help us clean up the switch statement.</p><p>The solution is built by working with an interface and InjectionToken. Each class that implements the interface will hold the logic found under a single case block. Each class will be registered to Angular’s DI under our InjectionToken using the multi-option.</p><p>Let&#39;s start with defining the interface that will later be used as the type that will be injected into the service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c76f45aa480ad00324c0cece9a462df/href">https://medium.com/media/2c76f45aa480ad00324c0cece9a462df/href</a></iframe><p>Next, we need to create an InjectionToken based on the above interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5459ec837ffd0bc31d2fd7fc673c5fa9/href">https://medium.com/media/5459ec837ffd0bc31d2fd7fc673c5fa9/href</a></iframe><p>Now, for every case block, we will create a class that will implement our interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd67321036e6427b6919d7343778a8c7/href">https://medium.com/media/fd67321036e6427b6919d7343778a8c7/href</a></iframe><p>We can then register the classes to Angular’s DI using the InjectionToekn:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/453e30078d7e45357d1638e6cd7cbff1/href">https://medium.com/media/453e30078d7e45357d1638e6cd7cbff1/href</a></iframe><p>When multi is set to <em>true</em> it means that our InjectionToken can be registered multiple times as provide and when injecting the token we get back all classes\values under that token:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7101332d2155bc1f763e39542605b8ec/href">https://medium.com/media/7101332d2155bc1f763e39542605b8ec/href</a></iframe><p>Now we can get rid of the switch statement completely and look for the CaseLogicHandler we need and then invoke the logic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a747045a4e3730b0813b1a2da465b993/href">https://medium.com/media/a747045a4e3730b0813b1a2da465b993/href</a></iframe><p>That’s it.</p><p>The solution is more verbose than the switch statement but provides a much cleaner and elegant code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=935afd199d91" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>