<?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: Nicolas Lapointe</title>
    <description>The latest articles on Forem by Nicolas Lapointe (@nlapointe).</description>
    <link>https://forem.com/nlapointe</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%2F1407866%2F4a1d1848-8f05-4764-bdc9-e1b454667dfb.png</url>
      <title>Forem: Nicolas Lapointe</title>
      <link>https://forem.com/nlapointe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nlapointe"/>
    <language>en</language>
    <item>
      <title>Introduction to the principles of clean architecture in a NodeJs API (Express)</title>
      <dc:creator>Nicolas Lapointe</dc:creator>
      <pubDate>Tue, 21 May 2024 08:17:46 +0000</pubDate>
      <link>https://forem.com/nlapointe/introduction-to-the-principles-of-clean-architecture-in-a-nodejs-api-express-13e9</link>
      <guid>https://forem.com/nlapointe/introduction-to-the-principles-of-clean-architecture-in-a-nodejs-api-express-13e9</guid>
      <description>&lt;p&gt;This is the fourth article in our series on clean architecture (the previous ones are listed at the bottom of the page), and we're getting more technical with an introduction to implementing the principles in a Node.JS API with Express.&lt;/p&gt;

&lt;p&gt;Let's take a look at the overall structure of a project and the details of the main concepts (entities, adapters, injection...).&lt;/p&gt;

&lt;h2&gt;
  
  
  Organization of main directories
&lt;/h2&gt;

&lt;p&gt;Let's start by taking a look at the general directory structure. As is often the case, all our API source code is grouped together in the "/src" directory. Next, let's take a look at the &lt;a href="https://dev.to/nlapointe/clean-architecture-infrastructure-vs-core-492k"&gt;Core vs. Infrastructure&lt;/a&gt; distinction:&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%2Fiwanr9gnjig93zr2lnjh.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%2Fiwanr9gnjig93zr2lnjh.png" alt="Structure tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The heart of the application in "/core"
&lt;/h2&gt;

&lt;p&gt;This is where use cases, business entities and business rules reside. Not dependent on any external framework or library, it represents the bare minimum needed to run our application: it's the &lt;strong&gt;functional core&lt;/strong&gt; of our application. It is itself divided into several sub-directories.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use cases
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;In "/core/use-cases", we find the business use cases&lt;/strong&gt;. Each use case has its own typescript file with, if required, its own input and output types (what goes into the use case and what comes out). There are two schools of thought:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A sub-folder with a grouping of use cases. Example: A user subfolder in which I group all user cases.&lt;/li&gt;
&lt;li&gt;All use cases at the root of use-cases, with no particular tree structure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I personally prefer the second solution, all "flat", for the simple reason that you never have to ask yourself the question "where do I put / where has this use case been put?". A simple example: do you put a user's address in "user" or "address"? There are as many answers as there are developers, so you might as well not put a sub-folder!&lt;/p&gt;

&lt;p&gt;An example of a use case that allows you to obtain a book by its id:&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;GetBook&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;bookRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BookRepository&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;logger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Logger&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bookRepository&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="nx"&gt;resolve&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BookRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BookRepository&lt;/span&gt;&lt;span class="dl"&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;logger&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="nx"&gt;resolve&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Logger&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;async&lt;/span&gt; &lt;span class="nf"&gt;execute&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Book&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BOOK_NOT_FOUND&lt;/span&gt;&lt;span class="dl"&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Get-book usecase] Start&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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;bookRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BOOK_NOT_FOUND&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;GetBook&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Entities
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;"/core/entities" directory contains our business data models&lt;/strong&gt;, such as "User" or "Product". These are simple objects that represent the concepts of our domain, and bring together their business rules.&lt;/p&gt;

&lt;p&gt;Let's take the example of our "User", and more precisely of a user who is not logged in, and therefore not known to the API:&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotExistingUser&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;hashPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notHashedPassword&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hmac&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createHmac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sha512&lt;/span&gt;&lt;span class="dl"&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;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notHashedPassword&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, we have a class that extends User and thus retrieves its properties, and a method that belongs to it, that is, "its" business rule: You hash a password (to create your account or connect to it).&lt;/p&gt;

