<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Imamori Daichi</title>
    <description>The latest articles on DEV Community by Imamori Daichi (@aidy1991).</description>
    <link>https://dev.to/aidy1991</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F169469%2F1191a31c-b190-463b-9825-baf0b12a1257.png</url>
      <title>DEV Community: Imamori Daichi</title>
      <link>https://dev.to/aidy1991</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aidy1991"/>
    <language>en</language>
    <item>
      <title>Dependency Injection for TypeScript</title>
      <dc:creator>Imamori Daichi</dc:creator>
      <pubDate>Mon, 20 Sep 2021 07:22:41 +0000</pubDate>
      <link>https://dev.to/aidy1991/dependency-injection-for-typescript-5b7b</link>
      <guid>https://dev.to/aidy1991/dependency-injection-for-typescript-5b7b</guid>
      <description>&lt;h2&gt;
  
  
  Ref
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-frontend-engineer-part1-42mi"&gt;Introduction to Clean Architecture for TypeScript: PART1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part2-11pi"&gt;Introduction to Clean Architecture for TypeScript: PART2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part3-aei"&gt;Introduction to Clean Architecture for TypeScript: PART3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part4-1h5f"&gt;Introduction to Clean Architecture for TypeScript: PART4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Abstract
&lt;/h2&gt;

&lt;p&gt;Clean Architecture deals with concentric architecture with the business domain at the core, but&lt;br&gt;
One of the key principles is the dependency constraint, which states that we should only depend on the inside from the outside.&lt;br&gt;
However, in a straightforward implementation, the processing flow and dependencies would be in the same direction.&lt;br&gt;
Of course, the Clean Architecture also requires processing from the inside to the outside, so a straightforward implementation will result in dependencies from the inside to the outside.&lt;br&gt;
In other words, in order to observe the dependency principle, the flow of processing and the direction of dependency must be reversed.&lt;br&gt;
The technique for solving this problem is called dependency inversion.&lt;br&gt;
As the name suggests, this technique is used to satisfy the Dependency Inversion Principle (DIP).&lt;br&gt;
So how do we reverse the dependency?&lt;br&gt;
In this chapter, we introduce a technique called Dependency Injection (DI), which is commonly used to reverse dependencies.&lt;/p&gt;
&lt;h2&gt;
  
  
  DI library for TypeScript
&lt;/h2&gt;

&lt;p&gt;Dependency Injection is a technique that has been used for a long time in statically typed languages such as Java.&lt;br&gt;
You can build your own mechanism to realize DI, or you can use existing DI libraries (DI frameworks).&lt;br&gt;
For example, in Java, there are many DI libraries such as Spring Framework and google/guice.&lt;br&gt;
These DI libraries allow you to use DI for developing various applications.&lt;/p&gt;

&lt;p&gt;How about in the context of JavaScript and TypeScript?&lt;br&gt;
It may not be common to use DI in web frontend development.&lt;br&gt;
Nevertheless, there are DI libraries available for JavaScript and TypeScript.&lt;br&gt;
For example, AngularJS and Angular have a DI framework built in.&lt;br&gt;
There are also libraries that provide DI functionality on its own, such as &lt;a href="https://github.com/inversify/InversifyJS" rel="noopener noreferrer"&gt;InversifyJS&lt;/a&gt; and &lt;a href="https://github.com/microsoft/tsyringe" rel="noopener noreferrer"&gt;TSyringe&lt;/a&gt;.&lt;br&gt;
Both InversifyJS and TSyringe are DI libraries for use with TypeScript.&lt;br&gt;
InversifyJS is a library with over 5000 Github stars and is used in over 18,000 repositories.&lt;br&gt;
On the other hand, there doesn't seem to be much active development going on these days.&lt;br&gt;
TSyringe is a DI library that is mainly developed by Microsoft. As of September 2020, it has about 1,400 stars on Github, and it seems to be under continuous development.&lt;/p&gt;

&lt;p&gt;In this chapter, we will first introduce a simple method to perform DI without using libraries.&lt;br&gt;
This simplified method uses a simple example to show what DI is trying to accomplish and what problems it has.&lt;br&gt;
We will then explain how TSyringe can be used to solve these problems.&lt;/p&gt;
&lt;h2&gt;
  
  
  Simple DI without libraries
&lt;/h2&gt;

&lt;p&gt;In this section, we will use a simple example to illustrate DI without using libraries.&lt;br&gt;
The goal is to demonstrate an understanding of the overview of DI and the issues involved in DI.&lt;br&gt;
However, as will be explained later, there are some practical problems with the method presented in this section.&lt;br&gt;
In the next section, we will confirm that these problems are solved by DI using the library.&lt;/p&gt;

&lt;p&gt;In this section, we will deal with a simple subject as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Printer&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Printer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;printer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;printer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Print:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Document&lt;/code&gt; will output its own content using the &lt;code&gt;output&lt;/code&gt; method.&lt;br&gt;
In the code above, the &lt;code&gt;output&lt;/code&gt; method uses the class &lt;code&gt;Printer&lt;/code&gt; internally.&lt;br&gt;
&lt;code&gt;Printer&lt;/code&gt; prints out the received string by the &lt;code&gt;print&lt;/code&gt; method.&lt;br&gt;
However, in order to simplify the implementation, we only use &lt;code&gt;console.log&lt;/code&gt; for the output.&lt;br&gt;
The codes to use &lt;code&gt;Document&lt;/code&gt; are as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Document&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Document&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is sample text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// "Print: this is sample text"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the dependencies, we see a dependency from &lt;code&gt;Document&lt;/code&gt; to &lt;code&gt;Printer&lt;/code&gt;.&lt;br&gt;
Now let's assume that &lt;code&gt;Email&lt;/code&gt; is added as an output of &lt;code&gt;Document&lt;/code&gt;.&lt;br&gt;
An example codes for this would look like the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Email&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Printer&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Printer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Printer&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;Printer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Email:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New &lt;code&gt;Email&lt;/code&gt; has been added, and &lt;code&gt;Document&lt;/code&gt; now receives the output method in the constructor.&lt;br&gt;
The dependency in this implementation is still the same as before, from &lt;code&gt;Document&lt;/code&gt; to &lt;code&gt;Printer&lt;/code&gt;.&lt;br&gt;
In addition, &lt;code&gt;Document&lt;/code&gt; now also depends on &lt;code&gt;Email&lt;/code&gt;.&lt;br&gt;
The code for the user side looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Document&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Document&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Printer&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Printer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Email&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;printerDocument&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is sample text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;printerDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// "Print: this is sample text"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;emailDocument&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is sample text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;emailDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// "Email: this is sample text"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you may have already noticed, this implementation violates the DIP.&lt;br&gt;
This is because &lt;code&gt;Document&lt;/code&gt; depends on the concrete implementations &lt;code&gt;Printer&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt;.&lt;br&gt;
This means that if &lt;code&gt;Printer&lt;/code&gt; or &lt;code&gt;Email&lt;/code&gt; is changed, &lt;code&gt;Document&lt;/code&gt; will also be affected.&lt;br&gt;
In a simple example, if you rename the &lt;code&gt;print&lt;/code&gt; method of &lt;code&gt;Printer&lt;/code&gt;, you will need to modify the &lt;code&gt;output&lt;/code&gt; method of &lt;code&gt;Document&lt;/code&gt; too.&lt;br&gt;
Also, if you want to add more output methods for &lt;code&gt;Document&lt;/code&gt;, as you did for &lt;code&gt;Email&lt;/code&gt;, you need to modify &lt;code&gt;Document&lt;/code&gt;.&lt;br&gt;
This is a violation of the Open-Closed Principle (OCP): "open to extensions".&lt;br&gt;
To solve the above problems, we will use DI to reverse the dependencies.&lt;br&gt;
Reverse the dependency in the following steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create &lt;code&gt;Document&lt;/code&gt; interface on &lt;code&gt;DocumentOutputMethod&lt;/code&gt; side&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Printer&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt; implement the &lt;code&gt;DocumentOutputMethod&lt;/code&gt; interface&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Document&lt;/code&gt; implements the process using the interface&lt;/li&gt;
&lt;li&gt;Pass an instance of the class that implements the &lt;code&gt;DocumentOutputMethod&lt;/code&gt; interface to the constructor of &lt;code&gt;Document&lt;/code&gt; in the user side codes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is called Dependency Injection (DI) because the instance to be depended on in step 4 is injected from the outside.&lt;br&gt;
Let's take a look at an example implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;DocumentOutputMethod&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DocumentOutputMethod&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Added the &lt;code&gt;DocumentOutputMethod&lt;/code&gt; interface under the management of &lt;code&gt;Document&lt;/code&gt;.&lt;br&gt;
And &lt;code&gt;Document&lt;/code&gt; uses this &lt;code&gt;DocumentOutputMethod&lt;/code&gt; to implement the &lt;code&gt;output&lt;/code&gt; method.&lt;br&gt;
By doing this, you can see that it no longer depends on specific implementations such as &lt;code&gt;Printer&lt;/code&gt; or &lt;code&gt;Email&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DocumentOutputMethod&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Document&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;DocumentOutputMethod&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Print:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DocumentOutputMethod&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Document&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;DocumentOutputMethod&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Email:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Printer&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt; implement the &lt;code&gt;DocumentOutputMethod&lt;/code&gt; interface.&lt;br&gt;
Added &lt;code&gt;output&lt;/code&gt; method to satisfy the &lt;code&gt;DocumentOutputMethod&lt;/code&gt; interface.&lt;br&gt;
This means that &lt;code&gt;Printer&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt; now depend on &lt;code&gt;Document&lt;/code&gt;.&lt;br&gt;
The above fix reversed the dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Document&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Document&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Printer&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Printer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Email&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;printerDocument&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is sample text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;printerDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;emailDocument&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is sample text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;emailDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, in the code of the user side, the constructor of &lt;code&gt;Document&lt;/code&gt; is passed an instance of a class that implements the &lt;code&gt;DocumentOutputMethod&lt;/code&gt; interface.&lt;br&gt;
Now you can do DI without using the library.&lt;/p&gt;
&lt;h3&gt;
  
  
  Problems with this method
