{"id":30423,"date":"2025-02-05T09:27:32","date_gmt":"2025-02-05T08:27:32","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/?p=30423"},"modified":"2025-02-05T09:32:10","modified_gmt":"2025-02-05T08:32:10","slug":"dependency-injection-typescript-angular-part-0","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/","title":{"rendered":"Understanding Angular \u2014 Exploring Dependency Injection and Design Patterns \u2014 Part 0 \ud83d\udd25\ud83d\ude80"},"content":{"rendered":"\n<p><strong><em>This is the 1st of a series of articles<\/em><\/strong> that have the goal to explore and understand a topic that I see too much neglected in the front-end world\u200a\u2014\u200a<strong><em>Dependency Injection (DI) &amp; Design patterns<\/em><\/strong>.<\/p>\n\n\n\n<p>You will start writing and understanding the basics of <strong><em>DI<\/em>\u200a<\/strong>\u2014\u200awhat is it, how does it work and how works in <a href=\"https:\/\/www.codemotion.com\/magazine\/frontend\/web-developer\/angular-fallback-content-in-ng-content\/\">Angular<\/a>.<\/p>\n\n\n\n<p>Our journey will continue flying between the <strong><em>SOLID<\/em><\/strong> principles, followed by some stops here and there to visit different design patterns and the relative use cases. You also may encounter more stuff along the way (this means that i could change my mind and add more things to the roadmap \ud83d\ude0e)<\/p>\n\n\n\n<p>Let\u2019s stop thinking about the whole journey\u200a\u2014\u200afocus on what\u2019s now!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-dependency-injection-di-nbsp-is\"><strong>What Dependency Injection (DI)&nbsp;is?<\/strong><\/h2>\n\n\n\n<p>Before getting into it here is a very brief and concise description of what the DI is.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-left\">DI is process that encourages the separation of concerns, thereby improving the maintainability and scalability of software systems.<br>At its core, DI is a technique employed to achieve Inversion of Control (IoC). IoC means that the control over the flow of a program\u2019s execution is inverted, with the framework or container managing the dependencies and their life cycles.<\/p>\n<\/blockquote>\n<\/div><\/div>\n\n\n\n<p>Is not that complex, isn\u2019t it? You and me, use this tool everyday without even noticing it. It\u2019s time to understand the magic behind.<\/p>\n\n\n\n<p>Let\u2019s proceed and see\u2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-who-are-the-actors-in-nbsp-di\"><strong>Who are the actors in&nbsp;DI?<\/strong><\/h2>\n\n\n\n<p>There are three major actors that are part of this movie called Dependency Injection.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Dependency,<\/li>\n\n\n\n<li>Client,<\/li>\n\n\n\n<li>Injector (optional).<\/li>\n<\/ol>\n\n\n\n<p>Everyone of them has it\u2019s own importance and the whole flow wouldn\u2019t exist without one of them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-dependency\"><strong>Dependency<\/strong><\/h4>\n\n\n\n<p>A dependency, in this context, refers to an object or service that another object relies upon to perform its function.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-client\"><strong>Client<\/strong><\/h4>\n\n\n\n<p>The client is the class or component that requires a dependency to fulfill its responsibilities. It is the class that will receive the injected dependency.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-injector\"><strong>Injector<\/strong><\/h4>\n\n\n\n<p>The injector is responsible for providing the necessary dependencies to the client. It acts as an intermediary, resolving and injecting the dependencies into the client class\u200a\u2014\u200a<strong><em>here is where the IoC takes place<\/em><\/strong>.<\/p>\n\n\n\n<p>The <strong><em>Injector<\/em><\/strong> is in charge to provide us the correct Service\u2019s instance.<\/p>\n\n\n\n<p>That was a brief introduction of who\u2019s in charge of what, but\u2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-does-di-works-without-nbsp-ioc\"><strong>How does DI works without&nbsp;IoC?<\/strong><\/h2>\n\n\n\n<p>You read that the <strong><em>Injector<\/em><\/strong> is an optional actor in this film, and it is.<\/p>\n\n\n\n<p>Having someone that can do the work for us does not mean that you can ignore how it works behind the scene.<\/p>\n\n\n\n<p>This chapter will be about use the DI without any help, but our brain.<\/p>\n\n\n\n<p>So, let\u2019s see\u2026<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-di-in-vanilla-typescript\"><strong>DI in Vanilla TypeScript<\/strong><\/h4>\n\n\n\n<p>Below you can find a little Stackblitz where DI is used, by hand.<\/p>\n\n\n\n<p>By hand means that you are in charge of passing the correct instance of a service to another one that need it to perform some actions.<\/p>\n\n\n\n<p>In the example below we have 2 classes:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><em>Counter<\/em><\/strong>,<\/li>\n\n\n\n<li><strong><em>ElementHandler<\/em><\/strong><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-counter\"><strong>Counter<\/strong><\/h4>\n\n\n\n<p>The <strong><em>Counter<\/em><\/strong> class uses the <strong><em>ElementHandler<\/em><\/strong> class to perform some simple actions on the <strong><em>HTMLElement<\/em><\/strong> that represent the <strong><em>Counter<\/em><\/strong> that you see on the screen.<\/p>\n\n\n\n<p>Pay attention to the <strong><em>Counter\u2019s class constructor<\/em><\/strong>\u200a\u2014\u200athe argument is a variable of type <strong><em>ElementHandler<\/em><\/strong>, the other class that you are going to see.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import { ElementHandler } from '.\/element';<br><br>export class Counter {<br>  private _elementHandler: ElementHandler;<br>  private _counter = 0;<br><br>  constructor(element: ElementHandler) {<br>    this._elementHandler = element;<br>  }<br><br>  setUpCounter() {<br>    this._elementHandler.attachEventListener('click', () =&gt;<br>      this.setCounter(this._counter + 1)<br>    );<br><br>    this.setCounter(0);<br>  }<br><br>  private setCounter(count: number) {<br>    this._counter = count;<br>    this._elementHandler.setInnerHtml(`count is ${this._counter}`);<br>  }<br>}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-elementhandler\"><strong>ElementHandler<\/strong><\/h4>\n\n\n\n<p>The <strong><em>ElementHandler<\/em><\/strong> class is pretty straight forward\u200a\u2014\u200ait acts as a wrapper for some basic element management (get an Element and add an event to it).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">export class ElementHandler {<br>  private _element: HTMLElement | null = null;<br>  private _id: string;<br><br>  constructor(id: string) {<br>    this._id = id;<br>    this.getElementById(this._id);<br>  }<br><br>  setInnerHtml(value: string) {<br>    if (this._element) this._element.innerHTML = value;<br>  }<br><br>  attachEventListener(event: keyof HTMLElementEventMap, cb: any) {<br>    if (this._element) this._element.addEventListener(event, cb);<br>  }<br><br>  private getElementById(id: string) {<br>    this._element = document.querySelector&lt;HTMLButtonElement&gt;(id)!;<br>  }<br>}<\/pre>\n\n\n\n<p>Putting all together in the\u2026<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-main-ts\"><strong>main.ts<\/strong><\/h4>\n\n\n\n<p>The main file is pretty small, but these are the only lines of code that you need to consider.<\/p>\n\n\n\n<p>As you can see we are <strong><em>manually creating the instances of the services<\/em><\/strong>.<\/p>\n\n\n\n<p>Notice that the <strong><em>elementHandler<\/em><\/strong> variable is the argument for the <strong><em>Counter class<\/em><\/strong>, as you saw when you read about it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const elementHandler = new ElementHandler('#counter');<br>const counter = new Counter(elementHandler);<br><br>counter.setUpCounter();<\/pre>\n\n\n\n<p><a href=\"https:\/\/stackblitz.com\/github\/giorgiogalassi\/di-vanilla-ts?file=src%2Fmain.ts\"><strong>Here<\/strong><\/a> is a project made with Stackblitz that you can use to play around.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-how-does-di-works-with-nbsp-ioc\"><strong>How does DI works with&nbsp;IoC?<\/strong><\/h3>\n\n\n\n<p>Well, DI in a nutshell could be explained like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*xkqJ8Ea8SmZul2tokwNK5g.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Liam \u201cInjector\u201d Neeson from Taken&nbsp;2<\/figcaption><\/figure>\n\n\n\n<p>Or\u2026 if you want to act as the \u201cSerious\u201d Developer you are, like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*W708TM-4IKRm4crm19kquA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Injector diagram<\/figcaption><\/figure>\n\n\n\n<p>You can image the <strong><em>Injector<\/em><\/strong> like a bus, which is filled with all the instances of all our Services. When a component\u200a\u2014\u200ain this example <strong><em>MyComponent<\/em><\/strong> <strong><em>aka the Client\u200a<\/em><\/strong>\u2014\u200aask for a specific class\u200a\u2014\u200ain this case <strong><em>MyService aka the dependency\u200a<\/em><\/strong><em>\u2014\u200a<\/em>the Injector will search within this bus and instance that can satisfy the client request. The founded dependency will be provided to the client, eventually.<\/p>\n\n\n\n<p>In the same StackBlitz above you can see how <strong><em>IoC can be achieve in TypeScript<\/em><\/strong>, using <a href=\"https:\/\/github.com\/inversify\/InversifyJS\" rel=\"noreferrer noopener\" target=\"_blank\"><strong><em>inversify<\/em><\/strong><\/a><strong><em>\u200a<\/em><\/strong>\u2014\u200aone of the external library available in the scene.<\/p>\n\n\n\n<p>I will not go in deep of how <strong><em>inversify<\/em><\/strong> works in detail, because it\u2019s not our focus. You just need to understand that, under the hood, it does what you read previously.<\/p>\n\n\n\n<p>Here is an updated version of the code that makes use of inversify<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-main-ts-with-inversify\"><strong>main.ts with inversify<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">import { Counter } from '.\/counter.ts';<br>import { ElementHandler } from '.\/element.ts';<br>import { Container } from 'inversify';<br><br>const container = new Container();<br><br>container.bind&lt;Counter&gt;(Counter).toSelf();<br>container.bind&lt;ElementHandler&gt;(ElementHandler).toConstantValue(new ElementHandler('#counterIoC'));<br><br>const counterIoC = container.get(Counter);<br><br>counterIoC.setUpCounter();<\/pre>\n\n\n\n<p>Something is starting to cook here and the things begin more technical. One question could easily arise\u2026<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-which-are-the-advantages-of-nbsp-di\"><strong>Which are the advantages of&nbsp;DI?<\/strong><\/h3>\n\n\n\n<p>Well, that\u2019s a really good question my friend.<\/p>\n\n\n\n<p>You can identify three major advantages:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Modularity,<\/li>\n\n\n\n<li>Testing,<\/li>\n\n\n\n<li>Readability and Maintainability.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-modularity\"><strong>Modularity<\/strong><\/h4>\n\n\n\n<p>DI promotes modularity by decoupling components, making it easier to replace or update individual parts of the system.<\/p>\n\n\n\n<p>This is also the <strong><em>S<\/em><\/strong> of the <strong><em>SOLID<\/em><\/strong> principles, which stands for <strong><em>Single-responsibility principle\u200a<\/em><\/strong>\u2014\u200awhich means that a class should do one job, and one job only. Nothing more, nothing less.<\/p>\n\n\n\n<p>You are going to encounter all the letters of the <strong><em>SOLID <\/em><\/strong>principles, because they are the pillars for a clean code and a clean architecture.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-testing\"><strong>Testing<\/strong><\/h4>\n\n\n\n<p>It facilitates unit testing by allowing dependencies to be mocked or substituted during testing.<\/p>\n\n\n\n<p>Remember that you are working with, hopefully, small classes that do one job only\u200a\u2014\u200athey are easier to test as a consequence!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-readability-and-maintainability\"><strong>Readability and Maintainability<\/strong><\/h4>\n\n\n\n<p>Code becomes more readable as dependencies are explicitly defined and separated from the client\u2019s logic.<\/p>\n\n\n\n<p>Our smaller class are handy here as well. Less code to read and to understand and, if you want to explore the connected dependencies, they are all listed in an explicit way.<\/p>\n\n\n\n<p>Now\u2026 you saw the actors that are acting in this movie. Understood how does it work with our special guest Liam Neeson. Examinated the advantages, but\u2026<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-how-does-it-work-within-nbsp-angular\"><strong>How does it work within&nbsp;Angular?<\/strong><\/h3>\n\n\n\n<p>This will be our next topic, that will be addressed in a dedicated article that you will find here in some days.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<p>Thanks for sticking with me and hope that everything is clear so far.<\/p>\n\n\n\n<p>The journey will continue and i will be glad if you want to enjoy the ride with me till the last article of the serie and maybe explore more of mine.<\/p>\n\n\n\n<p>See you in the next one,<br>G.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the 1st of a series of articles that have the goal to explore and understand a topic that I see too much neglected in the front-end world\u200a\u2014\u200aDependency Injection (DI) &amp; Design patterns. You will start writing and understanding the basics of DI\u200a\u2014\u200awhat is it, how does it work and how works in Angular.&#8230; <a class=\"more-link\" href=\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/\">Read more<\/a><\/p>\n","protected":false},"author":302,"featured_media":31919,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[6],"tags":[4141,9907],"collections":[],"class_list":{"0":"post-30423","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-frontend","8":"tag-angular","9":"tag-frameworks","10":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Dependency Injection (DI) in TypeScript and Angular: Part 0<\/title>\n<meta name=\"description\" content=\"Explore Dependency Injection (DI) in TypeScript and Angular. In Part 0, we introduce DI fundamentals, covering its purpose, actors, and benefits in software design.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding Angular \u2014 Exploring Dependency Injection and Design Patterns \u2014 Part 0 \ud83d\udd25\ud83d\ude80\" \/>\n<meta property=\"og:description\" content=\"Explore Dependency Injection (DI) in TypeScript and Angular. In Part 0, we introduce DI fundamentals, covering its purpose, actors, and benefits in software design.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/\" \/>\n<meta property=\"og:site_name\" content=\"Codemotion Magazine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Codemotion.Italy\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-05T08:27:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-05T08:32:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1792\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Giorgio Galassi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:site\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Giorgio Galassi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/\"},\"author\":{\"name\":\"Giorgio Galassi\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/19fc0726e8a2c812a1a85816899fe69a\"},\"headline\":\"Understanding Angular \u2014 Exploring Dependency Injection and Design Patterns \u2014 Part 0 \ud83d\udd25\ud83d\ude80\",\"datePublished\":\"2025-02-05T08:27:32+00:00\",\"dateModified\":\"2025-02-05T08:32:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/\"},\"wordCount\":1152,\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp\",\"keywords\":[\"Angular\",\"Frameworks\"],\"articleSection\":[\"Frontend\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/\",\"name\":\"Dependency Injection (DI) in TypeScript and Angular: Part 0\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp\",\"datePublished\":\"2025-02-05T08:27:32+00:00\",\"dateModified\":\"2025-02-05T08:32:10+00:00\",\"description\":\"Explore Dependency Injection (DI) in TypeScript and Angular. In Part 0, we introduce DI fundamentals, covering its purpose, actors, and benefits in software design.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#primaryimage\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp\",\"width\":1792,\"height\":1024},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Frontend\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Understanding Angular \u2014 Exploring Dependency Injection and Design Patterns \u2014 Part 0 \ud83d\udd25\ud83d\ude80\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"name\":\"Codemotion Magazine\",\"description\":\"We code the future. Together\",\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\",\"name\":\"Codemotion\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"width\":225,\"height\":225,\"caption\":\"Codemotion\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Codemotion.Italy\/\",\"https:\/\/x.com\/CodemotionIT\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/19fc0726e8a2c812a1a85816899fe69a\",\"name\":\"Giorgio Galassi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2025\/04\/profile-big-4-100x100.jpeg\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2025\/04\/profile-big-4-100x100.jpeg\",\"caption\":\"Giorgio Galassi\"},\"description\":\"Giorgio is a Senior Frontend Developer with nearly 8 years of experience building dynamic, user-focused web applications. Specializing in Angular, he also keeps an eye on other frontend frameworks and technologies, always seeking innovative approaches to problem-solving. Beyond coding, Giorgio is deeply involved in the developer community. As a proud Organizer of GDG Roma Citt\u00e0, he fosters knowledge-sharing and collaboration among peers. He enjoys making complex topics more accessible and helping others sharpen their technical skills.\",\"sameAs\":[\"https:\/\/medium.com\/@giorgio.galassi\",\"https:\/\/www.linkedin.com\/in\/giorgiogalassi\/\"],\"url\":\"https:\/\/www.codemotion.com\/magazine\/author\/ggalassi\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Dependency Injection (DI) in TypeScript and Angular: Part 0","description":"Explore Dependency Injection (DI) in TypeScript and Angular. In Part 0, we introduce DI fundamentals, covering its purpose, actors, and benefits in software design.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/","og_locale":"en_US","og_type":"article","og_title":"Understanding Angular \u2014 Exploring Dependency Injection and Design Patterns \u2014 Part 0 \ud83d\udd25\ud83d\ude80","og_description":"Explore Dependency Injection (DI) in TypeScript and Angular. In Part 0, we introduce DI fundamentals, covering its purpose, actors, and benefits in software design.","og_url":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2025-02-05T08:27:32+00:00","article_modified_time":"2025-02-05T08:32:10+00:00","og_image":[{"width":1792,"height":1024,"url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp","type":"image\/webp"}],"author":"Giorgio Galassi","twitter_card":"summary_large_image","twitter_creator":"@CodemotionIT","twitter_site":"@CodemotionIT","twitter_misc":{"Written by":"Giorgio Galassi","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/"},"author":{"name":"Giorgio Galassi","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/19fc0726e8a2c812a1a85816899fe69a"},"headline":"Understanding Angular \u2014 Exploring Dependency Injection and Design Patterns \u2014 Part 0 \ud83d\udd25\ud83d\ude80","datePublished":"2025-02-05T08:27:32+00:00","dateModified":"2025-02-05T08:32:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/"},"wordCount":1152,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp","keywords":["Angular","Frameworks"],"articleSection":["Frontend"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/","url":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/","name":"Dependency Injection (DI) in TypeScript and Angular: Part 0","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp","datePublished":"2025-02-05T08:27:32+00:00","dateModified":"2025-02-05T08:32:10+00:00","description":"Explore Dependency Injection (DI) in TypeScript and Angular. In Part 0, we introduce DI fundamentals, covering its purpose, actors, and benefits in software design.","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp","width":1792,"height":1024},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/dependency-injection-typescript-angular-part-0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"Frontend","item":"https:\/\/www.codemotion.com\/magazine\/frontend\/"},{"@type":"ListItem","position":3,"name":"Understanding Angular \u2014 Exploring Dependency Injection and Design Patterns \u2014 Part 0 \ud83d\udd25\ud83d\ude80"}]},{"@type":"WebSite","@id":"https:\/\/www.codemotion.com\/magazine\/#website","url":"https:\/\/www.codemotion.com\/magazine\/","name":"Codemotion Magazine","description":"We code the future. Together","publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codemotion.com\/magazine\/#organization","name":"Codemotion","url":"https:\/\/www.codemotion.com\/magazine\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","width":225,"height":225,"caption":"Codemotion"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Codemotion.Italy\/","https:\/\/x.com\/CodemotionIT"]},{"@type":"Person","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/19fc0726e8a2c812a1a85816899fe69a","name":"Giorgio Galassi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2025\/04\/profile-big-4-100x100.jpeg","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2025\/04\/profile-big-4-100x100.jpeg","caption":"Giorgio Galassi"},"description":"Giorgio is a Senior Frontend Developer with nearly 8 years of experience building dynamic, user-focused web applications. Specializing in Angular, he also keeps an eye on other frontend frameworks and technologies, always seeking innovative approaches to problem-solving. Beyond coding, Giorgio is deeply involved in the developer community. As a proud Organizer of GDG Roma Citt\u00e0, he fosters knowledge-sharing and collaboration among peers. He enjoys making complex topics more accessible and helping others sharpen their technical skills.","sameAs":["https:\/\/medium.com\/@giorgio.galassi","https:\/\/www.linkedin.com\/in\/giorgiogalassi\/"],"url":"https:\/\/www.codemotion.com\/magazine\/author\/ggalassi\/"}]}},"featured_image_src":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-600x400.webp","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-600x600.webp","author_info":{"display_name":"Giorgio Galassi","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/ggalassi\/"},"uagb_featured_image_src":{"full":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp",1792,1024,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-150x150.webp",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-300x171.webp",300,171,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-768x439.webp",768,439,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-1024x585.webp",1024,585,true],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-1536x878.webp",1536,878,true],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1.webp",1792,1024,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-100x100.webp",100,100,true],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-180x128.webp",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-896x504.webp",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-400x225.webp",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-600x400.webp",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2024\/11\/DALL\u00b7E-2025-02-05-09.24.09-A-futuristic-digital-illustration-representing-Angular-framework-concepts.-The-image-features-a-glowing-Angular-logo-in-the-center-surrounded-by-abst-1-600x600.webp",600,600,true]},"uagb_author_info":{"display_name":"Giorgio Galassi","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/ggalassi\/"},"uagb_comment_info":0,"uagb_excerpt":"This is the 1st of a series of articles that have the goal to explore and understand a topic that I see too much neglected in the front-end world\u200a\u2014\u200aDependency Injection (DI) &amp; Design patterns. You will start writing and understanding the basics of DI\u200a\u2014\u200awhat is it, how does it work and how works in Angular.&#8230;&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/30423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/users\/302"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/comments?post=30423"}],"version-history":[{"count":3,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/30423\/revisions"}],"predecessor-version":[{"id":31921,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/30423\/revisions\/31921"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/31919"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=30423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=30423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=30423"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=30423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}