&lt;h3&gt;
  
  
  Ports
&lt;/h3&gt;

&lt;p&gt;These are simple interfaces linking the core and the infrastructure. They serve as a contract without any concrete implementation. You may find ports for technical dependencies, such as a logger, or for repositories (databases, etc.). An example of a User :&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;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;findByEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&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;&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;create&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This contract includes two methods, each with input and output parameters. The code part therefore knows what it will receive from the infrastructure, and vice versa.&lt;/p&gt;

&lt;h2&gt;
  
  
  Technical details in "/infrastructure"
&lt;/h2&gt;

&lt;p&gt;At the same level as core is the infrastructure folder, which manages all the technical details such as data persistence and calls to external services. This is where the interfaces defined in core are actually implemented.&lt;/p&gt;

&lt;h3&gt;
  
  
  The API
&lt;/h3&gt;

&lt;p&gt;Let's start with the API, the folder in which we'll put Express' config, controllers and other middleware.&lt;/p&gt;

&lt;p&gt;In detail, for controllers, the "/infrastructure/api/controllers" folder groups &lt;strong&gt;controllers by resource or use case&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Their role here is to retrieve data from the HTTP request, validate and map it to the inputs expected by the use case, execute the use case and finally format the response. We'll therefore find sub-folders for each resource, with a typescript file for the controller itself, the input and output DTOs, and the encoder/decoder for these inputs/outputs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adapters
&lt;/h3&gt;

&lt;p&gt;As the name suggests, this directory contains the technical adapters for the ports defined on the core side. It's very important to identify implementation dependencies in the tree structure. Let's take our example of the User repository:&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;// /core/ports/user-repository.port.ts&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;findByEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&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;&amp;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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// /infrastructure/adapters/mongo/user.repository.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;UserRepository&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;../../core/ports/user-repository.port.ts&lt;/span&gt;&lt;span class="dl"&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;MongoUserRepository&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;findByEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&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;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// logique d'accès MongoDB&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;async&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// logique d'accès MongoDB&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;At tree level, we find "adapters/&lt;strong&gt;mongo&lt;/strong&gt;", which means that in this sub-directory we have all the technical implementation for accessing mongo DB. As you can see, the adapter takes over the contract specified in the port. If tomorrow I want to change my dependency to SQLite, for example, I'll simply create a second adapter, change the dependency injection, and there'll be no impact on the core.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This structure clearly distributes the various responsibilities of a Node.js API. The core has no external dependencies, ports are decoupled from business logic and implementation details are delegated to the infrastructure layer.&lt;/p&gt;

&lt;p&gt;By following these Clean Architecture principles, your code gains in maintainability, testability and scalability. Although the example is given with TypeScript and Express code, this organization can easily be adapted to other Node.js frameworks or even any other language.&lt;br&gt;
In the end, that's the whole point of clean architecture: no language or framework, but a return to basics, to common sense, and contrary to popular misconception, a return to simplicity!&lt;/p&gt;

&lt;p&gt;Frequently Asked Questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What are the advantages of following Clean Architecture principles in an Express project?&lt;/strong&gt; &lt;br&gt;
The disadvantage of Express (or advantage, depending on your point of view!) is that it's an empty shell. So you can do absolutely anything you want, and also anything at all. Clean architecture provides a framework for the whole team.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;If I'm doing clean architecture, do I have to follow this structure to the letter?&lt;/strong&gt; &lt;br&gt;
No, this structure is just one example. The most important thing is to respect the fundamental principles of Clean Architecture: separation of concerns, decoupling of technical details, external dependencies, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;How are tests structured in this architecture?&lt;/strong&gt; &lt;br&gt;
We haven't put it in this article, but use case unit tests can be put at the same level as each other, in /core/use-cases. Personally, I prefer to have a tests folder at the root, which is a convention we often see.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;This article is an introduction to Clean Architecture and is part of a dedicated series on this topic. Stay tuned for more!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Want to learn how implement it with typescript and express? See my udemy course! In &lt;a href="https://www.udemy.com/course/clean-architecture-avec-typescript-et-express/?referralCode=8C9C5BF69F624247D63A" rel="noopener noreferrer"&gt;french&lt;/a&gt; or &lt;a href="https://www.udemy.com/course/clean-architecture-with-typescript-and-nodejs-express/?referralCode=A0683A3DE7EDE1BD15A0" rel="noopener noreferrer"&gt;english&lt;/a&gt; 😉&lt;/p&gt;