&lt;/h3&gt;

&lt;p&gt;Actually, there is a problem with this method.&lt;br&gt;
That is, when you instantiate &lt;code&gt;Document&lt;/code&gt;, you need to instantiate and pass in &lt;code&gt;Printer&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt;.&lt;br&gt;
This is another way of saying that any code that uses &lt;code&gt;Document&lt;/code&gt; will always depend on &lt;code&gt;Printer&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt;.&lt;br&gt;
In the above example, &lt;code&gt;main.ts&lt;/code&gt; is affected by this, but&lt;br&gt;
If you use &lt;code&gt;Document&lt;/code&gt; in multiple locations, each location will depend on &lt;code&gt;Printer&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt;.&lt;br&gt;
After removing the dependency on the concrete implementation from &lt;code&gt;Document&lt;/code&gt;, this just shifts the dependency elsewhere.&lt;br&gt;
To solve this problem, the dependency on concrete code should be gathered in as few places as possible, as described in &lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part4-1h5f"&gt;Introduction to Clean Architecture for TypeScript: PART4&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So how do we aggregate our dependency on concrete code?&lt;br&gt;
This can be solved by using an object called a DI container, which has a correspondence table of dependencies.&lt;br&gt;
The DI container holds a list of correspondences between the class to be injected (&lt;code&gt;Document&lt;/code&gt; in this case) and the class to inject (&lt;code&gt;Printer&lt;/code&gt; or &lt;code&gt;Email&lt;/code&gt;).&lt;br&gt;
When instantiating the class to be injected, it automatically selects the class to inject from the correspondence table of the DI container, instantiates it, and passes it on.&lt;br&gt;
Then, the description of the correspondence of the DI containers is integrated into the main component or configuration files.&lt;br&gt;
In this way, the class to be injected (&lt;code&gt;Document&lt;/code&gt;) can be used in various places while consolidating the dependencies on concrete code.&lt;/p&gt;

&lt;p&gt;It is of course possible to implement a DI container without any libraries.&lt;br&gt;
However, there are a number of technical issues that need to be resolved, and it is quite difficult to implement them on your own.&lt;br&gt;
This is a brief bullet list of issues that need to be resolved.&lt;br&gt;
If you are interested, please check out the details.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Need to use decorator&lt;/li&gt;
&lt;li&gt;Metadata needs to be retrieved by reflection&lt;/li&gt;
&lt;li&gt;TypeScript interface information is removed at compile time and cannot be mapped in the DI container.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These points have been resolved in the existing DI library.&lt;br&gt;
In the next section, we will show an example of a DI that solves this problem by using TSyringe.&lt;/p&gt;
&lt;h2&gt;
  
  
  TSyringe
&lt;/h2&gt;

&lt;p&gt;TSyringe is a DI library developed under the leadership of Microsoft.&lt;br&gt;
As the name suggests, TSyringe allows you to introduce DI into your TypeScript development.&lt;/p&gt;
&lt;h3&gt;
  
  
  TypeScript Decorators
&lt;/h3&gt;

&lt;p&gt;TSyringe uses an experimental feature in TypeScript called Decorators.&lt;br&gt;
Decorators are in the Stage 2 Draft stage of JavaScript as of March 2021.&lt;br&gt;
As such, there is room for change as a specification.&lt;br&gt;
Similarly, TypeScript has been implemented it as an experimental feature, and its specifications are subject to change in the future.&lt;/p&gt;

&lt;p&gt;So why does TSyringe use Decorators?&lt;br&gt;
This is because the nature of the DI library is such that it works well with decorators (as a general language feature).&lt;br&gt;
The DI library needs to know in some way the class to which it is injecting dependencies.&lt;br&gt;
Before decorators were used, the injection target was set by a configuration file.&lt;br&gt;
In this way, it is not possible to know what the DI settings are in the file where the class is declared.&lt;br&gt;
There is also the cost of writing and managing configuration files.&lt;br&gt;
By using decorators, these problems can be solved.&lt;br&gt;
The following is an example of using TypeScript Decorators (not related to TSyringe).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;methodDecorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;descriptor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropertyDescriptor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is method decorator f()&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;classDecorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is class decorator f()&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;classDecorator&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SampleClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;methodDecorator&lt;/span&gt;
    &lt;span class="nf"&gt;hoge&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to use Decorators in TypeScript, you need to add the following setting in the compiler options.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;compilerOptions&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;experimentalDecorators&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installation and initial setup
&lt;/h3&gt;

&lt;p&gt;This section describes the setup for using TSyringe.&lt;br&gt;
The information here is based on official documentation.&lt;br&gt;
First, install the package.&lt;/p&gt;

&lt;p&gt;Install the package &lt;code&gt;tsyringe&lt;/code&gt; by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; tsyringe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn add tsyringe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TSyringe uses an experimental feature called Decorators in TypeScript, so we will rewrite &lt;code&gt;tsconfig.js&lt;/code&gt; as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;compilerOptions&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;experimentalDecorators&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;emitDecoratorMetadata&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use Decorators by enabling &lt;code&gt;experimentalDecorators&lt;/code&gt;.&lt;br&gt;
You can also enable &lt;code&gt;emitDecoratorMetadata&lt;/code&gt; to allow the decorator to handle a lot of metadata.&lt;br&gt;
To enable the Reflect API to handle metadata, you need to select one of the following libraries to use as a polyfill.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reflect-metadata&lt;/li&gt;
&lt;li&gt;core-js (core-js/es7/reflect)&lt;/li&gt;
&lt;li&gt;reflection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example, we install &lt;code&gt;reflect-metadata&lt;/code&gt; as shown below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; reflect-metadata
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add reflect-metadata
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;reflect-metadata&lt;/code&gt; needs to be &lt;code&gt;import&lt;/code&gt; only once before using DI.&lt;br&gt;
It is a good idea to do &lt;code&gt;import&lt;/code&gt; at the top level as much as possible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;reflect-metadata&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you are ready to use TSyringe.&lt;br&gt;
Next, let's look at how to actually do DI.&lt;/p&gt;
&lt;h3&gt;
  
  
  Define the class where you want to do DI
&lt;/h3&gt;

&lt;p&gt;TSyringe provides a simple API for doing DI.&lt;br&gt;
The implementation code differs depending on whether &lt;code&gt;interface&lt;/code&gt; is used for the type of the object to be injected or not.&lt;br&gt;
Here is an example of how to use &lt;code&gt;interface&lt;/code&gt;.&lt;br&gt;
For more information, please refer to the official documentation.&lt;/p&gt;

&lt;p&gt;First, define the type to be injected with &lt;code&gt;interface&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// apiInterface.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;UserApiInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;UserApiInterface&lt;/code&gt; defines an API for retrieving user information.&lt;br&gt;
It is assumed that the class that implements this interface includes processes such as communicating with the API server and connecting directly to the DB.&lt;br&gt;
In the following, as a mock, we will implement a class that receives a user ID and returns static information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// apiImpl.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserApiInterface&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./apiInterface&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserApiImpl&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;UserApiInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`This is fetchUse of UserApi: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, define the class in which &lt;code&gt;UserApiInterface&lt;/code&gt; should be injected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// api.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;injectable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inject&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tsyringe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserApiInterface&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./apiInterface&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserApi&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UserApiInterface&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserApiInterface&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;UserApi&lt;/code&gt; receives an object of type &lt;code&gt;UserApiInterface&lt;/code&gt; in &lt;code&gt;constructor&lt;/code&gt; as an argument and keeps it as a private member.&lt;br&gt;
Then, in &lt;code&gt;UserApi.fetchUser()&lt;/code&gt;, it calls &lt;code&gt;api.fetchUser()&lt;/code&gt;, which is implemented in &lt;code&gt;api&lt;/code&gt;.&lt;br&gt;
In this way, &lt;code&gt;UserApi&lt;/code&gt; can change its behavior depending on the object passed in &lt;code&gt;constructor&lt;/code&gt;.&lt;br&gt;
By writing &lt;code&gt;@inject('UserApiInterface')&lt;/code&gt; in &lt;code&gt;constructor&lt;/code&gt;, we pass the interface metadata to the DI library side.&lt;/p&gt;
&lt;h3&gt;
  
  
  Injecting an object
