<?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>Forem: Raphael Ben Hamo</title>
    <description>The latest articles on Forem by Raphael Ben Hamo (@raphaelartlist).</description>
    <link>https://forem.com/raphaelartlist</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%2F782986%2Fd80b96c2-6b7e-4c9d-ba17-f79d2d855d56.jpeg</url>
      <title>Forem: Raphael Ben Hamo</title>
      <link>https://forem.com/raphaelartlist</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/raphaelartlist"/>
    <language>en</language>
    <item>
      <title>Rings and Onion in Your Next Node Application</title>
      <dc:creator>Raphael Ben Hamo</dc:creator>
      <pubDate>Sun, 07 Aug 2022 13:40:36 +0000</pubDate>
      <link>https://forem.com/artlist/rings-and-onion-in-your-next-node-application-5aga</link>
      <guid>https://forem.com/artlist/rings-and-onion-in-your-next-node-application-5aga</guid>
      <description>&lt;p&gt;Sometimes we need to create a new service or refactor the existing one. Most of the time, we spend a couple of hours initializing the application - Node application setup, creating files, folders, etc. The way we structure the folders’ skeleton can be something similar to other services in our repos or based on a service we have done in the past.&lt;/p&gt;

&lt;p&gt;This moment is highly important! If we stop and take the time to plan before starting this process, it will be worth it in the long run.&lt;/p&gt;

&lt;p&gt;In this article, we will do it by creating the folders’ skeleton using the Clean Architecture approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Why software architecture is so important&lt;/li&gt;
&lt;li&gt;About Clean Architecture&lt;/li&gt;
&lt;li&gt;Node API Service with Clean Architecture&lt;/li&gt;
&lt;li&gt;Folders skeleton&lt;/li&gt;
&lt;li&gt;Summary &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Software Architecture Is so Important
&lt;/h2&gt;

&lt;p&gt;In a large project, we want it to be easy to maintain, stable (not quick &amp;amp; dirty), and open to new features as quickly as possible.&lt;/p&gt;

&lt;p&gt;To achieve that, we need to separate our files and folders into components with different responsibilities that can change independently without affecting other components.&lt;/p&gt;

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

&lt;p&gt;In short, Clean Architecture is a system architecture guideline proposed by Robert C. Martin (Uncle Bob). &lt;br&gt;
You can read about it &lt;a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://betterprogramming.pub/the-clean-architecture-beginners-guide-e4b7058c1165" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://www.techtarget.com/whatis/definition/clean-architecture" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The main concept can be shown by Robert Martin’s illustration:&lt;br&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%2F0dycqznshrbupyi0tyb9.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%2F0dycqznshrbupyi0tyb9.png" alt="Image by Robert C. Martin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each circle represents a different area in our software. According to the dependency rule, the outer layer can depend on inner layers but not on any layer outer from it, which means that as a general rule, the deeper the layer, the less it is prone to changes.&lt;br&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%2Fpt3tz5kimxt2q4d23n4o.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%2Fpt3tz5kimxt2q4d23n4o.png" alt="Image by Robert C. Martin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since this is a little bit abstract, let us demonstrate what it looks like in Node API Service.&lt;/p&gt;

&lt;h2&gt;
  
  
  Node API Service with Clean Architecture
&lt;/h2&gt;

&lt;p&gt;We will start creating a Node service that has an API and Database. &lt;br&gt;
In Clean Architecture, we will have these layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Domain layer&lt;/strong&gt; - the abstract layer, responsible for all of our business data, our business functions and entities, but abstracted - interfaces and abstract classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Application layer&lt;/strong&gt; - the logic layer, each file here is responsible for a flow or use case in our project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Adapter layer&lt;/strong&gt; - the API presentation layer, which includes routes, controllers, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Infrastructure layer&lt;/strong&gt; - database configurations, implementations of the entities, providers, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2Fyod4lwhiumvysyuh49h2.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%2Fyod4lwhiumvysyuh49h2.png" alt="Image from TechTarget"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Folders skeleton
&lt;/h2&gt;

&lt;p&gt;The layers break down into these folders: domain, use cases, API, and infrastructure.&lt;br&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%2F13y4vqet09hfl77i8p09.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%2F13y4vqet09hfl77i8p09.png" alt="Screenshot by author"&gt;&lt;/a&gt;&lt;br&gt;
In the Artlist world, it can be implemented as a service for managing artists and songs.&lt;/p&gt;

&lt;p&gt;First, let us see what it looks like for each folder:&lt;br&gt;
&lt;strong&gt;domain&lt;/strong&gt; - contains files such as enums, interfaces, models, etc.&lt;br&gt;
In Artlist, it would be the place for artist and song properties and abstract classes of the logic for each flow.&lt;br&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%2Fndd6j5j0savfkovt9q5g.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%2Fndd6j5j0savfkovt9q5g.png" alt="Screenshot by author"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;usecases&lt;/strong&gt; -  contains files of the project logic, flow, and use cases.&lt;br&gt;
Each file can present a separate flow, like "download song use-case" or all song use-cases.&lt;br&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%2Fz1dc9gx6w15mue8tjiss.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%2Fz1dc9gx6w15mue8tjiss.png" alt="Screenshot by author"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;api&lt;/strong&gt; - contains files such as controllers, interceptors, guards, etc.&lt;br&gt;
For Artlist, this is the API for the client side to get the artist and song data. &lt;br&gt;
Here we can call from the controller 'a to use case 'a.x' and use case 'a.y'. If we do so, we make a dependency from the &lt;strong&gt;adapter layer&lt;/strong&gt; to the &lt;strong&gt;application layer&lt;/strong&gt;. That is ok because the dependency is from the outer layer to a deeper layer.&lt;br&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%2F9taa277wgf3eajvigicw.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%2F9taa277wgf3eajvigicw.png" alt="Screenshot by author"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;infrastructure&lt;/strong&gt; - contains database configurations - table entity implementations, external providers, DAL repositories, etc.&lt;br&gt;
Here we can implement the abstract classes declared in the domain layer - also, the database entities and ORM.&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%2Frg382bcd4cqofsxuw5pc.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%2Frg382bcd4cqofsxuw5pc.png" alt="Screenshot by author"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have a basic folder structure for Node API service using Clean Architecture.&lt;/p&gt;

&lt;p&gt;You can save it as a template repo and start other services from it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we learned the basics of Clean Architecture. First, we translated it to the Node world, then demonstrated how to build a project skeleton for Node API service using Clean Architecture and finally showed how it can be implemented in the Artlist world.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>node</category>
      <category>architecture</category>
      <category>backend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