&lt;p&gt;Articles on Clean Architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/demystifying-clean-architecture-fundamentals-and-benefits-5ae7"&gt;Demystifying Clean Architecture: Fundamentals and Benefits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/clean-architecture-infrastructure-vs-core-492k"&gt;Clean Architecture : Infrastructure vs Core&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/clean-architecture-business-rules-first-4dlo"&gt;Clean Architecture: Business rules first!&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>cleancode</category>
      <category>cleanarchitecture</category>
    </item>
    <item>
      <title>Clean Architecture: Business rules first!</title>
      <dc:creator>Nicolas Lapointe</dc:creator>
      <pubDate>Mon, 13 May 2024 10:32:14 +0000</pubDate>
      <link>https://forem.com/nlapointe/clean-architecture-business-rules-first-4dlo</link>
      <guid>https://forem.com/nlapointe/clean-architecture-business-rules-first-4dlo</guid>
      <description>&lt;p&gt;With this third article on Clean Architecture, let's go into a little more detail! The &lt;a href="https://dev.to/nlapointe/demystifying-clean-architecture-fundamentals-and-benefits-5ae7"&gt;first article&lt;/a&gt; gave an overview of Clean Architecture, followed by &lt;a href="https://dev.to/nlapointe/clean-architecture-infrastructure-vs-core-492k"&gt;a second&lt;/a&gt; on the separation between Core (business code) and Infrastructure (technical code). In this article, we're going to look at the Core part, and therefore the business rules, its central role in design and so as an essential (main!) element of Clean Architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Obvious application behavior!
&lt;/h2&gt;

&lt;p&gt;It seems obvious to say it: business rules and features are what govern the behavior of an application, and therefore its design. And yet... Many applications find themselves constrained by the technical aspect that takes precedence, or prevents a rule from evolving.&lt;/p&gt;

&lt;p&gt;And that's exactly why I like Clean Architecture: we're simply putting back the basics, which existed years ago. It recognizes the value of business rules, and brings them to the fore. They're not just lines of code, but the pillars on which the design structure rests.&lt;/p&gt;

&lt;p&gt;Let's delve into the foundations and examine how it ensures that business rules come first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Business rules: the foundation of the application
&lt;/h3&gt;

&lt;p&gt;Business rules represent the very essence of what an application does. They define how data is to be manipulated, what actions can be performed and under what circumstances. In short, they are the rules of the game that dictate the application's behavior in all possible situations.&lt;/p&gt;

&lt;p&gt;And they must directly influence the way an application is designed and implemented. Quite simply, they must guide the architectural and structural decisions made throughout the development process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Entities: bearers of business rules
&lt;/h3&gt;

&lt;p&gt;At the heart of Clean Architecture are a set of entities, the main actors that embody the application's business rules from a code standpoint. They encapsulate data and essential business logic. If we take our theme from the play in the previous article, the entities would be the protagonists, carrying the weight of decisions and actions on their shoulders.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Role of entities:&lt;/strong&gt; Entities represent the fundamental objects of the business domain, such as users, products or orders. They capture the business rules specific to these objects, and ensure that they are respected at all times at this single point in the code base.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Entity independence:&lt;/strong&gt; Entities maintain strict independence from technical details. They remain focused on their core responsibility: faithfully representing the business domain, that's all! This independence allows entities to evolve independently of the underlying infrastructure, guaranteeing their reusability and extensibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take a look at an example of Typescript code for a :&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExistingUser&lt;/span&gt; &lt;span class="kd"&gt;extends&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;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="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;ExistingUserConstructorArgs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&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;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;signAndEncodeUserAccessToken&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;accessToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;sub&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="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;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;expiresIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;86400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 24 hours&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;id&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;_id&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;This entity, which represents an application user, includes a data element (id) and a method or business rule (access token generation).&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases: Executors of business rules
&lt;/h3&gt;