&lt;/h3&gt;

&lt;p&gt;Now, inject the object into &lt;code&gt;UserApi&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tsyringe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UseApiImpl&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./apiImpl&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserApi&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UserApiInterface&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserApiImpl&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userApi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;UserApi&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;container&lt;/code&gt; is TSyringe's DI container, which holds the information to perform DI.&lt;br&gt;
This &lt;code&gt;container&lt;/code&gt;'s &lt;code&gt;register()&lt;/code&gt; method sets the object to be injected.&lt;br&gt;
In this example, we are configuring &lt;code&gt;UserApiInterface&lt;/code&gt; to inject &lt;code&gt;UserApiImpl&lt;/code&gt;.&lt;br&gt;
And finally, &lt;code&gt;container.resolve(UseApi)&lt;/code&gt; instantiates &lt;code&gt;UserApi&lt;/code&gt;.&lt;br&gt;
At this time, &lt;code&gt;container&lt;/code&gt; instantiates &lt;code&gt;UserApiImpl&lt;/code&gt; and passes it to &lt;code&gt;UserApi&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In other words, TSyringe is a mechanism to create instances while resolving dependencies (which registered by &lt;code&gt;register()&lt;/code&gt;) by &lt;code&gt;resolve()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the basic usage of TSyringe.&lt;br&gt;
This DI mechanism can be used to reverse the dependency.&lt;/p&gt;
&lt;h2&gt;
  
  
  Dependency Inversion with DI Library
&lt;/h2&gt;

&lt;p&gt;In the previous section, we presented an example of DI using TSyringe.&lt;br&gt;
As many readers may have noticed from the implementation examples, using a DI library like TSyringe does not automatically reverse the dependencies.&lt;br&gt;
The DI library does this by externally selecting and injecting the objects that a class depends on.&lt;br&gt;
Therefore, it is possible to reverse the dependency by using it properly, but on the other hand, it is not possible to change the direction of the dependency if it is not used properly.&lt;br&gt;
This section describes how to use the DI library to reverse the dependencies.&lt;/p&gt;
&lt;h3&gt;
  
  
  Design modules
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdp0hbcgd8jqwwb2ih700.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdp0hbcgd8jqwwb2ih700.png" alt="user-database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, let's design modules that will be the subject of our example.&lt;br&gt;
In this section, we will consider the &lt;code&gt;User&lt;/code&gt; model, which is the domain model, and &lt;code&gt;Database&lt;/code&gt;, which is responsible for persisting the model.&lt;br&gt;
&lt;code&gt;User&lt;/code&gt; corresponds to the business logic and is located at the center of the concentric circles diagram.&lt;br&gt;
And since &lt;code&gt;Dataset&lt;/code&gt; is a concrete module, it is located outside the concentric diagram.&lt;br&gt;
Therefore, in accordance with Clean Architecture, the dependency goes from &lt;code&gt;Database&lt;/code&gt; to &lt;code&gt;User&lt;/code&gt;.&lt;br&gt;
This is represented in the diagram as the above image.&lt;br&gt;
&lt;code&gt;Database&lt;/code&gt; can know about &lt;code&gt;User&lt;/code&gt;, but conversely, &lt;code&gt;User&lt;/code&gt; cannot know about &lt;code&gt;Database&lt;/code&gt;.&lt;br&gt;
For the sake of illustration, let's assume that one file corresponds to one module, and create &lt;code&gt;user.ts&lt;/code&gt; and &lt;code&gt;database.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, let's define each one without considering the correct dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Database&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./database&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;User&lt;/code&gt; can be set to &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt;.&lt;br&gt;
It persists itself via &lt;code&gt;Database&lt;/code&gt; by &lt;code&gt;User.save()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Database&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; has been saved`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Database&lt;/code&gt; will receive the &lt;code&gt;User&lt;/code&gt; model and persist it.&lt;br&gt;
However, for the sake of simplicity, we will only display the received &lt;code&gt;User&lt;/code&gt;'s &lt;code&gt;name&lt;/code&gt; in the console.&lt;/p&gt;

&lt;p&gt;Let's take a look at the dependency between &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Database&lt;/code&gt; defined so far.&lt;br&gt;
&lt;code&gt;Database&lt;/code&gt; is &lt;code&gt;import&lt;/code&gt; the type information of &lt;code&gt;User&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;User&lt;/code&gt; also uses &lt;code&gt;Database&lt;/code&gt; as &lt;code&gt;import&lt;/code&gt;, but&lt;br&gt;
This is because &lt;code&gt;User&lt;/code&gt; uses &lt;code&gt;Database&lt;/code&gt; for persistence.&lt;br&gt;
If you look at these dependencies, you can see that they violate the dependency rules.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0n7p3s2n3mv81kt1k37.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0n7p3s2n3mv81kt1k37.png" alt="user-database-bidirectional"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's fix the dependency to the correct orientation.&lt;/p&gt;
&lt;h3&gt;
  
  
  Reversing dependencies between modules
&lt;/h3&gt;

&lt;p&gt;First, define &lt;code&gt;DatabaseInterface&lt;/code&gt; in &lt;code&gt;user.ts&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DatabaseInterface&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;DatabaseInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;save&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change the &lt;code&gt;constructor&lt;/code&gt; argument of &lt;code&gt;User&lt;/code&gt; to &lt;code&gt;DatabaseInterface&lt;/code&gt;.&lt;br&gt;
This will remove &lt;code&gt;import&lt;/code&gt; from &lt;code&gt;user.ts&lt;/code&gt; to &lt;code&gt;database.ts&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;User&lt;/code&gt; is no longer dependent on &lt;code&gt;Database&lt;/code&gt;.&lt;br&gt;
Next, modify &lt;code&gt;Database&lt;/code&gt; so that it implements &lt;code&gt;DatabaseInterface&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DatabaseInterface&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Database&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;DatabaseInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; has been saved`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Database&lt;/code&gt; is importing &lt;code&gt;User&lt;/code&gt;, which turns out to be fine according to the dependency rules we were aiming for.&lt;br&gt;
Now we have satisfied the dependency rule.&lt;br&gt;
The last step is to configure TSyringe.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;injectable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inject&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tsyringe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DatabaseInterface&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DatabaseInterface&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;$export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;DatabaseInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;save&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Added &lt;code&gt;User&lt;/code&gt; with &lt;code&gt;@injectable()&lt;/code&gt; and added &lt;code&gt;database&lt;/code&gt; argument of &lt;code&gt;constructor&lt;/code&gt; with the decorator &lt;code&gt;@inject('DatabaseInterface')&lt;/code&gt;.&lt;br&gt;
Now you can inject &lt;code&gt;Database&lt;/code&gt; into &lt;code&gt;User&lt;/code&gt; by &lt;code&gt;container&lt;/code&gt;.&lt;br&gt;
Let's take a look at the code that uses these modules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tsyringe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./di/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Database&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./di/database&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DatabaseInterface&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Database&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;test user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 'test user saved' will be displayed in the console&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This reversed the dependency and helped us to follow the rules.&lt;br&gt;
The important thing here is to define &lt;code&gt;DatabaseInterface&lt;/code&gt; , on which &lt;code&gt;User&lt;/code&gt; originally depends, in the &lt;code&gt;User&lt;/code&gt; module.&lt;br&gt;
In this way, &lt;code&gt;User&lt;/code&gt; can manage &lt;code&gt;DatabaseInterface&lt;/code&gt; within the scope of its own responsibilities without directly relying on &lt;code&gt;Database&lt;/code&gt;.&lt;br&gt;
In other words, if you define &lt;code&gt;DatabaseInterface&lt;/code&gt; in the &lt;code&gt;Database&lt;/code&gt; module, it will lose its meaning.&lt;/p&gt;

&lt;p&gt;Now we know that with proper use of interfaces and DI, we can reverse the dependencies between modules.&lt;/p&gt;

&lt;h3&gt;
  
  
  Aggregating dependencies on concrete code
&lt;/h3&gt;

&lt;p&gt;As described in the previous section, DI without libraries has a problem that it cannot aggregate dependencies on concrete code.&lt;br&gt;
In the example in this section using TSyringe, we can aggregate &lt;code&gt;container.register()&lt;/code&gt; into &lt;code&gt;app.ts&lt;/code&gt;.&lt;br&gt;
And by using &lt;code&gt;container.resolve(User)&lt;/code&gt;, the code on the side using &lt;code&gt;User&lt;/code&gt; no longer depends on &lt;code&gt;Database&lt;/code&gt;.&lt;br&gt;
In this way, we can see that the problem that the simple DI without the library has been solved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ads
&lt;/h2&gt;