&lt;p&gt;Just as entities represent the central objects of the business domain, use cases direct the execution of rules by coordinating the interactions between the various players in the application - a master of ceremonies in itself!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Role of Use Cases:&lt;/strong&gt; Use cases act as script writers, defining the actions users can perform and the responses the application must provide in return. They translate business requirements into concrete actions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coordination with Entities:&lt;/strong&gt; In executing use cases, entities provide the data required to meet the specific business needs defined by each use case. In our theater, this is the coordination between our protagonists and the texts of the play's overall scenario, our application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at another example from the code side, for a Use Case :&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;async&lt;/span&gt; &lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;login&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;password&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;accessToken&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;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;USER_NOT_FOUND&lt;/span&gt;&lt;span class="dl"&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Get-user usecase] Start&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;notExistingUser&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;NotExistingUser&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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;userRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByLoginAndPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;login&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;notExistingUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hashPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;password&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="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="na"&gt;accessToken&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;signAndEncodeUserAccessToken&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;USER_NOT_FOUND&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's an execution method, which represents our scenario. What does this scenario do? It will use a method to retrieve a user from the database, and user entities to use business methods, such as the generation of an access token seen above.&lt;/p&gt;

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

&lt;p&gt;Clean Architecture recognizes the importance of business rules in application design. It puts these rules at the forefront of design, and ensures that every development choice is aligned with the application's business needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modularity and scalability of Entities and Use Cases
&lt;/h3&gt;

&lt;p&gt;The application's entire business logic is based on entities and use cases. Designed to be modular and scalable, they provide a solid basis for development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Entities and use cases are designed to be easily modifiable and extensible. This modularity means that new functionalities can be added or existing ones modified without compromising the integrity of the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; Entities and use cases are also designed to evolve with the changing needs of the application. For example, the addition of a new business rule can be easily integrated without disrupting existing parts of the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Robustness and scalability
&lt;/h3&gt;

&lt;p&gt;This modular, scalable design of entities and use cases naturally leads to greater robustness and scalability of the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Robustness:&lt;/strong&gt; Because business logic is isolated from the rest of the application, Clean Architecture ensures that business rules are applied consistently and reliably. This reduces the risk of errors and bugs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; Modularity and scalability, and therefore adaptability to future growth in functionality or other developments. Clean Architecture provides a solid foundation on which to build and extend the application over time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Maintaining alignment with business needs
&lt;/h3&gt;

&lt;p&gt;By always keeping an eye on business needs, Clean Architecture ensures that application development remains aligned with them. This approach ensures that every feature developed responds directly to a specific business need, guaranteeing the application's relevance and added value.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article is an introduction to Clean Architecture and is part of a dedicated series on this topic. Stay tuned for more!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Want to learn how implement it with typescript and express? See my udemy course! In &lt;a href="https://www.udemy.com/course/clean-architecture-avec-typescript-et-express/?referralCode=8C9C5BF69F624247D63A"&gt;french&lt;/a&gt; or &lt;a href="https://www.udemy.com/course/clean-architecture-with-typescript-and-nodejs-express/?referralCode=A0683A3DE7EDE1BD15A0"&gt;english&lt;/a&gt; 😉&lt;/p&gt;

&lt;p&gt;Articles on Clean Architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/demystifying-clean-architecture-fundamentals-and-benefits-5ae7"&gt;Demystifying Clean Architecture: Fundamentals and Benefits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/clean-architecture-infrastructure-vs-core-492k"&gt;Clean Architecture : Infrastructure vs Core&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/introduction-to-the-principles-of-clean-architecture-in-a-nodejs-api-express-13e9"&gt;Introduction to the principles of clean architecture in a NodeJs API (Express)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>development</category>
      <category>webdev</category>
      <category>programming</category>
      <category>cleancoding</category>
    </item>
    <item>
      <title>Clean Architecture : Infrastructure vs Core</title>
      <dc:creator>Nicolas Lapointe</dc:creator>
      <pubDate>Mon, 06 May 2024 08:54:35 +0000</pubDate>
      <link>https://forem.com/nlapointe/clean-architecture-infrastructure-vs-core-492k</link>
      <guid>https://forem.com/nlapointe/clean-architecture-infrastructure-vs-core-492k</guid>
      <description>&lt;p&gt;This article follows on from a more comprehensive presentation of Clean Architecture, which you can find &lt;a href="https://dev.to/nlapointe/demystifying-clean-architecture-fundamentals-and-benefits-5ae7"&gt;here&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Imagine your application as a complex story between two essential protagonists: the Business Domain (Core) and the Technical Domain (Infrastructure). Their clear separation is the basis of Clean Architecture. Let's discover how this division, like a well-orchestrated score, optimizes the maintainability, scalability and clarity of your code.&lt;/p&gt;

&lt;p&gt;In the complex world of application development, Clean Architecture stands out for its ability to clearly separate the business domain (Core) from the technical domain (Infrastructure). In this article, we'll explore in detail the separation between Core and Infrastructure that ensures better management of software complexities, as well as the importance of use cases as drivers of business rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the separation: Core vs Infrastructure
&lt;/h2&gt;

&lt;p&gt;Before delving into the details, it's essential to understand the nature of this separation. Core represents the very essence of the application, where business rules and key concepts reside. Infrastructure, on the other hand, encompasses the technical details, such as database access or the management of external dependencies. This clear distinction ensures efficient management of the different aspects of your application: technical constraints are not mixed up with business rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Business Domain (Core)
&lt;/h2&gt;

&lt;p&gt;The Core is the rampart of business logic, where entities and use cases reside. Entities represent the key objects of the domain (the “core elements”, what we “present” to the business), while use cases direct the execution of business rules. This layer is the heart of the application, guaranteeing consistency and clarity of functionality.&lt;/p&gt;

&lt;p&gt;The core can be divided into 2 layers: entities and use cases. Let's start with the first layer in the Core, Entities.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nature of entities
&lt;/h3&gt;

&lt;p&gt;Entities represent the fundamental concepts of the business domain. They encapsulate essential data and business rules, ensuring the consistency and integrity of information. Think of them as the main actors in a play, each with a defined role to play in the story.&lt;/p&gt;

&lt;p&gt;For example, in an order management application, an entity might represent an order or a product.&lt;/p&gt;

&lt;p&gt;Entities maintain strict independence from technical details, guaranteeing their reusability and scalability. This clear separation enables developers to concentrate fully on business logic without being distracted by technical considerations.&lt;/p&gt;

&lt;p&gt;Compare this to the separation between the script of a play (Core) and the sets (Infrastructure) - the show can evolve without altering the main plot.&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%2Fwo38agcn12ihe4daav5q.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%2Fwo38agcn12ihe4daav5q.png" alt="Illustration"&gt;&lt;/a&gt;&lt;em&gt;Image from &lt;a href="https://fr.freepik.com/photos-gratuite/etudiants-se-preparent-pour-cours-theatre_83059163.htm#fromView=search&amp;amp;page=1&amp;amp;position=8&amp;amp;uuid=beca9285-9bb5-4cff-b5e2-e8bfa120a2b7" rel="noopener noreferrer"&gt;Freepik&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Role of Use Cases
&lt;/h3&gt;

&lt;p&gt;Use cases describe the application's functionality from the user's point of view. They coordinate the actions performed by entities to meet the application's functional requirements.&lt;/p&gt;

&lt;p&gt;For example, a use case might be “Create a new order” or “Modify product details”.&lt;/p&gt;

&lt;p&gt;Use cases also maintain a clear separation from Infrastructure, focusing solely on business logic. This approach enables agile scalability, where functionality can be added or modified without disrupting the application's structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Technical Domain (Infrastructure)
&lt;/h2&gt;

&lt;p&gt;Conversely, the Infrastructure is the technical backdrop to your application. It manages everything behind the scenes, from databases to external frameworks, ensuring that the Core remains isolated from the technical details. It will never encroach on business logic. It's like a discreet orchestrator, ensuring that technical details never compromise the clarity and simplicity of the core scenario.&lt;/p&gt;