&lt;p&gt;This article is an excerpt from the first chapter of the book "React Clean Architecture". Some wording has been changed. &lt;a href="https://www.amazon.co.jp/dp/B09FG94392/" rel="noopener noreferrer"&gt;You can buy the kindle version of this book from amazon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" alt="React Clean Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>di</category>
    </item>
    <item>
      <title>Introduction to Clean Architecture for TypeScript: PART4</title>
      <dc:creator>Imamori Daichi</dc:creator>
      <pubDate>Sun, 12 Sep 2021 02:17:19 +0000</pubDate>
      <link>https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part4-1h5f</link>
      <guid>https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part4-1h5f</guid>
      <description>&lt;h2&gt;
  
  
  Refs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-frontend-engineer-part1-42mi"&gt;Introduction to Clean Architecture for TypeScript: PART1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part2-11pi"&gt;Introduction to Clean Architecture for TypeScript: PART2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part3-aei"&gt;Introduction to Clean Architecture for TypeScript: PART3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Clean Architecture
&lt;/h2&gt;

&lt;p&gt;In this section, we will finally discuss the Clean Architecture.&lt;br&gt;
As we have discussed in this chapter, Clean Architecture focuses on how to protect the core business domain of the product from other dependencies.&lt;br&gt;
Decouple the business domain from specific and changeable technologies such as UI View, DB, and storage.&lt;br&gt;
By doing so, you can protect critical business rules from change and separate business decisions from technology factors.&lt;br&gt;
So how exactly do you protect your business domain from change?&lt;br&gt;
In this section, we will answer that question.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzk4nhwuwsrjbj349czec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzk4nhwuwsrjbj349czec.png"&gt;&lt;/a&gt;&lt;br&gt;Robert C, Martin. "The Clean Architecture". Clean Architecture. Pearson Education.
  &lt;/p&gt;

&lt;p&gt;The concentric circles in this figure represent Clean Architecture in a nutshell.&lt;br&gt;
Looking at this diagram, you can see that each layer is arranged in concentric circles with the Entity at the center.&lt;br&gt;
The arrows are drawn from the outside to the inside, which represents the dependency rule.&lt;br&gt;
Then, there is a collection of squares and arrows in the lower right corner of the figure, which represent the reversal of the processing flow and dependencies.&lt;br&gt;
We have already discussed dependency inversion in the section "Dependency Injection", and as you can see in this figure, dependency inversion is a very important concept.&lt;/p&gt;

&lt;p&gt;Now, let's take a closer look at the elements that make up the Clean Architecture.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependency rules
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbg02onxub12ixxsfz2ja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbg02onxub12ixxsfz2ja.png" alt="circles-dependency"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This figure illustrates concentric circles and the direction of dependencies.&lt;br&gt;
There are business rules called Entities in the center, and layers are arranged in concentric circles around them.&lt;br&gt;
And this diagram contains important rules about dependencies.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Source code dependencies must point only inward, toward higher-level policies.&lt;br&gt;
(Robert C, Martin. "The Clean Architecture". Clean Architecture. Pearson Education. )&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To rephrase this rule, we can say that the inner element must not depend on the outer element.&lt;br&gt;
For example, a Use case can depend on an Entity, but the Entity cannot be the details of the Use case.&lt;br&gt;
In this way, the Entity is isolated and protected from outside changes.&lt;/p&gt;