&lt;p&gt;Think of this layer as the production team in the movie world, ensuring that the show runs smoothly while remaining invisible to the viewer.&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%2Fclqmuero1oc0b026ncp0.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%2Fclqmuero1oc0b026ncp0.png" alt="Illustration image"&gt;&lt;/a&gt;&lt;em&gt;Image de &lt;a href="https://fr.freepik.com/photos-gratuite/beaucoup-systemes-eclairage-led-peu-filtres-couleur-escaliers-dans-decor-cinema_11175402.htm#fromView=search&amp;amp;page=1&amp;amp;position=3&amp;amp;uuid=ad99132e-3cea-4bb3-8649-0397ef9494e0" rel="noopener noreferrer"&gt;frimufilms&lt;/a&gt; sur Freepik&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Technical Details Management
&lt;/h3&gt;

&lt;p&gt;The Infrastructure also provides interfaces for interacting with these components. For example, rather than accessing a specific database directly, the Core uses an interface defined with the Infrastructure to interact with the data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Separation of responsibilities
&lt;/h3&gt;

&lt;p&gt;Finally, an essential feature of the Infrastructure is its ability to separate responsibilities. Each component is designed to perform a specific task, making it easy to maintain and evolve that part too. For example, a component responsible for data access can be easily replaced or upgraded without affecting the rest of the application or even the rest of the technical part.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of Core vs. Infrastructure separation
&lt;/h2&gt;

&lt;p&gt;The clear distinction between Core and Infrastructure offers many advantages for application development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Maintainability and scalability
&lt;/h3&gt;

&lt;p&gt;By isolating the business logic from the rest of the application, Clean Architecture facilitates code maintenance and evolution. Changes made to the Infrastructure have no impact on the Core, enabling technical components to be updated or replaced without disrupting business functionality.&lt;/p&gt;

&lt;p&gt;For example, you can decide to change the SMS sending partner, without any impact on the business code, which remains unchanged.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clear understanding of the Code Base
&lt;/h3&gt;

&lt;p&gt;The distinction between Core and Infrastructure makes the code clearer and more comprehensible. As I often say, the team PO can even have an eye on the Core part, without necessarily having a technical background.&lt;/p&gt;

&lt;p&gt;Developers can easily identify which parts of the application are responsible for the business logic and which are responsible for the technical details. This facilitates debugging, collaboration and long-term maintenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core and Infrastructure, the heart of Clean Architecture
&lt;/h2&gt;

&lt;p&gt;Clean Architecture, with its distinction between Core and Infrastructure, offers a solid, structured approach to application development. By fully understanding this separation and focusing on entities and use cases as the pillars of the business domain, you can create applications that are robust, scalable and easier to maintain.&lt;/p&gt;

&lt;p&gt;This article is an introduction to Clean Architecture and is part of a dedicated series on this topic. Stay tuned for more!&lt;/p&gt;

&lt;p&gt;Want to learn how implement it with typescript and express? See my udemy course! In &lt;a href="https://www.udemy.com/course/clean-architecture-avec-typescript-et-express/?referralCode=8C9C5BF69F624247D63A" rel="noopener noreferrer"&gt;french&lt;/a&gt; or &lt;a href="https://www.udemy.com/course/clean-architecture-with-typescript-and-nodejs-express/?referralCode=A0683A3DE7EDE1BD15A0" rel="noopener noreferrer"&gt;english&lt;/a&gt; 😉&lt;/p&gt;

&lt;p&gt;Articles on Clean Architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/demystifying-clean-architecture-fundamentals-and-benefits-5ae7"&gt;Demystifying Clean Architecture: Fundamentals and Benefits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/clean-architecture-business-rules-first-4dlo"&gt;Clean Architecture: Business rules first!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/introduction-to-the-principles-of-clean-architecture-in-a-nodejs-api-express-13e9"&gt;Introduction to the principles of clean architecture in a NodeJs API (Express)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>development</category>
      <category>webdev</category>
      <category>programming</category>
      <category>cleancoding</category>
    </item>
    <item>
      <title>Demystifying Clean Architecture: Fundamentals and Benefits</title>
      <dc:creator>Nicolas Lapointe</dc:creator>
      <pubDate>Thu, 02 May 2024 08:30:59 +0000</pubDate>
      <link>https://forem.com/nlapointe/demystifying-clean-architecture-fundamentals-and-benefits-5ae7</link>
      <guid>https://forem.com/nlapointe/demystifying-clean-architecture-fundamentals-and-benefits-5ae7</guid>
      <description>&lt;p&gt;Imagine building a house with an inconsistent plan, where walls, roof, and foundations blend into a cheerful chaos... Well, that's exactly what the world of development can look like! Let's not kid ourselves; it happens (far too) often. We've probably all faced the spaghetti plate, a code where everything is mixed up and hard to maintain. Clean Architecture will be the master builder accompanying the construction of the house, bringing a framework that brings order to the increasing complexity of modern applications.&lt;/p&gt;

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

&lt;p&gt;Clean Architecture is not just a buzzword. Resulting from the work of Robert C. Martin (a publication in 2012 followed by a book in 2017 still available), it is an increasingly essential concept in modern development that offers a structured approach to designing robust, more easily maintainable, and scalable applications.&lt;br&gt;
&lt;strong&gt;Let's first define the basics.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;Clean Architecture aims to &lt;strong&gt;clearly separate concerns&lt;/strong&gt;, with business aspects on one side and technical aspects on the other. It also aims to maintain &lt;strong&gt;essential control over dependencies&lt;/strong&gt;, avoiding being tightly bound or coupled to them.&lt;/p&gt;

&lt;p&gt;These are the two fundamental principles:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1- Separation of Concerns&lt;/strong&gt;&lt;br&gt;
With Clean Architecture, imagine each element of your application having a clear and distinct role. &lt;strong&gt;Business logic doesn't mix with technical details.&lt;/strong&gt; The application is divided into layers, each with its responsibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2- Independence of Frameworks and Tools&lt;/strong&gt;&lt;br&gt;
Here, flexibility is key. &lt;strong&gt;Your application must be agile and able to adapt to changes without compromising its stability.&lt;/strong&gt; A significant security flaw in one dependency? GDPR constraint on another? No matter, adapting the code, replacing it with another dependency, should be painless.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Layers of Clean Architecture
&lt;/h3&gt;

&lt;p&gt;In the definition part, we discussed decomposing into several distinct layers, each with a specific responsibility and role, following the principle of isolation. &lt;strong&gt;Let's dive into the details&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;The Entities Layer&lt;/strong&gt;&lt;br&gt;
Entities form the heart of domain modeling, business. &lt;strong&gt;They are the building blocks of your application, guardians of business logic&lt;/strong&gt;, without worrying about technical details, data persistence, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Use Cases Layer&lt;/strong&gt;&lt;br&gt;
Use cases are like conductors of your application. &lt;strong&gt;They direct business logic and ensure that everything runs smoothly&lt;/strong&gt; with coordination between different parts of the application. They clearly describe business logic scenarios. I like to emphasize that the Product Owner's perspective can be interesting in this layer!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The User Interfaces Layer (Interface Adapters)&lt;/strong&gt;&lt;br&gt;
In this layer, we deal with interactions with the outside world. &lt;strong&gt;User interfaces and adapters handle inputs and outputs&lt;/strong&gt;, like a gateway between the application and external dependencies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Infrastructure Layer&lt;/strong&gt;&lt;br&gt;
Finally, the infrastructure layer is the lair of technology. &lt;strong&gt;It is responsible for interacting with databases, frameworks, and everything related to technical aspects.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each layer is isolated, independent. To make them communicate with each other, we'll use what's called dependency injection.&lt;/p&gt;

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

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