&lt;p&gt;In this concentric circle diagram, there is a "outside world of the application" outside the circle.&lt;br&gt;
And the outermost layers, such as the DB and UI, are connected to the outside world.&lt;br&gt;
In general, applications are driven by signals from the outside world, such as user interactions, DB rewrites, and event subscriptions.&lt;br&gt;
In other words, the starting point of the process is the outer boundary of the concentric circle, which propagates toward the center of the circle.&lt;br&gt;
Then, the content processed inside the circle is transmitted to the outside again, and influences the outside world by presenting it to the user or rewriting it in the DB.&lt;br&gt;
To put it another way, the flow of processing starts from the outside of the concentric circles, goes through the inside, and returns to the outside again.&lt;br&gt;
The important thing here is that the flow of processing and dependencies are reversed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4g29sd0low3o2kklmavc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4g29sd0low3o2kklmavc.png" alt="process-direction"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DIP and DI can control this reversal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Entity
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Entities encapsulate enterprise-wide Critical Business Rules.&lt;br&gt;
An entity can be an object with methods, or it can be a set of data structures and functions.&lt;br&gt;
It doesn't matter so long as the entities can be used by many different applications in the enterprise.&lt;br&gt;
(Robert C, Martin. "The Clean Architecture". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Entities represent the rules of the business that the company is engaged in.&lt;br&gt;
Business rules are the core elements for applications, and they are configured to protect cores.&lt;br&gt;
Entities can be difficult to define clearly.&lt;br&gt;
The definition should be flexible, depending on the business the company is working on and the applications that make it possible.&lt;br&gt;
To think about what an Entity is, especially for an SPA such as a React application, we need to consider&lt;br&gt;
API servers and backend for frontend (BFF) are deeply involved.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use case
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The software in the use cases layer contains application-specific business rules.&lt;br&gt;
(Robert C, Martin. "The Clean Architecture". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whereas Entities dealt with the entire business of an enterprise, Use cases deal with the business rules specific to that application.&lt;br&gt;
The Use cases perform the necessary processing for the application while manipulating the Entities.&lt;br&gt;
In other words, Entities abstract the processing across multiple applications, and more abstract rules than Use cases.&lt;/p&gt;

&lt;p&gt;Again, how to handle Use cases in building an SPA is a matter of debate.&lt;/p&gt;

&lt;h4&gt;
  
  
  Framework
&lt;/h4&gt;

&lt;p&gt;In this book, we will focus on a specific view library called React and how to incorporate Clean Architecture ideas into React applications.&lt;br&gt;
However, in fact, the Clean Architecture is designed to be independent of any particular framework or library.&lt;br&gt;
In other words, it doesn't matter whether you use React or Vue.js for Clean Architecture.&lt;br&gt;
This is because the goal of this architecture is to separate frameworks and libraries from the application and make them easy to replace.&lt;br&gt;
Ideally, Clean Architecture should be applied to SPA, and the design should be able to switch between view frameworks (and libraries),&lt;br&gt;
but as a preliminary step, this book will focus on React while understanding that aspect of the Clean Architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Main component
&lt;/h2&gt;

&lt;p&gt;In general, there are several parts of the product code that do not satisfy the Dependency Inversion Principle (DIP).&lt;br&gt;
In other words, there are some components that are dependent on concrete components.&lt;br&gt;
Because concrete components are unstable and prone to change, components that depend on them are also affected by these changes.&lt;br&gt;
Clean Architecture uses DI (Dependency Injection) to reverse dependencies and eliminate dependencies on concrete components.&lt;br&gt;
However, in order to do DI, we need to depend on concrete components.&lt;br&gt;
This is because DI is the process of indicating to an interface which concrete components to use.&lt;br&gt;
Thus, even if DI is applied to DIP violations, the dependency on concrete components will not disappear.&lt;br&gt;
Therefore, we will consider aggregating the dependencies of DI on concrete objects into one place. That part is called Main component.&lt;br&gt;
It is called this because this component often contains the main function.&lt;br&gt;
In other words, we use DI to fix multiple DIP violations in the product code to appropriate dependencies direction and aggregating dependencies on concrete components to Main component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F672jfiuhmgbgxwvkjdci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F672jfiuhmgbgxwvkjdci.png" alt="main-component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The dependency on concrete components cannot be completely removed from the product code.&lt;br&gt;
This is because you cannot use a concrete component without depending on it.&lt;br&gt;
Therefore, we can reduce the number of DIP violations by creating an element like Main component that takes care of the dependency on the concrete component.&lt;br&gt;
Of course, it is ideal to aggregate them in one place, but even if that is not the case, we need to aggregate them in as few places as possible.&lt;br&gt;
It is important to take into account the product's characteristics and the team's culture to design where Main components should be and what their roles should be.&lt;/p&gt;

&lt;h3&gt;
  
  
  About the number and naming of layers
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The circles are intended to be schematic: You may find that you need more than just these four. There's no rule that says you must always have just these four. However, the Dependency Rule always applies. Source code dependencies always point inward.&lt;br&gt;
(Robert C, Martin. "The Clean Architecture". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As Robert C. Martin notes, it is not always right to obediently follow what is written in these concentric circles.&lt;br&gt;
The most important thing is the dependency rule, not the number of concentric layers or their naming.&lt;br&gt;
There is no mention of layer naming in "Clean Architecture".&lt;br&gt;
On the other hand, naming your own unnecessarily would mean throwing away the benefit of following a particular architecture.&lt;br&gt;
This is because one of the advantages of following methods such as DDD or Clean Architecture is that developers who already know those methods for a particular naming can understand the architecture of the application at a glance.&lt;br&gt;
For example, if names such as Entity, Use case, Repository, etc. are used, you can guess what role the module will play without looking at the code details.&lt;br&gt;
Thus, even though we are adopting the Clean Architecture, it can be confusing to give it a different name than the naming of Entity or Use case.&lt;br&gt;
However, SPA and React have a naming culture that has been used traditionally as well.&lt;br&gt;
For example, Store, Dispatcher, Reducer, etc. used in Flux.&lt;br&gt;
How to map these naming cultures to the layered structure of the Clean Architecture is a matter of debate.&lt;br&gt;
This will be key to implementing Clean Architecture in React applications.&lt;br&gt;
These correspondences will be discussed a bit in &lt;a href="https://www.amazon.co.jp/dp/B09FG94392/" rel="noopener noreferrer"&gt;the original book&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Ads
&lt;/h2&gt;

&lt;p&gt;This article is an excerpt from the first chapter of the book "React Clean Architecture". Some wording has been changed. &lt;a href="https://www.amazon.co.jp/dp/B09FG94392/" rel="noopener noreferrer"&gt;You can buy the kindle version of this book from amazon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" alt="React Clean Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cleanarchitecture</category>
      <category>frontend</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Introduction to Clean Architecture for TypeScript: PART3</title>
      <dc:creator>Imamori Daichi</dc:creator>
      <pubDate>Sun, 12 Sep 2021 02:09:17 +0000</pubDate>
      <link>https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part3-aei</link>
      <guid>https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part3-aei</guid>
      <description>&lt;h2&gt;
  
  
  Refs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-frontend-engineer-part1-42mi"&gt;Introduction to Clean Architecture for TypeScript: PART1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part2-11pi"&gt;Introduction to Clean Architecture for TypeScript: PART2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part4-1h5f"&gt;Introduction to Clean Architecture for TypeScript: PART4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Component Principles
&lt;/h2&gt;

&lt;p&gt;This section describes the component-level design principles.&lt;br&gt;
As mentioned earlier, SOLID is a design principle for connections between modules.&lt;br&gt;
In Clean Architecture, a component is a set of modules, which is the unit of deployment.&lt;br&gt;
In other words, a component is a collection of modules designed based on SOLID.&lt;br&gt;
The component principles described in this section guide how to design components.&lt;br&gt;
Some of the principles of the component are similar to SOLID.&lt;br&gt;
This suggests that the main ideas of these principles can be applied recursively regardless of the size of the component.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a component?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Components are the units of deployment.&lt;br&gt;
 They are the smallest entities that can be deployed as part of a system.&lt;br&gt;
 In Java, they are jar files. In Ruby, they are gem files. In .Net, they are DLLs.&lt;br&gt;
 In compiled languages, they are aggregations of binary files.&lt;br&gt;
 In interpreted languages, they are aggregations of source files.&lt;br&gt;
 In all languages, they are the granule of deployment.&lt;br&gt;
(Robert C, Martin. "Components". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In TypeScript, for example, each package managed by npm is a deployment unit and a component.&lt;br&gt;
Web Components, as the name implies, are also components.&lt;br&gt;
What is difficult to determine is what is a component in an application development.&lt;br&gt;
If you divide the compilation granularity appropriately into jar files as in a Java application, they become components.&lt;br&gt;
On the other hand, TypeScript applications are usually transpiled as a whole.&lt;br&gt;
In other words, an application is a single deployment unit.&lt;br&gt;
Of course, we may break it up into chunks to optimize delivery, but that is more to reduce the load on the client side rather than to reduce the granularity of build and deployment.&lt;br&gt;
With this background, it will be difficult to adopt the "component = deployment unit" approach as is.&lt;br&gt;
On the other hand, this doesn't mean that the component principle is useless for designing React applications.&lt;br&gt;
For example, in developing an application, processes that are highly reusable may be separated as libraries.&lt;br&gt;
When this happens, it means that the library is recognized as a separate component from the application.&lt;br&gt;
Have you ever made a library without being sure, because "it's highly reusable" or "it looks better as a library"?&lt;br&gt;
By knowing these principles, you will be able to determine how components should be made independent from the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reuse/Release Equivalence Principle (REP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The granule of reuse is the granule of release.&lt;br&gt;
(Robert C, Martin. "Component Cohesion". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Reuse/Release Equivalence Principle (REP), as the name implies, is the principle that granule of reuse and granule of release should be equivalent.&lt;br&gt;
As an example, consider the reuse of a component A. Suppose that the desired function cannot be reused without another component B.&lt;br&gt;
In this case, the unit of reuse and the unit of release are different.This is an example of "unit of reuse &amp;lt; unit of release".&lt;br&gt;
On the other hand, if "unit of reuse &amp;gt; unit of release", then a single release unit contains multiple granule of reuse.&lt;br&gt;
In this case, if you modify only one reuse unit, you will also need to release it.&lt;br&gt;
From the above, by making the unit of reuse and the unit of release equivalent, the cohesion of components can be improved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Closure Principle (CCP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Gather into components those classes that change for the same reasons and at the same times.&lt;br&gt;
 Separate into different components those classes that change at different times and for different reasons.&lt;br&gt;
(Robert C, Martin. "Component Cohesion". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gue33cafa1mnn0556y1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gue33cafa1mnn0556y1.png" alt="ccp"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Common Core Principle of Closure (CCP) is a component version of the Single Responsibility Principle (SRP).&lt;br&gt;
What the two principles have in common is that one element should not be changed for more than one reason.&lt;br&gt;
In the case of SRP, an element is a class, and in the case of CCP, it is a component.&lt;/p&gt;

&lt;p&gt;CCP is also closely related to the Open-Closed Principle (OCP).&lt;br&gt;
The "closed" in OCP meant closed to modification.&lt;br&gt;
CCP will change this wording to "combine similar types of changes into one component" to achieve the same goal.&lt;br&gt;
We aim to achieve the same goal.&lt;/p&gt;

&lt;p&gt;When you want to cut out a library from your application, you need to pay attention to when that library is going to be modified.&lt;br&gt;
For example, if you want to cut out common UI components to be used in multiple applications, be careful that these UI components are really changed for different reasons than the application.&lt;br&gt;
You also need to pay attention to whether that each element of cut out UI component will be modified for the same reason.&lt;br&gt;
This is because elements that change for different reasons need to be separated into different components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Reuse Principle (CRP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't force users of a component to depend on things they don't need.&lt;br&gt;
(Robert C, Martin. "Component Cohesion". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Common Reuse Principle (CRP) is a principle for determining which elements should be combined into components in the same way as CCP.&lt;br&gt;
While CPP focuses on the reason for the change, CRP focuses on the user's usage.&lt;/p&gt;

&lt;p&gt;It is also a generalization of Interface Separation Principle (ISP) to components.&lt;br&gt;
What CRP and ISP have in common is that users should not rely on what users do not use.&lt;/p&gt;

&lt;p&gt;CRP can help you determine not only which elements should be included in a component, but also which elements should not be included.&lt;br&gt;
By assuming the user who will use the component, we can ensure that the component does not contain elements that will not be used by that user.&lt;/p&gt;

&lt;p&gt;In general, I think there are multiple possible use cases for a component.&lt;br&gt;
For example, there are libraries for date manipulation such as Moment.js.&lt;br&gt;
This library is divided into two components: Moment and Moment Timezone.&lt;br&gt;
This is because there are two use cases where you want to perform date manipulations: one where you want to take timezone into account, and one where you do not.&lt;br&gt;
If we follow the CRP approach, we should avoid forcing users who do not need timezone handling to rely on the timezone module.&lt;br&gt;
In other words, assuming that the use case of not using timezone is not negligible, this kind of component division in Moment.js satisfies the CRP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Acyclic Dependencies Principle (ADP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Allow no cycles in the component dependency graph.&lt;br&gt;
(Robert C, Martin. "Component Coupling". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Acyclic Dependency Principle (ADP), as the name implies, calls for dependencies to be acyclic.&lt;br&gt;
In TypeScript, it can be detected by eslint rule import/no-cycle.&lt;br&gt;
It is easy to imagine that the cycle of dependency can lead to many unexpected problems.&lt;br&gt;
For example, if class A and class B depend on each other, it is easy to detect and imagine the possible problems.&lt;br&gt;
In an extreme case, if class A and class B instantiate each other in the constructor, the process will loop.&lt;br&gt;
A little more confusing is the case of indirect circular dependence. In this case, the problem is that a modification of one side indirectly affects the other.&lt;br&gt;
Suppose that class C and class D are indirectly circularly dependent on each other. Then, changes to class C will have some effect on class D.&lt;br&gt;
Then class D will be modified to accommodate the changes in class C.&lt;br&gt;
In this case, class C also depends on class D, so class C is also affected by the modification of class D.&lt;br&gt;
This is even though the modifications to class D were intended to address changes in class C.&lt;br&gt;
Thus, the cyclic of dependencies means that the effects on change are cyclic.&lt;br&gt;
For products with larger applications and more developers, these issues can be hard to ignore.&lt;/p&gt;

&lt;p&gt;In order to solve the circular dependency, we need to rethink the division of components.&lt;br&gt;
Specifically, we will cut out the elements that class C and class D have in common and make them into new components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stable Dependencies Principle (SDP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Depend in the direction of stability.&lt;br&gt;
(Robert C, Martin. "Component Coupling". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stable Dependency Principle (SDP) defines the relationship between dependency and stability.&lt;br&gt;
As explained in SOLID's Dependency Inversion Principle (DIP), code stability and dependency direction are closely related.&lt;br&gt;
SDP gives the details of the relation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stability
&lt;/h3&gt;

&lt;p&gt;So how is the stability level determined?&lt;br&gt;
In Clean Architecture, stability is defined as "something that cannot be easily moved".&lt;br&gt;
A component that is "hard to change" is said to be a stable component.&lt;br&gt;
In other words, it is dependent on many components.&lt;br&gt;
This is because if you change a component that is dependent on many other components, it will affect many other components and they will need to be adjusted.&lt;br&gt;
The number of dependencies from other components determines the stability of that component.&lt;br&gt;
If you want a component to be stable, design it to be dependent on other components.&lt;br&gt;
On the other hand, components that are expected to change a lot need to be easy to modify, and thus need to be unstable.&lt;br&gt;
To make a component unstable, make it as independent from other components as possible.&lt;/p&gt;

&lt;p&gt;In "Clean Architecture", a quantitative measure of stability is presented.&lt;br&gt;
If you are interested in this topic, please refer to "Clean Architecture".&lt;/p&gt;

&lt;h2&gt;
  
  
  Stable Abstractions Principle (SAP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A component should be as abstract as it is stable.&lt;br&gt;
(Robert C, Martin. "Component Coupling". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stable Abstractions Principle (SAP) specifies the relationship between stability and abstraction.&lt;br&gt;
In the previous section, we explained that stability can be specified by dependencies.&lt;br&gt;
And SAP says that the level of abstraction must be determined by the level of stability.&lt;br&gt;
So why does a highly stable component also need to be highly abstract?&lt;br&gt;
As an example, let's look at the business domain.&lt;br&gt;
Your business domain is the core element of your business, and you want it to be stable.&lt;br&gt;
Therefore, it should be designed to be dependent on other components to ensure a high degree of stability.&lt;br&gt;
But then, the high stability of the business domain makes it difficult to fix.&lt;br&gt;
This makes the business inflexible, and the architecture becomes a bottleneck for the business.&lt;br&gt;
So, to make it easier to make changes while maintaining a high level of stability, we use a high level of abstraction.&lt;br&gt;
Design the business logic to a high level of abstraction through interfaces and abstract classes, and make other components depend on these.&lt;br&gt;
This way, modifying the business logic within that level of abstraction will not affect other components.&lt;/p&gt;

&lt;h2&gt;
  
  
  SDP + SAP = DIP
&lt;/h2&gt;

&lt;p&gt;Stable Dependency Principle (SDP) says, "Depend on the less stable to the more stable".&lt;br&gt;
Stable Abstractions Principle (SAP) says, "If something is less stable, make it less abstract. If it is more stable, make it more abstract".&lt;br&gt;
Putting these two principles together, we can say, "Depend on the lower level of abstraction to the higher level".&lt;br&gt;
This is exactly what Dependency Inversion Principle (DIP) is.&lt;br&gt;
Of course, DIP is a module-level principle, while SDP and SAP are component-level principles, so strictly speaking, they cannot be put together.&lt;br&gt;
However, through the explanations I have given so far, I think the relationship between these three principles has become clearer.&lt;/p&gt;

&lt;p&gt;NEXT: &lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part4-1h5f"&gt;Introduction to Clean Architecture for TypeScript: PART4&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ads
&lt;/h2&gt;

&lt;p&gt;This article is an excerpt from the first chapter of the book "React Clean Architecture". Some wording has been changed. &lt;a href="https://www.amazon.co.jp/dp/B09FG94392/" rel="noopener noreferrer"&gt;You can buy the kindle version of this book from amazon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" alt="React Clean Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cleanarchitecture</category>
      <category>frontend</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Introduction to Clean Architecture for TypeScript: PART2</title>
      <dc:creator>Imamori Daichi</dc:creator>
      <pubDate>Sun, 12 Sep 2021 02:02:36 +0000</pubDate>
      <link>https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part2-11pi</link>
      <guid>https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part2-11pi</guid>
      <description>&lt;h2&gt;
  
  
  Refs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-frontend-engineer-part1-42mi"&gt;Introduction to Clean Architecture for TypeScript: PART1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part3-aei"&gt;Introduction to Clean Architecture for TypeScript: PART3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part4-1h5f"&gt;Introduction to Clean Architecture for TypeScript: PART4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Design principles
&lt;/h2&gt;

&lt;p&gt;In the book "Clean Architecture", before moving on to the discussion of architecture, there is an explanation of a design principle called SOLID.&lt;br&gt;
SOLID principles are a set of principles that apply at the module level and provide guidance for connections between modules.&lt;br&gt;
Although the design guidelines for the code level, which is the building block of a module, are not covered in this book, "Clean Code" by Uncle Bob or "The Art of Readable Code" may be helpful.&lt;br&gt;
Adherence to the SOLID principles is a key element in achieving Clean Architecture.&lt;br&gt;
In this book, we will also look back at the content of SOLID before discussing Clean Architecture in React applications.&lt;br&gt;
SOLID is an acronym for the following five principles&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single Responsibility Principle (SRP)&lt;/li&gt;
&lt;li&gt;Open-Closed Principle (OCP)&lt;/li&gt;
&lt;li&gt;Liskov Substitution Principle (LSP)&lt;/li&gt;
&lt;li&gt;Interface Segregation Principle (ISP)&lt;/li&gt;
&lt;li&gt;Dependency Inversion Principle (DIP)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Clean Architecture, the SOLID principle is introduced as having the following three objectives&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Be resistant to change.&lt;/li&gt;
&lt;li&gt;Easy to understand.&lt;/li&gt;
&lt;li&gt;Be available for use in many software systems as a basis for components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"Easy to understand" is a characteristic that is also required at the code level.&lt;br&gt;
Then, by connecting modules that consist of well-written and clean code based on SOLID, the relationship between modules becomes easy to understand.&lt;/p&gt;

&lt;p&gt;At first glance, "Be available for use in many software systems" tends to be used in situations where many users will use it, such as libraries and frameworks, but it can also be important in designing specific software.&lt;br&gt;
As your business succeeds and your software grows, you will increasingly want to make certain modules available to other systems within your business.&lt;br&gt;
For example, a service that was initially intended only for the web, but later grew to require native app development.&lt;br&gt;
If you want to use some modules of your software written in React from React Native, you can minimize the modification for reuse if the SOLID is satisfied beforehand.&lt;/p&gt;

&lt;p&gt;And "Be resistant to change" is a key feature in carrying out the objective of protecting the business domain from changes in other components.&lt;br&gt;
In particular, the Dependency Inversion Principle (DIP) plays an important role in protecting the business domain from dependencies.&lt;br&gt;
Let me introduce the five principles of SOLID in turn.&lt;/p&gt;
&lt;h2&gt;
  
  
  Single Responsibility Principle (SRP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A module should have one, and only one, reason to change.&lt;br&gt;
(Robert C, Martin. "SRP: The Single Responsibility Principle". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The term "actors" refers to a grouping of users and stakeholders.&lt;br&gt;
For example, let's take an e-commerce service as an example.The following actors may be considered.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users who shopping&lt;/li&gt;
&lt;li&gt;Users on the store side who manage the products&lt;/li&gt;
&lt;li&gt;Stakeholders who determine the commission to the store side&lt;/li&gt;
&lt;li&gt;The team that decides on the sale launch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For these actors, make sure that one module depends on one actor.&lt;br&gt;
In other words, SRP is the principle that no module should be affected by more than one actor.&lt;br&gt;
The DRY (Don't repeat yourself) principle should also be considered when applying SRP.&lt;br&gt;
I won't go into the details of the DRY principle itself, but in a nutshell, it is the principle that code that does the same thing should be put in one place.&lt;br&gt;
If you try to practice DRY easily, you will end up sharing the same process of doing the same thing.&lt;/p&gt;

&lt;p&gt;If you make processes that have responsibilities for different actors common because they are doing similar things, you may have trouble later on.&lt;br&gt;
This is because even if they seem to be doing the same thing at the moment, as long as the actors they depend on are different, it may be necessary to add different actor-dependent processes in subsequent updates.&lt;br&gt;
In other words, you may end up merging seemingly identical but essentially different processes.&lt;br&gt;
You can distinguish such cases by identifying which actor the process depends on.&lt;br&gt;
Of course, even if the actors are different, the process can be common if there is a relationship between them.&lt;br&gt;
In any case, when practicing DRY, it is necessary to consider the SRP as well and make a careful decision.&lt;/p&gt;
&lt;h2&gt;
  
  
  Open-Closed Principle (OCP)
&lt;/h2&gt;

&lt;p&gt;The Open-Closed Principle (OCP) was proposed by Bertrand Meyer in 1988.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A software artifact should be open for extension but closed for modification.&lt;br&gt;
(Bertrand Meyer. Object Oriented Software Construction, Prentice Hall, 1988, p. 23.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It must be open for extensions such as adding features.&lt;br&gt;
"open" means that it can be added without affecting the existing code.&lt;br&gt;
And when modifying the existing code of a module, the effect of the modification must be closed within the module.&lt;br&gt;
The OCP defines these two rules.&lt;/p&gt;

&lt;p&gt;What are the situations in which this principle is not being followed?&lt;br&gt;
Even if you want to add a trivial feature, you will need to modify the existing code.&lt;br&gt;
When adding a new feature, we carefully read the dependencies among the thousands or tens of thousands of existing lines of code and make modifications.&lt;br&gt;
The larger the code base, the more man-hours it takes to add features, even if it is a trivial feature.&lt;br&gt;
On the other hand, if you follow OCP, you can limit the impact of specification changes and bug fixes because the modifications are closed to the module.&lt;/p&gt;

&lt;p&gt;Being open to extensions and closed to modifications makes the software resistant to feature additions and specification changes.&lt;/p&gt;
&lt;h2&gt;
  
  
  Liskov Substitution Principle (LSP)
&lt;/h2&gt;

&lt;p&gt;The Liskov Substitution Principle (LSP) was proposed by Barbara Liskov in 1988.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is wanted here is something like the following substitution property:&lt;br&gt;
 If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T,&lt;br&gt;
 the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.1&lt;br&gt;
(Barbara Liskov, "Data Abstraction and Hierarchy," SIGPLAN Notices 23, 5 (May 1988).)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, "If S is a derivative of T, then it must behave the same even if T is replaced by S.&lt;br&gt;
Since it is difficult to explain with just words, here is an example code.&lt;br&gt;
This can be expressed in TypeScript as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;o1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;o2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;P&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;P&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, for the sake of illustration, we have assumed that T is a simple type with only value. In fact, what details T has has nothing to do with LSP.&lt;br&gt;
S inherits from T.&lt;br&gt;
The function P takes T as an argument.&lt;br&gt;
In this case, since o2 is type T, so P(o2) is accepted.&lt;br&gt;
Similarly, o1, which is type S, P(o1) is also accepted.&lt;br&gt;
In such a case, we say that S is a derived type of T.&lt;br&gt;
In other words, the LSP says that if you want to inherit a type, it should be a derived type.&lt;/p&gt;

&lt;p&gt;Looking at this example alone, one might think, "What is this, just inheritance of types, or is LSP just a rephrasing of the language feature of inheritance?&lt;br&gt;
To show that this is wrong, here is an example of inheritance that violates the LSP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;calcArea&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;o1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;o2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Function to find the ratio of the sum of the lengths of the sides to the area&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Rectangle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;calcArea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nc"&gt;P&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Can be calculated correctly&lt;/span&gt;
&lt;span class="nc"&gt;P&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5 * 4 / 5 * 5 is correct, but actually (2 * 2 + 5 * 2) / 5 * 5 is calculated&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this codes, we use the Rectangle type to represent a rectangle and the Square type to represent a square as examples.&lt;br&gt;
This is an example of an old known LSP violation.&lt;br&gt;
Mathematically, according to the definition, a square is a special case of a rectangle, so it might seem natural that Square inherits from Rectangle.&lt;br&gt;
However, a square assumes tighter constraints than a rectangle. It is the constraint that the length of the vertical side is equal to the length of the horizontal side.&lt;br&gt;
Therefore, in Square, when setting the length of the edge, the length of the vertical and horizontal edges are processed to be the same.&lt;br&gt;
As a result, P(o1, 2, 5) is getting a different result than intended.&lt;/p&gt;

&lt;p&gt;If there is codes that violates the LSP like this, conditional branching will occur on the client side that uses it (in this case, the function P()).&lt;br&gt;
Then, when there are more classes inheriting from T, it becomes necessary to modify the conditional branch on the client side.&lt;br&gt;
This means that it also violates the Open-Closed Principle (OCP).&lt;/p&gt;
&lt;h2&gt;
  
  
  Interface Segregation Principle (ISP)
&lt;/h2&gt;

&lt;p&gt;The Interface Segregation Principle (ISP) can be summed up: You should not cram everything into a single interface, but &lt;br&gt;
provide only the functionality that the client needs.&lt;/p&gt;

&lt;p&gt;This is also related to SRP.&lt;br&gt;
Consider the following example of an EC site with the following actors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Buyer: The user who purchases the product&lt;/li&gt;
&lt;li&gt;Seller: The user who sells the product&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, these two users are abstracted by a single interface User.&lt;br&gt;
 User requests the methods getPurchaseHistory() and getTotalSales(), but the former method is only used by Buyer, while the latter is only required by Seller.&lt;br&gt;
 At this time, the implementation of Buyer will need to implement some processing, even though it does not use getTotalSales().&lt;br&gt;
 To begin with, Buyer did not need to know about the existence of getTotalSales(), but by violating ISP, it created an unnecessary dependency.&lt;br&gt;
User also violates SRP because it depends on two actors.&lt;br&gt;
 Using or modifying User will require changes to both Buyer and Seller, which may also violate OCP.&lt;/p&gt;

&lt;p&gt;Being careful not to overburden any one interface according to the ISP will also help to protect other principles such as SRP and OCP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;getPurchaseHistory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Purchase&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;// get the purchase history&lt;/span&gt;
  &lt;span class="nl"&gt;getTotalSales&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get total sales&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Buyer&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;getPurchaseHistory&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;history&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Process to retrieve purchase history&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;getTotalSales&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Seller&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;getPurchaseHistory&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;getTotalSales&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sales&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Process to calculate total sales&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sales&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dependency Inversion Principle (DIP)
&lt;/h2&gt;

&lt;p&gt;The last part of SOLD is Dependency Inversion Principle (DIP).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Dependency Inversion Principle (DIP) tells us that the most flexible systems are those in which source code dependencies refer only to abstractions, not to concretions.&lt;br&gt;
(Robert C, Martin. "DIP: The Dependency Inversion Principle". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What is the source code dependencies?&lt;br&gt;
In the case of TypeScript, a dependency is created by referencing import or require.&lt;br&gt;
In other words, the DIP says that when referring to import, you should specify an abstract code such as interface, not a concrete implementation code.&lt;/p&gt;

&lt;p&gt;For example, suppose that a.ts imports b.ts.&lt;br&gt;
At this time, a.ts will know the file name of b.ts as well as the classes and methods described in it.&lt;br&gt;
And if b.ts is changed, it will also affect a.ts.&lt;br&gt;
This is even if a.ts is originally a stable code that is not easily changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  stability of the code
&lt;/h3&gt;

&lt;p&gt;This is where the idea of "stable code" comes into play.&lt;br&gt;
Suppose all the codes could be divided into "stable codes" and "unstable codes".Then, the dependencies can be classified into four types as follows&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;stable → stable&lt;/li&gt;
&lt;li&gt;stable → unstable&lt;/li&gt;
&lt;li&gt;unstable → stable&lt;/li&gt;
&lt;li&gt;unstable → unstable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of the left side of the arrow as being dependent on the right side.&lt;br&gt;
The first is not a problem because both sides are stable.&lt;br&gt;
In 2, the stable code depends on the unstable code, and the inherent stability is compromised.&lt;br&gt;
3 does not cause problems because the unstable code depends on the stable code.&lt;br&gt;
In 4, the unstable code depends on the unstable code, which makes it more unstable, but it is not as much of a problem as 2.&lt;br&gt;
In other words, we need to eliminate the relationship in 2 and reduce the relationship in 4 as much as possible.&lt;/p&gt;

&lt;p&gt;So what exactly are "stable code" and "unstable code"?&lt;br&gt;
In Clean Architecture, we focus on the following two points when considering stability.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;abstract or concrete&lt;/li&gt;
&lt;li&gt;close to input/output or close to domain model&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Abstract or concrete?
&lt;/h3&gt;

&lt;p&gt;Hidden in the DIP is the assumption that abstract components are stable and concrete components are unstable.&lt;br&gt;
In order to verify whether this assumption is correct, let's compare the abstract interface with its concrete implementation.&lt;br&gt;
As the interface is modified, the implementation is modified accordingly.&lt;br&gt;
On the other hand, the interface does not necessarily need to be changed when the implementation is changed.&lt;br&gt;
This shows that the abstracted code is more stable.&lt;br&gt;
It can be said that abstraction is the process of extracting the common denominator or essence from multiple concrete objects. Therefore, it is easy to understand that abstracted code is hard to be changed.&lt;br&gt;
However, it is important to note that implementation is not always easy to change.&lt;br&gt;
For example, the standard library of a programming language is an implementation, but it does not change much.&lt;br&gt;
There is no need to be more nervous than necessary about references to such an unchanging implementation.&lt;/p&gt;

&lt;p&gt;Thus, abstract or concrete is an important indicator for the stability of a code.&lt;br&gt;
On the other hand, concrete codes are not necessarily changeable.&lt;br&gt;
In addition, please keep in mind that not all codes can be divided into stable or unstable, and that there are varying degrees of stability there.&lt;/p&gt;

&lt;h4&gt;
  
  
  Close to input/output or close to domain model
&lt;/h4&gt;

&lt;p&gt;As briefly introduced in the first image of this article, Clean Architecture divides the software into layers of concentric circles.&lt;br&gt;
The UI View, DB, storage and other inputs and outputs are located on the outermost side, and the domain model is located on the innermost side.&lt;br&gt;
The intention is to place what we want to stabilize inside the layer and protect it from the unstable outside.&lt;br&gt;
We also use DIP to protect the center of the circle, which we want to stabilize.&lt;br&gt;
This concentric circle architecture is the fundamental idea of Clean Architecture.&lt;br&gt;
This is supported by DIP.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F13rxa5txo9ambwcicv9d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F13rxa5txo9ambwcicv9d.png" alt="stable-unstable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependency Inversion
&lt;/h3&gt;

&lt;p&gt;According to the DIP, the destination referred to by import must be stable.&lt;br&gt;
But can we always follow this rule?&lt;br&gt;
There are many cases where you want to call unstable code from stable code for processing.&lt;br&gt;
For example, the Clean Architecture considers DB to be the outermost of the concentric circles and unstable.&lt;br&gt;
If you want to operate DB from a stable code inside the circle, you need to operate an unstable code from a stable code.&lt;br&gt;
As you can see, the flow of processing and the direction of dependencies are usually the same. In other words, if you try to manipulate the DB, you will depend on the DB.&lt;br&gt;
However, this would violate the DIP. We need to reverse the process flow and dependencies somehow.&lt;br&gt;
Dependency Injection (DI) solves this problem.&lt;br&gt;
For more information about DI, please refer to the original book for a detailed explanation with sample codes.&lt;br&gt;
DI reverses the flow of processing and dependencies, and protects the domain model at the center of the concentric circles from instability. &lt;/p&gt;

&lt;p&gt;NEXT: &lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part3-aei"&gt;Introduction to Clean Architecture for TypeScript: PART3&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ads
&lt;/h2&gt;

&lt;p&gt;This article is an excerpt from the first chapter of the book "React Clean Architecture". Some wording has been changed. &lt;a href="https://www.amazon.co.jp/dp/B09FG94392/" rel="noopener noreferrer"&gt;You can buy the kindle version of this book from amazon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" alt="React Clean Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cleanarchitecture</category>
      <category>frontend</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Introduction to Clean Architecture for TypeScript: PART1</title>
      <dc:creator>Imamori Daichi</dc:creator>
      <pubDate>Sun, 12 Sep 2021 01:44:53 +0000</pubDate>
      <link>https://dev.to/aidy1991/introduction-to-clean-architecture-for-frontend-engineer-part1-42mi</link>
      <guid>https://dev.to/aidy1991/introduction-to-clean-architecture-for-frontend-engineer-part1-42mi</guid>
      <description>&lt;h2&gt;
  
  
  Refs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part2-11pi"&gt;Introduction to Clean Architecture for TypeScript: PART2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part3-aei"&gt;Introduction to Clean Architecture for TypeScript: PART3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part4-1h5f"&gt;Introduction to Clean Architecture for TypeScript: PART4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Clean Architecture?
&lt;/h2&gt;

&lt;p&gt;Clean Architecture is a software design methodology proposed by Robert C. Martin.&lt;br&gt;
Looking back at its history, the Clean Architecture methodology was introduced by a blog post on August 13, 2012.&lt;br&gt;
An English version of the book was published in 2017.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzk4nhwuwsrjbj349czec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzk4nhwuwsrjbj349czec.png"&gt;&lt;/a&gt;&lt;br&gt;Robert C, Martin. "The Clean Architecture". Clean Architecture. Pearson Education.
  &lt;/p&gt;

&lt;p&gt;Clean Architecture is a design methodology derived from Robert C. Martin's experience in various software (or hardware) developments.&lt;br&gt;
The image is often used to represent Clean Architecture, and even if you don't know much about Clean Architecture, you've probably seen this diagram before.&lt;br&gt;
The details of this diagram will be described later, but I introduced it at the beginning because it is an important diagram in explaining the Clean Architecture.&lt;br&gt;
I hope you will keep this concentric circle diagram in mind as you continue reading.&lt;br&gt;
The following sentence is from the preface of Clean Architecture.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The architecture rules are the same!&lt;/strong&gt;&lt;br&gt;
This is startling because the systems that I have built have all been so radically different.&lt;br&gt;
 Why should such different systems all share similar rules of architecture?&lt;br&gt;
 My conclusion is that the rules of software architecture are independent of every other variable.&lt;br&gt;
(Robert C, Martin. "Preface". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These common rules for software development are summarized as practices and are called Clean Architecture.&lt;br&gt;
In other words, Clean Architecture was born as a common rule by highly abstracting the experience gained from various developments.&lt;/p&gt;

&lt;p&gt;Of course, extracting best practices from experience and compiling them into rules has been done before Clean Architecture.&lt;br&gt;
The history of programming language paradigms is just that, and so are programming patterns such as the GoF design pattern.&lt;br&gt;
There is also a collection of design principles called SOLID, compiled by Uncle Bob himself.&lt;/p&gt;

&lt;p&gt;In "Clean Architecture", Sections I through IV will be used to explain these traditional practices.&lt;br&gt;
In this way, Clean Architecture is a design methodology that draws on existing practices.&lt;br&gt;
Therefore, Clean Architecture is not a design methodology that has been released in recent years, but rather it is based on empirical rules that have been developed by many software developers over the years.&lt;br&gt;
On the other hand, there are some things to keep in mind.As mentioned earlier, Clean Architecture is a highly abstracted method.&lt;/p&gt;

&lt;p&gt;Since it does not propose specific implementation methods, it cannot be directly applied to actual software development.&lt;br&gt;
Therefore, it is necessary to understand the meaning of the abstraction of this design methodology and to put it into the concrete work of actual software development.&lt;br&gt;
In some cases, you may want to apply some of the rules in Clean Architecture and ignore others.&lt;br&gt;
However, even if you dare to ignore the rule, there is a big difference between knowing the rule and ignoring it, and breaking the rule without knowing it.&lt;br&gt;
In these articles, we will grasp the principles of Clean Architecture and then discuss how to apply Clean Architecture when building React applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Clean Architecture?
&lt;/h2&gt;

&lt;p&gt;In "Clean Architecture", the world realized by good software design is described as follows&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The goal of software architecture is to minimize the human resources required to build and maintain the required system.&lt;br&gt;
(Robert C, Martin. "What Is Design and Architecture?". Clean Architecture. Pearson Education.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Leaving aside the question of how feasible this goal is, few developers would deny the goal itself.&lt;br&gt;
Clean Architecture has been proposed as one of the means to achieve this goal.&lt;br&gt;
As I said it is a means to an end, you can also aim for this goal by means other than Clean Architecture.&lt;br&gt;
Of course, not everything will work with Clean Architecture.&lt;br&gt;
The specific implementation is not within the scope of Clean Architecture, and adopting it does not immediately solve anything.&lt;br&gt;
What Clean Architecture provides is the thinking and practices to achieve the above goals.&lt;br&gt;
There is no solid right answer for the implementation to realize it, and there are as many right answers as there are products.&lt;/p&gt;

&lt;p&gt;Learn about the background and concept of Clean Architecture, and we believe that cultivating the ability to think about how to apply it to specific implementations is necessary to achieve a step-change in software design.&lt;br&gt;
In particular, it can be applied to React applications to empower not only web development but also application development on multiple platforms and it will support product growth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clean Architecture and Domain Driven Design
&lt;/h2&gt;

&lt;p&gt;Domain Driven Design (DDD) is one of the building blocks of Clean Architecture.&lt;br&gt;
DDD is a software design methodology proposed by Eric Evans.&lt;br&gt;
DDD focuses on how to link business domains and software development, and does not refer to individual technologies.&lt;br&gt;
What is important in DDD is that domain experts and software developers, who are familiar with the business domain, can communicate with each other through a common language, the ubiquitous language.&lt;br&gt;
The point is that we shape our services through communication.&lt;br&gt;
Clean Architecture is based on the concept of DDD. Domain models and domain logic, which are formed through communication, are the core and most important elements of a service.&lt;br&gt;
Clean Architecture focuses on how to protect them from surrounding dependencies.&lt;/p&gt;

&lt;p&gt;In this way, Clean Architecture is closely related to DDD and cannot be separated from it.&lt;br&gt;
In the book "Clean Architecture", terms such as domain, domain model, and other terms used in DDD such as Entity and Repository also appear.&lt;/p&gt;

&lt;p&gt;NEXT: &lt;a href="https://dev.to/aidy1991/introduction-to-clean-architecture-for-typescript-part2-11pi"&gt;Introduction to Clean Architecture for TypeScript: PART2&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ads
&lt;/h2&gt;

&lt;p&gt;This article is an excerpt from the first chapter of the book "React Clean Architecture". Some wording has been changed. &lt;a href="https://www.amazon.co.jp/dp/B09FG94392/" rel="noopener noreferrer"&gt;You can buy the kindle version of this book from amazon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv8e3y7l0mzlvooan7c5.png" alt="React Clean Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>cleanarchitecture</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