&lt;p&gt;Now that we've seen the fundamentals and understand the basics, let's look at the &lt;strong&gt;concrete advantages&lt;/strong&gt; of Clean Architecture principles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Better Maintainability &amp;amp; Scalability&lt;/strong&gt;&lt;br&gt;
Our master builder has fulfilled its mission, the house is well-designed, and each room adapts to a single need. If I want to add a room or change a window, it's immediately simpler! &lt;strong&gt;Clean Architecture brings this flexibility, allowing the application to adapt and grow without difficulty.&lt;/strong&gt; Unlike spaghetti code, where pulling on one side causes crashes elsewhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Better Testability&lt;/strong&gt;&lt;br&gt;
It's quite logical that with the principle of isolation, separation of concerns, where each layer has its responsibility, &lt;strong&gt;the code structure facilitates the implementation of unit tests.&lt;/strong&gt; Simple elements lead to simple tests, simple tests mean more robust code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Technical Independence&lt;/strong&gt;&lt;br&gt;
Still in our house, think of being able to change furniture without affecting the basic structure. This is exactly one of the fundamental principles of Clean Architecture, &lt;strong&gt;the independence of the technical layer.&lt;/strong&gt; If changing the table requires changing the tiles, two walls, and three windows, it's immediately another project!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clearer Understanding of the Code Base&lt;/strong&gt;&lt;br&gt;
By following the principles of separation of concerns at the code structure level, &lt;strong&gt;developers find a clear path within and between each layer&lt;/strong&gt;, facilitating the understanding of the different parts of the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages?&lt;/strong&gt;&lt;br&gt;
Of course, as often in the world of development, everything is not black or white. If we have advantages, we have disadvantages too: More complexity in development, a slightly steeper learning curve, it can be interesting to climb a few steps by following training. We can also add a larger source code size, or what can be considered as overdevelopment, and complexity during the initial configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  To Conclude
&lt;/h2&gt;

&lt;p&gt;Clean Architecture is not just a development method; it is a philosophy that changes the way we design our applications. &lt;strong&gt;By adopting its principles, we ensure developing robust, scalable, and easier to maintain applications.&lt;/strong&gt; And inevitably, it's also an improvement in the developer experience, which we'll address in a separate article.&lt;/p&gt;

&lt;p&gt;But it's not a magic solution either! It's a development architecture, like others, and like others that will come tomorrow. &lt;strong&gt;It's up to you to determine&lt;/strong&gt; if Clean Architecture is a good choice for your project, your context. In one of my experiences, we developed a POC, with the aim of validating a market and Web 3 technical specifications. We were moving forward without real functional specifications; each day brought, removed, modified a feature: It would have been too expensive to ensure having a clean structure for code that we had already planned to "discard."&lt;/p&gt;

&lt;p&gt;But anyway, with the rise of generative AIs or Low Code / No Code applications (and both at the same time tomorrow?), &lt;strong&gt;the market share of applications developed with a tool will continue to grow&lt;/strong&gt; and take its place, that's my conviction. Any application that doesn't fall into this category will inevitably be more complex, will require addressing challenges that Low Code / No Code or AI cannot solve. So, it will necessarily require a &lt;strong&gt;solid, robust, scalable, maintainable structure&lt;/strong&gt;, everything that Clean Architecture offers today, and why not a new method or new principles tomorrow!&lt;/p&gt;

&lt;p&gt;This article is an introduction to Clean Architecture and is part of a dedicated series on this topic. Stay tuned for more!&lt;/p&gt;

&lt;p&gt;Want to learn how implement it with typescript and express? See my udemy course! In &lt;a href="https://www.udemy.com/course/clean-architecture-avec-typescript-et-express/?referralCode=8C9C5BF69F624247D63A"&gt;french&lt;/a&gt; or &lt;a href="https://www.udemy.com/course/clean-architecture-with-typescript-and-nodejs-express/?referralCode=A0683A3DE7EDE1BD15A0"&gt;english&lt;/a&gt; 😉&lt;/p&gt;

&lt;p&gt;Articles on Clean Architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/clean-architecture-infrastructure-vs-core-492k"&gt;Clean Architecture : Infrastructure vs Core&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/clean-architecture-business-rules-first-4dlo"&gt;Clean Architecture: Business rules first!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nlapointe/introduction-to-the-principles-of-clean-architecture-in-a-nodejs-api-express-13e9"&gt;Introduction to the principles of clean architecture in a NodeJs API (Express)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>cleancode</category>
      <category>cleancoding</category>
      <category>cleanarchitecture</category>
    </item>
  </channel>
</rss>
