<?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: Nahid Chowdhury</title>
    <description>The latest articles on DEV Community by Nahid Chowdhury (@ovichowdhury).</description>
    <link>https://dev.to/ovichowdhury</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%2F1183338%2F410fe023-be5c-4e8d-943e-12dba2fcdaa6.jpg</url>
      <title>DEV Community: Nahid Chowdhury</title>
      <link>https://dev.to/ovichowdhury</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ovichowdhury"/>
    <language>en</language>
    <item>
      <title>The Dependency Injection Secrets That Change How You Design Systems</title>
      <dc:creator>Nahid Chowdhury</dc:creator>
      <pubDate>Wed, 25 Feb 2026 09:44:07 +0000</pubDate>
      <link>https://dev.to/ovichowdhury/the-dependency-injection-secrets-that-change-how-you-design-systems-1nch</link>
      <guid>https://dev.to/ovichowdhury/the-dependency-injection-secrets-that-change-how-you-design-systems-1nch</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In object-oriented programming, Dependency Injection is one of the most important design techniques for building scalable and maintainable large-scale applications. While it is often associated with OOP, the underlying idea is not limited to object-oriented languages and can also be applied in functional programming environments.&lt;/p&gt;

&lt;p&gt;To truly understand Dependency Injection, we must first understand the principle behind it. Dependency Injection is not the goal itself—it is a technique used to achieve the Dependency Inversion Principle, one of the five SOLID principles.&lt;/p&gt;

&lt;h2&gt;
  
  
  DIP — According to Robert C. Martin
&lt;/h2&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;/p&gt;

&lt;p&gt;In a statically typed language, like Java, this means that the use, import, and include statements should refer only to source modules containing interfaces, abstract classes, or some other kind of abstract declaration. Nothing concrete should be depended on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the formal definition of the Dependency Inversion Principle. In simpler terms, it means that we should depend on abstractions (interfaces or abstract classes) rather than concrete implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;❌ Violates DIP (high-level depends on low-level)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailSender&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Email to &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;EmailSender&lt;/span&gt; &lt;span class="n"&gt;_emailSender&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;EmailSender&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// concrete dependency&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;PlaceOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;customerEmail&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// order logic...&lt;/span&gt;
        &lt;span class="n"&gt;_emailSender&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="n"&gt;customerEmail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Your order has been placed."&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;strong&gt;✅ Applies DIP + DI (depend on abstraction, inject implementation)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;INotifier&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailNotifier&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;INotifier&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Email to &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;INotifier&lt;/span&gt; &lt;span class="n"&gt;_notifier&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;OrderService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;INotifier&lt;/span&gt; &lt;span class="n"&gt;notifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// injected abstraction&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_notifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notifier&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="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;PlaceOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;customerEmail&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// order logic...&lt;/span&gt;
        &lt;span class="n"&gt;_notifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customerEmail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Your order has been placed."&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="c1"&gt;// Program.cs (Composition - Manual DI)&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;notifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;EmailNotifier&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;notifier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;PlaceOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"nahid@example.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding Dependency Injection
&lt;/h2&gt;

&lt;p&gt;Now that we understand the Dependency Inversion Principle, let’s look at Dependency Injection.&lt;/p&gt;

&lt;p&gt;As the example code block shows, manually injecting dependent objects requires initialization logic. For example, &lt;code&gt;OrderService&lt;/code&gt; needs an &lt;code&gt;EmailService&lt;/code&gt;, so the &lt;code&gt;EmailService&lt;/code&gt; must be created beforehand. Now imagine that EmailService depends on an &lt;code&gt;HttpService&lt;/code&gt;, which must also be created and passed in. Then &lt;code&gt;HttpService&lt;/code&gt; depends on &lt;code&gt;SocketService&lt;/code&gt; and &lt;code&gt;CryptoService&lt;/code&gt;, which must also be instantiated and supplied.&lt;/p&gt;

&lt;p&gt;The chain continues.&lt;/p&gt;

&lt;p&gt;Managing this object graph manually quickly becomes complex and hard to maintain. Dependency Injection tools are designed to solve this problem—and more. At its core, a DI container helps us organize and resolve dependencies so we don’t have to manually construct and pass every dependency through every layer of the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependency Injection: Beyond the new Keyword&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So far, we’ve discussed how Dependency Injection solves the problem of building and managing complex object graphs. But I also said it solves more than that.&lt;/p&gt;

&lt;p&gt;You might be thinking: “My application is small. I don’t need a complex setup to manage my objects. Using the &lt;code&gt;new&lt;/code&gt; operator in my controller, service, or repository layer is enough.”&lt;/p&gt;

&lt;p&gt;But stop.&lt;/p&gt;

&lt;p&gt;The real power of Dependency Injection goes beyond object creation. It directly impacts your application's &lt;strong&gt;performance&lt;/strong&gt; and &lt;strong&gt;maintainability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the following sections, I will practically demonstrate how mismanaging—or completely ignoring—Dependency Injection can make your application worse than you expect. You’ll also see how proper use of DI can significantly improve both performance and long-term maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object Creation Is Not Free&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In object-oriented programming, object creation and lifecycle management are among the most critical aspects that require careful consideration.&lt;/p&gt;

&lt;p&gt;According to the Gang of Four (GoF) design patterns, Creational Design Patterns (such as Singleton, Factory, etc.) primarily focus on managing object creation in a controlled and structured way.&lt;/p&gt;

&lt;p&gt;So why should we care about object creation?&lt;/p&gt;

&lt;p&gt;Because every object has both time and space complexity associated with it. Each object allocated in memory consumes space in the CLR heap (.NET), and its creation requires CPU cycles. Modern computers are fast, but at scale, these costs add up significantly.&lt;/p&gt;

&lt;p&gt;To demonstrate this, I conducted a benchmark on the following machine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU: Intel(R) Core(TM) i7-8700 @ 3.20GHz&lt;/li&gt;
&lt;li&gt;Memory: 16 GB&lt;/li&gt;
&lt;li&gt;OS: Windows 11&lt;/li&gt;
&lt;li&gt;Runtime: .NET 9&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The benchmark measured the overhead of creating an Entity Framework DbContext with three DbSets and tracking one active entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benchmark Details:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ovichowdhury/TransactionService/blob/main/TransactionService.Console/Scripts/ObjectMemoryOverhead.csx" rel="noopener noreferrer"&gt;Explore the Benchmark Code&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iterations: 100,000 (create context + touch model + track 1 entity)&lt;/li&gt;
&lt;li&gt;Total time: 2.94 seconds&lt;/li&gt;
&lt;li&gt;Average time per iteration: 29.39 µs (~0.029 ms) ~34,000 iterations per second&lt;/li&gt;
&lt;li&gt;Total managed allocations: ~2.04 GB (≈ 1.90 GiB)&lt;/li&gt;
&lt;li&gt;Average allocation per iteration: ~20.38 KB (≈ 19.9 KiB)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though each iteration seems inexpensive in isolation, 100,000 iterations resulted in over 2 GB of managed allocations. That is a significant amount of memory pressure, which directly impacts garbage collection and overall application performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Performance Implications of Dependency Injection
&lt;/h2&gt;

&lt;p&gt;The performance implications of Dependency Injection are largely determined by object (or service) lifecycles. A DI container forces us to think explicitly about how long each object should live within an application.&lt;/p&gt;

&lt;p&gt;To understand its impact on performance, we must first understand the different lifetimes an object can have. I will use .NET as a practical example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Think of an application like a hotel.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Different things inside a hotel have different lifetimes. The building is shared by everyone, all day, every day. It is not rebuilt for each guest. In a .NET application, this is like a &lt;strong&gt;Singleton&lt;/strong&gt; service — created once and shared for the entire lifetime of the application process. Services such as logging, configuration, or caching usually belong to this category.&lt;/p&gt;

&lt;p&gt;A hotel room, however, is assigned to one guest for the duration of their stay. It exists only during that stay and is reset afterward. In ASP.NET Core, this is similar to a &lt;strong&gt;Scoped&lt;/strong&gt; service — created once per HTTP request and disposed at the end of that request. A &lt;code&gt;DbContext&lt;/code&gt; is a perfect example of this lifetime.&lt;/p&gt;

&lt;p&gt;Finally, consider a dental kit provided to a guest. It is created for immediate use and discarded afterward. This is like a &lt;strong&gt;Transient&lt;/strong&gt; service — a new instance is created every time it is requested. Transient services are typically lightweight and stateless.&lt;/p&gt;

&lt;p&gt;In a real OOP application, objects behave the same way. Some should live for the entire application lifetime, some should exist only within a request boundary, and others should be created per use. Choosing the correct lifetime is not just an architectural preference — it directly affects memory usage, garbage collection, concurrency safety, and overall performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object Lifetime Management in ASP.NET Core DI&lt;/strong&gt;&lt;br&gt;
Let’s walk through a practical demonstration of how the .NET DI container manages object lifetimes — and how incorrect lifetime configuration can significantly degrade application performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Program.cs - Correct lifetimes in .NET DI (baseline)&lt;/span&gt;

&lt;span class="c1"&gt;// app-wide&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddSingleton&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ICache&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MemoryCacheService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;    
&lt;span class="c1"&gt;// per request &lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddScoped&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IUnitOfWork&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;UnitOfWork&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// per request (typical)         &lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddScoped&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AppDbContext&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// per use                    &lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IJsonFormatter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;JsonFormatter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s look at an example of inconsistent use of Dependency Injection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ITokenKeyStore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;GetKey&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="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TokenKeyStore&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ITokenKeyStore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;_keyMaterial&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;TokenKeyStore&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Simulate expensive initialization + memory allocation&lt;/span&gt;
        &lt;span class="n"&gt;_keyMaterial&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;1024&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// 10 MB&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;42&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;NextBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_keyMaterial&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="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;GetKey&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_keyMaterial&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// ❌ BAD: creates a new 10MB object graph frequently → allocations + GC pressure&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ITokenKeyStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TokenKeyStore&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// ✅ GOOD: create once, reuse safely (if thread-safe / immutable)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddSingleton&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ITokenKeyStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TokenKeyStore&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example, TokenKeyStore performs an expensive initialization. During construction, it allocates 10MB of memory and fills it with key material. This means that every time a new instance is created, the runtime must allocate a large object on the heap and eventually reclaim it during garbage collection.&lt;/p&gt;

&lt;p&gt;If this service is registered as Transient, a new 10MB instance will be created every time it is requested from the container.&lt;/p&gt;

&lt;p&gt;Under light development traffic, this may go unnoticed. But under real production load, frequent large allocations can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increase garbage collection frequency&lt;/li&gt;
&lt;li&gt;Add CPU overhead&lt;/li&gt;
&lt;li&gt;Reduce throughput&lt;/li&gt;
&lt;li&gt;Introduce latency spikes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How DI Protects You from Circular Dependency Pitfalls&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s look at a simple example and understand circular dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ServiceA&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ServiceB&lt;/span&gt; &lt;span class="n"&gt;_serviceB&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;ServiceA&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_serviceB&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ServiceB&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ServiceB&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ServiceA&lt;/span&gt; &lt;span class="n"&gt;_serviceA&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;ServiceB&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_serviceA&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ServiceA&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;Now when we try to run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;serviceA&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ServiceA&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ServiceA&lt;/code&gt; creates &lt;code&gt;ServiceB&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;ServiceB&lt;/code&gt; creates &lt;code&gt;ServiceA&lt;/code&gt;.&lt;br&gt;
That new &lt;code&gt;ServiceA&lt;/code&gt; creates another &lt;code&gt;ServiceB&lt;/code&gt;.&lt;br&gt;
And the cycle continues…&lt;/p&gt;

&lt;p&gt;Until the runtime gives up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Stack overflow.
Repeated 12002 times...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a circular dependency—two services depending on each other in a loop. It becomes even more problematic when dealing with real-world, enterprise-grade applications that involve hundreds of objects interacting at the same time.&lt;/p&gt;

&lt;p&gt;When using a DI container, this problem is detected early. Instead of crashing with a stack overflow, the container throws a clear exception saying a circular dependency was detected.&lt;/p&gt;

&lt;p&gt;And that’s a good thing.&lt;/p&gt;

&lt;p&gt;Because circular dependencies don’t just break your application — they signal that your design is fighting itself.&lt;/p&gt;

&lt;p&gt;You can try it yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// CircularService.cs&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ServiceA&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;ServiceA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ServiceB&lt;/span&gt; &lt;span class="n"&gt;serviceB&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;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;DoSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ServiceA is doing something."&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ServiceB&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;ServiceB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ServiceA&lt;/span&gt; &lt;span class="n"&gt;serviceA&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;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;DoSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ServiceB is doing something."&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="c1"&gt;// Program.cs - Testing Circular Dependency&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddScoped&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ServiceA&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddScoped&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ServiceB&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;When we run this application using &lt;code&gt;dotnet run&lt;/code&gt;, it throws an error during application startup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Unhandled exception. System.AggregateException: Some services are not able to be constructed &lt;span class="o"&gt;(&lt;/span&gt;Error &lt;span class="k"&gt;while &lt;/span&gt;validating the service descriptor &lt;span class="s1"&gt;'ServiceType: ServiceA Lifetime: Scoped ImplementationType: ServiceA'&lt;/span&gt;: A circular dependency was detected &lt;span class="k"&gt;for &lt;/span&gt;the service of &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="s1"&gt;'ServiceA'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is one of the real strengths of Dependency Injection. It prevents catastrophic runtime failures and forces architectural issues to surface early, before they evolve into performance bottlenecks or production outages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is There Any Performance Implication of Manual DI?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes — but not because manual DI is slow.&lt;/p&gt;

&lt;p&gt;Manual DI is like driving a manual car. It’s perfectly fine. In fact, in the right hands, it can perform beautifully. But it requires discipline. You control when objects are created, how long they live, and when they are disposed.&lt;/p&gt;

&lt;p&gt;And that’s exactly where things go wrong.&lt;/p&gt;

&lt;p&gt;In small applications, manual DI works great. You create objects carefully, reuse what needs to be reused, and everything behaves. But as the application grows and more developers touch the code, &lt;code&gt;new&lt;/code&gt; starts appearing everywhere. Heavy objects get recreated unnecessarily. Expensive resources are not reused. Disposal is forgotten.&lt;/p&gt;

&lt;p&gt;Suddenly, memory usage increases, garbage collection runs more frequently, CPU usage spikes, and production starts behaving very differently from your local machine.&lt;/p&gt;

&lt;p&gt;So no — manual DI isn’t slow.&lt;/p&gt;

&lt;p&gt;But manual DI without strict lifetime discipline?&lt;br&gt;
That’s how performance slowly degrades, one innocent &lt;code&gt;new&lt;/code&gt; at a time.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Maintainability Advantages of Dependency Injection
&lt;/h2&gt;

&lt;p&gt;Performance matters. Stability matters.&lt;/p&gt;

&lt;p&gt;But in real-world software, one thing matters even more:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Change.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requirements change.&lt;/li&gt;
&lt;li&gt;Business rules change.&lt;/li&gt;
&lt;li&gt;Technologies change.&lt;/li&gt;
&lt;li&gt;Teams change.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the real question is not:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Does my code work today?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“How painful will it be to change tomorrow?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s look at a simple example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;EmailService&lt;/span&gt; &lt;span class="n"&gt;_emailService&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;OrderService&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_emailService&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;EmailService&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="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;PlaceOrder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Business logic&lt;/span&gt;
        &lt;span class="n"&gt;_emailService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SendReceipt&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;At first glance, this looks harmless.&lt;/p&gt;

&lt;p&gt;But now imagine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to switch from &lt;code&gt;EmailService&lt;/code&gt; to &lt;code&gt;SmsService&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You want to mock notifications for testing&lt;/li&gt;
&lt;li&gt;You want to introduce retry logic&lt;/li&gt;
&lt;li&gt;You want to log notification failures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you must modify &lt;code&gt;OrderService&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Your business logic is tightly coupled to a concrete implementation.&lt;/p&gt;

&lt;p&gt;Now let’s refactor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;INotificationService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SendReceipt&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailNotificationService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;INotificationService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SendReceipt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"📧 Receipt sent via Email."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// real-world: SMTP/SendGrid/etc.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// SMS implementation&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmsNotificationService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;INotificationService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SendReceipt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"📱 Receipt sent via SMS."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// real-world: Twilio/etc.&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;INotificationService&lt;/span&gt; &lt;span class="n"&gt;_notificationService&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;OrderService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;INotificationService&lt;/span&gt; &lt;span class="n"&gt;notificationService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_notificationService&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notificationService&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="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;PlaceOrder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_notificationService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SendReceipt&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;Now OrderService does not care:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whether it’s email&lt;/li&gt;
&lt;li&gt;Whether it’s SMS&lt;/li&gt;
&lt;li&gt;Whether it’s a mock&lt;/li&gt;
&lt;li&gt;Whether it’s a composite service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To switch implementations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddScoped&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;INotificationService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EmailNotificationService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// or&lt;/span&gt;

&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddScoped&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;INotificationService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SmsNotificationService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;No changes to business logic.&lt;/p&gt;

&lt;p&gt;That’s &lt;strong&gt;maintainability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And this is just the beginning.&lt;/p&gt;

&lt;p&gt;So far, we only used DI to make one dependency replaceable. But once your codebase grows, DI unlocks a few other quiet advantages that most teams don’t fully take advantage of.&lt;/p&gt;

&lt;p&gt;For example, DI makes it easy to add &lt;strong&gt;cross-cutting concerns&lt;/strong&gt;—like logging, retries, caching, metrics, and tracing—without polluting your business logic. Instead of sprinkling &lt;code&gt;try/catch&lt;/code&gt;, &lt;code&gt;logger.Log(...)&lt;/code&gt;, and retry loops inside every service, you can wrap behavior using &lt;strong&gt;decorators&lt;/strong&gt; and keep the core logic clean.&lt;/p&gt;

&lt;p&gt;DI also helps you catch design problems early (like circular dependencies) and gives you a single place to manage object lifetimes and disposal—both of which become huge for long-term stability.&lt;/p&gt;

&lt;p&gt;I won’t dive deep into those topics here, because this article would turn into a book. But keep them in mind: once you start treating DI as a design tool—not just a convenience tool—it changes how you build and evolve systems.&lt;/p&gt;

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

&lt;p&gt;Dependency Injection isn’t just about avoiding the new keyword. It’s about making your software behave like a well-managed hotel instead of a chaotic hostel where everyone builds their own room, shares the same toothbrush, and somehow the building gets rebuilt every time a guest checks in.&lt;/p&gt;

&lt;p&gt;Used correctly, DI gives you control over lifetimes, protects you from circular dependency disasters, and makes change feel like a refactor—not a horror movie. Used incorrectly, it quietly burns CPU cycles, inflates memory, and makes production behave like it’s possessed.&lt;/p&gt;

&lt;p&gt;So the next time you reach for &lt;code&gt;new&lt;/code&gt; inside a controller, just remember: you’re not just creating an object, you might be creating a future outage.&lt;/p&gt;

&lt;p&gt;Happy injecting — and may your dependencies never loop back to haunt you.😄&lt;/p&gt;

</description>
      <category>dependencyinversion</category>
      <category>solidprinciples</category>
      <category>systemdesign</category>
      <category>oop</category>
    </item>
    <item>
      <title>Beating Scope Creep: Implementing Extreme Prototyping (XPT) with GenAI Bolt, Figma &amp; Postman</title>
      <dc:creator>Nahid Chowdhury</dc:creator>
      <pubDate>Thu, 10 Jul 2025 04:52:47 +0000</pubDate>
      <link>https://dev.to/ovichowdhury/beating-scope-creep-implementing-extreme-prototyping-xpt-with-genai-bolt-figma-postman-51m4</link>
      <guid>https://dev.to/ovichowdhury/beating-scope-creep-implementing-extreme-prototyping-xpt-with-genai-bolt-figma-postman-51m4</guid>
      <description>&lt;h2&gt;
  
  
  What is Extreme Prototyping?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Extreme Prototyping (XPt)&lt;/strong&gt; is a rapid, three-stage approach to building web-based (or API-centric) applications.&lt;br&gt;
Its goal is to get a &lt;em&gt;working&lt;/em&gt; version in front of users very quickly, then layer in functionality step-by-step—minimizing wasted effort on back-end details until the front-end experience is nailed down.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Three Stages at a Glance
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Typical Deliverables&lt;/th&gt;
&lt;th&gt;Key Roles Involved&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Stage 1 – Static Mock-up&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Nail the look-and-feel&lt;/td&gt;
&lt;td&gt;Pure HTML/CSS (or low-code UI) with placeholder data&lt;/td&gt;
&lt;td&gt;UX designer, front-end dev, product owner&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Stage 2 – Simulated Services&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Validate user flows&lt;/td&gt;
&lt;td&gt;Same UI, but wired to &lt;em&gt;stub or mock&lt;/em&gt; services that return canned JSON/XML&lt;/td&gt;
&lt;td&gt;Front-end &amp;amp; back-end devs collaborate on service contracts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Stage 3 – Live Services&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Make it real&lt;/td&gt;
&lt;td&gt;Replace stubs with production-ready APIs, add persistence, security, performance&lt;/td&gt;
&lt;td&gt;Full cross-functional team&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Why Teams Use It
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Instant feedback&lt;/strong&gt;&lt;br&gt;
Stakeholders click through a realistic UI in days, not weeks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced re-work&lt;/strong&gt;&lt;br&gt;
Because the front-end is proven early, fewer late-stage changes ripple into the back-end.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Parallel work streams&lt;/strong&gt;&lt;br&gt;
Back-end engineers can build real services &lt;em&gt;in parallel&lt;/em&gt; with Stage 2 once contracts are frozen.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Risk containment&lt;/strong&gt;&lt;br&gt;
Usability and business-logic risks surface early; technical-integration risks are isolated to Stage 3.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Simple Walk-Through Example
&lt;/h3&gt;

&lt;p&gt;Imagine you’re building an &lt;strong&gt;online travel-booking portal&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;What You Release&lt;/th&gt;
&lt;th&gt;What Users Can Do&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Static pages: Search form, flight list, seat-selection pop-up.&lt;/td&gt;
&lt;td&gt;Click around; see dummy fares and seats.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;UI now calls &lt;code&gt;/flights/search&lt;/code&gt; and &lt;code&gt;/seats/mock&lt;/code&gt; endpoints returning hard-coded JSON.&lt;/td&gt;
&lt;td&gt;Perform full booking flow, receive “Your reservation is #0000”. No real tickets issued.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Real payment gateway, live airline API, database persistence, auth.&lt;/td&gt;
&lt;td&gt;Book and pay for an actual flight, get e-ticket, receive email confirmation.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;At each stage, feedback loops stay tight: marketing tweaks copy in Stage 1, UX adjusts seat-map behavior in Stage 2, DevOps tunes API latency in Stage 3.&lt;/p&gt;

&lt;h3&gt;
  
  
  When Extreme Prototyping Shines
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You’re building something from scratch&lt;/strong&gt;.&lt;br&gt;
Maybe it’s a brand-new web or mobile app and no one’s 100% sure how it should look or behave yet. XPT lets you get a clickable version in front of people fast—so you’re not guessing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Your team is API-first or backend-heavy&lt;/strong&gt;.&lt;br&gt;
Stage 2’s mocked services mean your backend developers can define and build APIs independently, while the UI team moves ahead without waiting. Bonus: your mocks become living documentation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You're juggling evolving requirements&lt;/strong&gt;.&lt;br&gt;
When product decisions shift mid-sprint, having a flexible front-end-first workflow makes it easier to adapt without breaking everything.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Figma, Bolt New, and Postman fit into the picture
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;XPT Stage&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Tools Used&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;How They Help&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Stage 1 – Static Mock-up&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Figma, GenAI Bolt&lt;/td&gt;
&lt;td&gt;In Stage 1, Figma is used to quickly map out the look and feel of the app—think screens, buttons, and flow. GenAI Bolt steps in to fill those screens with realistic placeholder text, form fields, and sample data, so everything feels real enough for early feedback without writing a single line of backend code.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Stage 2 – Simulated Services&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;GenAI Bolt, Postman&lt;/td&gt;
&lt;td&gt;In Stage 2, Postman’s Mock Servers let the team simulate how real APIs will behave—so the frontend doesn’t have to wait for the backend to be ready. It’s also the point where frontend and backend teams sit down and agree on the service contract (what data goes where, and how). GenAI Bolt helps by generating the glue code that connects the UI to these mock services, while Figma gets refined as real users click through and provide feedback.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Stage 3 – Live Services&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Postman, GenAI Bolt, Figma (minor)&lt;/td&gt;
&lt;td&gt;In Stage 3, Postman is used to test real APIs and make sure everything connects smoothly across systems. GenAI Bolt helps speed things up by generating boilerplate code and test cases, while Figma supports any last-mile UI tweaks—fine-tuning the interface before launch.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Quick Checklist to Get Started
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Stage 1:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;☐ Flesh out critical screens in Figma or code.&lt;/li&gt;
&lt;li&gt;☐ Review with users; freeze visual design.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Stage 2:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;☐ Define API endpoints &amp;amp; sample payloads.&lt;/li&gt;
&lt;li&gt;☐ Wire UI to mocks; validate end-to-end journeys.&lt;/li&gt;
&lt;li&gt;☐ Capture edge-cases and refine contracts.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Stage 3:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;☐ Implement real services behind the same contracts.&lt;/li&gt;
&lt;li&gt;☐ Add data persistence, security, and monitoring.&lt;/li&gt;
&lt;li&gt;☐ Run full regression; launch!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key takeaway
&lt;/h3&gt;

&lt;p&gt;Extreme Prototyping (XPT) is all about getting real user feedback early—before diving deep into the technical build. It helps teams focus on what really matters to users first, while backend details catch up in parallel. The best part? XPT fits naturally into Scrum and sprint cycles, making it easier to demo working prototypes every sprint and ship the right product faster—without cutting corners on quality.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>xpt</category>
      <category>bolt</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Write a Software Project Proposal: Complete Template &amp; Guide</title>
      <dc:creator>Nahid Chowdhury</dc:creator>
      <pubDate>Thu, 26 Jun 2025 05:53:50 +0000</pubDate>
      <link>https://dev.to/ovichowdhury/how-to-write-a-software-project-proposal-complete-template-guide-6dn</link>
      <guid>https://dev.to/ovichowdhury/how-to-write-a-software-project-proposal-complete-template-guide-6dn</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;As a Software Architect, I was recently assigned to prepare a software project proposal for a client, which also involved conducting a requirement analysis in collaboration with our Business Analyst team to identify the functional and non-functional requirements. A significant part of software engineering is the systematic process of requirements engineering and the preparation of a comprehensive document known as the Software Requirements Specification (SRS). This document serves as an agreed-upon scope and working modality among all stakeholders. However, preparing such a document has not been within my comfort zone, as my experience over the past few years—as a Senior Software Engineer—has been primarily focused on development and reviewing technical artifacts.&lt;/p&gt;

&lt;p&gt;After conducting research and holding several internal review sessions, we successfully compiled a format that addresses the needs of all stakeholders within a single comprehensive document.&lt;/p&gt;

&lt;h1&gt;
  
  
  Template
&lt;/h1&gt;




&lt;h3&gt;
  
  
  1. &lt;strong&gt;Cover Page&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Project Title&lt;/li&gt;
&lt;li&gt;Client Name / Organization&lt;/li&gt;
&lt;li&gt;Submitted By (Company / Team)&lt;/li&gt;
&lt;li&gt;Date&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Table of Contents&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The following section outlines the document's headings along with corresponding page numbers and hyperlinks for easy navigation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Executive Summary / Project Overview&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Brief description of the project&lt;/li&gt;
&lt;li&gt;Business goals and objectives&lt;/li&gt;
&lt;li&gt;Summary of proposed solution&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Project Objectives&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Specific outcomes the system aims to achieve&lt;/li&gt;
&lt;li&gt;KPIs or measurable goals (if known)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Scope of Work&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  A. Functional Scope
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Feature modules broken down by user type or system area (e.g., Customer, Admin, Vendor, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  B. Out of Scope (Optional)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Clearly define what's not included to avoid scope creep&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Technical Approach / Solution Architecture&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;High-level system architecture diagram (optional but recommended)&lt;/li&gt;
&lt;li&gt;Data flow or component diagrams&lt;/li&gt;
&lt;li&gt;Description of how the solution works at a technical level&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Technology Stack&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Technologies&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Frontend&lt;/td&gt;
&lt;td&gt;(e.g., React, Flutter)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend&lt;/td&gt;
&lt;td&gt;(e.g., Node.js, Django)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database&lt;/td&gt;
&lt;td&gt;(e.g., PostgreSQL, MongoDB)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hosting&lt;/td&gt;
&lt;td&gt;(e.g., AWS, Azure)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CI/CD&lt;/td&gt;
&lt;td&gt;(e.g., GitHub Actions)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  8. &lt;strong&gt;Non-Functional Requirements (NFRs)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Performance&lt;/li&gt;
&lt;li&gt;Scalability&lt;/li&gt;
&lt;li&gt;Security (e.g., authentication, data protection)&lt;/li&gt;
&lt;li&gt;Availability/Uptime&lt;/li&gt;
&lt;li&gt;Compliance (if applicable)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  9. &lt;strong&gt;Team Composition &amp;amp; Roles&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;List of team roles and responsibilities&lt;/li&gt;
&lt;li&gt;Optional: resource allocation timeline (e.g., frontend dev full-time for 6 weeks)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  10. &lt;strong&gt;Project Timeline / Milestones&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Phase&lt;/th&gt;
&lt;th&gt;Duration&lt;/th&gt;
&lt;th&gt;Deliverables&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Example: UI/UX Design&lt;/td&gt;
&lt;td&gt;2 weeks&lt;/td&gt;
&lt;td&gt;Wireframes, mockups&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Include Gantt chart (optional but great for presentations)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  11. &lt;strong&gt;Deliverables&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;List of tangible outputs (e.g., source code, documentation, deployed app, admin panel, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  12. &lt;strong&gt;Assumptions &amp;amp; Dependencies&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What you expect the client to provide&lt;/li&gt;
&lt;li&gt;3rd party dependencies (e.g., payment gateway API access)&lt;/li&gt;
&lt;li&gt;Timeline assumptions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  13. &lt;strong&gt;Risk Analysis &amp;amp; Mitigation Plan&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Risk&lt;/th&gt;
&lt;th&gt;Impact&lt;/th&gt;
&lt;th&gt;Likelihood&lt;/th&gt;
&lt;th&gt;Mitigation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  14. &lt;strong&gt;Change Management Process&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;How change requests will be handled&lt;/li&gt;
&lt;li&gt;Approval workflow&lt;/li&gt;
&lt;li&gt;Impact on timeline and cost&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  15. &lt;strong&gt;Communication &amp;amp; Reporting Plan&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Meeting frequency (daily standups, weekly syncs)&lt;/li&gt;
&lt;li&gt;Tools used (e.g., Slack, Jira, Google Meet)&lt;/li&gt;
&lt;li&gt;Reporting responsibilities&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  16. &lt;strong&gt;Testing &amp;amp; Quality Assurance&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Types of testing (unit, integration, UAT)&lt;/li&gt;
&lt;li&gt;QA process and tools&lt;/li&gt;
&lt;li&gt;Acceptance criteria&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  17. &lt;strong&gt;Deployment &amp;amp; Release Strategy&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Staging and production environment&lt;/li&gt;
&lt;li&gt;Rollout plan&lt;/li&gt;
&lt;li&gt;Versioning and rollback strategy&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  18. &lt;strong&gt;Maintenance &amp;amp; Support Plan&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Post-launch support (duration, scope)&lt;/li&gt;
&lt;li&gt;SLA or response time for bugs&lt;/li&gt;
&lt;li&gt;Optional: support tiers (e.g., Basic, Premium)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  19. &lt;strong&gt;Cost Estimate / Pricing&lt;/strong&gt; &lt;em&gt;(Optional if pre-sales or internal)&lt;/em&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Breakdown by deliverables, roles, or timeline&lt;/li&gt;
&lt;li&gt;Payment schedule/milestones&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  20. &lt;strong&gt;Appendices&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;UI mockups&lt;/li&gt;
&lt;li&gt;Sample diagrams&lt;/li&gt;
&lt;li&gt;API documentation reference&lt;/li&gt;
&lt;li&gt;Legal disclaimers or contracts&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Writing a software project proposal used to feel outside my comfort zone—but through collaboration, research, and a bit of trial and error, I realized it’s just another form of clear, structured communication. This template isn’t just a checklist; it’s a bridge between technical minds and business goals. If you’re a developer or architect like me, I hope this format helps you feel more confident tackling the non-code side of software delivery. After all, great software starts with great understanding.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>srs</category>
      <category>projectproposal</category>
      <category>software</category>
    </item>
    <item>
      <title>Demystifying Two Phase Commit (2PC) for Distributed Transaction in Microservices</title>
      <dc:creator>Nahid Chowdhury</dc:creator>
      <pubDate>Mon, 27 Jan 2025 11:57:39 +0000</pubDate>
      <link>https://dev.to/ovichowdhury/demystifying-two-phase-commit-2pc-for-distributed-transaction-in-microservices-5ca7</link>
      <guid>https://dev.to/ovichowdhury/demystifying-two-phase-commit-2pc-for-distributed-transaction-in-microservices-5ca7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Distributed transactions are a fundamental challenge in distributed systems. The rise of microservices architecture has amplified the importance of addressing this challenge effectively. Two-Phase Commit (2PC) is a well-established protocol designed to guarantee the reliable and consistent completion of transactions that involve multiple databases or systems. It remains a significant feature supported by many modern relational database management systems (RDBMS) such as Oracle and PostgreSQL. This post aims to demystify Two-Phase Commit (2PC) by implementing it using Node.js and PostgreSQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Two-Phase Commit (2PC)
&lt;/h2&gt;

&lt;p&gt;Two-Phase Commit is a protocol for managing distributed transactions involving multiple databases or systems. It ensures data atomicity and consistency across multiple systems where a distributed transaction is necessary. It is primarily based on the RDBMS Prepared Transaction feature, which is supported by Oracle &lt;code&gt;DBMS_TRANSACTION&lt;/code&gt;, PostgreSQL &lt;code&gt;PREPARE TRANSACTION&lt;/code&gt;, and MS SQL Server &lt;code&gt;Distributed Transaction Coordinator&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;2PC involves two distinct phases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prepare Phase&lt;/strong&gt;&lt;br&gt;
In this phase, a coordinator informs related services to prepare a transaction with relevant data. However, the transaction remains in the staging area of RDBMS without impacting the actual data. For PostgreSQL, it can be viewed in the &lt;code&gt;pg_prepared_xacts&lt;/code&gt; table. If transaction preparation is successful related services inform the coordinator service with a YES response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decision Phase&lt;/strong&gt;&lt;br&gt;
This phase is also known as Commit/Rollback Phase. If all the related services for the transaction reply with affirmative. The coordinator service informs related services to &lt;code&gt;commit&lt;/code&gt; the prepared transaction.&lt;/p&gt;

&lt;p&gt;if any of the related services failed to prepare the transaction and reply with NO. then the coordinator informs other services to &lt;code&gt;rollback&lt;/code&gt; the prepared transaction. Thus it maintains atomicity and consistency in the data for multiple databases spanning throughout the different services. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Faurgaf5y3yvwa1r4d2yk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Faurgaf5y3yvwa1r4d2yk.png" alt="2PC Success" width="421" height="351"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Fig-1: Commit scenario for 2PC&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fsd85hky5388d4xn18543.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsd85hky5388d4xn18543.png" alt="2PC Failure" width="421" height="351"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Fig-2: Rollback scenario for 2PC&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Preparing PostgreSQL for 2PC
&lt;/h2&gt;

&lt;p&gt;In PostgreSQL, &lt;code&gt;max_prepared_transactions&lt;/code&gt; parameter is used for controlling the number of prepared transactions that can be queued. Adjust it proportionally to your needs and resource availability in the database server. &lt;/p&gt;

&lt;p&gt;For example, you can set this parameter in the postgresql.conf file located in &lt;code&gt;/etc/postgresql/&amp;lt;version&amp;gt;/main/postgresql.conf&lt;/code&gt; in Ubuntu for default &lt;code&gt;apt&lt;/code&gt; based installation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;max_prepared_transactions = 50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PostgreSQL Syntax for 2PC
&lt;/h2&gt;

&lt;p&gt;PostgreSQL supports two-phase commit (2PC) using the commands &lt;code&gt;PREPARE TRANSACTION '&amp;lt;tx_name&amp;gt;'&lt;/code&gt; and &lt;code&gt;COMMIT PREPARED '&amp;lt;tx_name&amp;gt;'&lt;/code&gt;. The transaction name acts as the anchor between the prepare and commit phases, ensuring transaction consistency across multiple nodes or systems. Below is an example demonstrating 2PC functionality in PostgreSQL, which can be tested in pgAdmin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Table Creation&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cgpa&lt;/span&gt; &lt;span class="nb"&gt;NUMERIC&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Begin the transaction&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Perform some operations&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cgpa&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Khan'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Prepare the transaction for future commit/rollback&lt;/span&gt;
&lt;span class="k"&gt;PREPARE&lt;/span&gt; &lt;span class="n"&gt;TRANSACTION&lt;/span&gt; &lt;span class="s1"&gt;'txn_001'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Either commit the transaction at a later point in time&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt; &lt;span class="n"&gt;PREPARED&lt;/span&gt; &lt;span class="s1"&gt;'txn_001'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Or rollback the transaction using tx_name&lt;/span&gt;
&lt;span class="k"&gt;ROLLBACK&lt;/span&gt; &lt;span class="n"&gt;PREPARED&lt;/span&gt; &lt;span class="s1"&gt;'txn_001'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implementation in Node.js
&lt;/h2&gt;

&lt;p&gt;In this example, Express.js and node-postgres have been used for API development.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pool&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pg&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;crypto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;crypto&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// PostgreSQL connection details&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pool&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;Pool&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;postgres&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;testdb&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1234&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Default PostgreSQL port&lt;/span&gt;
  &lt;span class="na"&gt;idleTimeoutMillis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;connectionTimeoutMillis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Health Check
 */&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/health&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="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ok&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="cm"&gt;/**
 * Prepare a Transaction for Commit
 */&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/prepare&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;client&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="kd"&gt;const&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="nx"&gt;cgpa&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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;transactionName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomBytes&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="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Connect to the database&lt;/span&gt;
    &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Start the transaction&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;BEGIN&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Perform some operations&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;INSERT INTO students (name, cgpa) VALUES ($1, $2) RETURNING id&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cgpa&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Prepare the transaction&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`PREPARE TRANSACTION '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;transactionName&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;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;`[INFO] Transaction '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;transactionName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;' prepared.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[ERROR] Error during transaction:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&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;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Clean up and close the connection&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;release&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="p"&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;id&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;txId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;transactionName&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="cm"&gt;/**
 * Commit the Transaction
 */&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/commit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;client&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;txId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Connect to the database&lt;/span&gt;
    &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// Commit the prepared transaction&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`COMMIT PREPARED '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;txId&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;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;`[INFO] Transaction '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;txId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;' committed.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[ERROR] Error during transaction:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&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;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Clean up and close the connection&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;release&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;txId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;txId&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="cm"&gt;/**
 * Rollback the Transaction
 */&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/rollback&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;client&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;txId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Connect to the database&lt;/span&gt;
    &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// Commit the prepared transaction&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`ROLLBACK PREPARED '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;txId&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;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;`[INFO] Transaction '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;txId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;' rollbacked.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[ERROR] Error during transaction:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&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;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Clean up and close the connection&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;release&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;txId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;txId&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="c1"&gt;// Start the application&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3030&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="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="s2"&gt;[INFO] Server listening on port 3030&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prepare API:&lt;/strong&gt; Prepare a transaction with &lt;code&gt;insert&lt;/code&gt; operation in &lt;code&gt;students&lt;/code&gt; table. And returns the transaction name as &lt;code&gt;txId&lt;/code&gt; in response.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;--location&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:3030/prepare'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--header&lt;/span&gt; &lt;span class="s1"&gt;'Content-Type: application/json'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--data&lt;/span&gt; &lt;span class="s1"&gt;'{
  "name": "Nahid Chowdhury",
  "cgpa": "3.9"
}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Commit API:&lt;/strong&gt; Commit a staged transaction with transaction name (txId).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;--location&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:3030/commit'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--header&lt;/span&gt; &lt;span class="s1"&gt;'Content-Type: application/json'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--data&lt;/span&gt; &lt;span class="s1"&gt;'{
    "txId": "7b8a1e6bc0"
}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rollback API:&lt;/strong&gt; Rollback a staged transaction with transaction name (txId).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;--location&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:3030/rollback'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--header&lt;/span&gt; &lt;span class="s1"&gt;'Content-Type: application/json'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--data&lt;/span&gt; &lt;span class="s1"&gt;'{
    "txId": "7b8a1e6bc0"
}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Github Repo:&lt;/strong&gt; &lt;a href="https://github.com/ovichowdhury/node-pg-2pc" rel="noopener noreferrer"&gt;node-pg-2pc&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Two-Phase Commit (2PC) in PostgreSQL is a critical protocol for ensuring data consistency and integrity in distributed systems. By coordinating transaction preparation and finalization across multiple participants, 2PC provides a robust mechanism to manage complex transactions spanning multiple databases or nodes. While its implementation requires careful consideration of performance and failure scenarios, it remains an invaluable tool for systems demanding strong consistency.&lt;/p&gt;

&lt;p&gt;Thank you for reading! If you have any questions or suggestions, feel free to reach out.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>.NET Development Essentials on macOS with VS Code</title>
      <dc:creator>Nahid Chowdhury</dc:creator>
      <pubDate>Sun, 29 Dec 2024 13:22:05 +0000</pubDate>
      <link>https://dev.to/ovichowdhury/net-development-essentials-on-macos-with-vs-code-1c5h</link>
      <guid>https://dev.to/ovichowdhury/net-development-essentials-on-macos-with-vs-code-1c5h</guid>
      <description>&lt;p&gt;.NET 9 continues the tradition of Microsoft's commitment to performance, reliability, and developer productivity. With every iteration, .NET has gained features and optimizations that make it one of the leading platforms for modern application development. But unfortunately the most powerful IDE of .NET development Visual Studio isn't available on macOS.&lt;/p&gt;

&lt;p&gt;Microsoft officially announced that they will no longer support Visual Studio for macOS and made VS Code the de-facto standard IDE for cross platform C# development. According to Microsoft official statement -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"While we have retired Visual Studio for Mac, we remain committed to our developers on Mac and .NET MAUI with alternatives like the C# Dev Kit for Visual Studio Code and other extensions you can use to take advantage of our ongoing investments in .NET development."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While VS Code with C# Dev Kit is a great choice but Visual Studio developers may need some guidance to effectively use VS Code with the C# Dev Kit. This blog post aims to provide a comprehensive guide to making .NET development on macOS feel like a native experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install C# Extensions in VS Code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fkmw6xoe7k45mar72dk8q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkmw6xoe7k45mar72dk8q.png" alt="Extensions Image" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are some VS Code extensions to enhance your C# and .NET development experience.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;C# Dev Kit&lt;/li&gt;
&lt;li&gt;C# - Base Language Support&lt;/li&gt;
&lt;li&gt;IntelliCode for C# Dev Kit&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After installing the extensions the command palette of VS Code is supercharged with .NET related commands i.e. Create a New Project, Opening an Existing Solution, etc. So lets inspect the changes with (Command + Shift + P) and type .NET in the command palette.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbbmbx59ge1k94lpxpd7o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbbmbx59ge1k94lpxpd7o.png" alt="Command Palette Image" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although the command palette offers a way to manage projects and solutions, using the .NET CLI directly can boost developer productivity.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Concept of Solution and Project in .NET
&lt;/h2&gt;

&lt;p&gt;In the .NET ecosystem, Solution and Project are foundational organizational concepts used within Visual Studio and other .NET development environments. They help developers manage the structure, dependencies, and workflow of software development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Project in .NET represents a single, self-contained unit of work or a collection of source files, references, and resources. It is essentially the building block of a .NET application. Defined by a .csproj or .vbproj file (depending on the language). For instance console project, web api project etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Solution is a container for organizing one or more related Projects. It provides a higher-level view of the application or system being developed. Defined by a .sln file and contains reference to multiple projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Simple representation of Solution and Project Hierarchy&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MySolution.sln
│
├── WebApp.csproj
│   ├── Controllers
│   ├── Views
│   └── Models
│
├── DataAccess.csproj
│   └── Repositories
│
└── Tests.csproj
    └── UnitTests

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Managing Development with .NET CLI
&lt;/h2&gt;

&lt;p&gt;Let's create an empty solution first so that we can add one or more projects later on. Create a folder and open the folder with VS Code. Inside the VS Code terminal run -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet new sln -n VsCodePlay&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I have named the solution VsCodePlay, and an empty solution has been created named "VsCodePlay.sln".&lt;/p&gt;

&lt;p&gt;Let's create a webapi project inside this solution with this command -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet new webapi -n Todo.API&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A webapi project with all the weatherforecast boilerplate has been created but it's still not part of VsCodePlay.sln, We can inspect that in Solution Explorer of VS Code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjcvb40f0ozwmtnhqczf2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjcvb40f0ozwmtnhqczf2.jpeg" alt="State of the project" width="800" height="500"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So run this command to add a project into a solution, The command should be run inside the root folder of the solution. In out case VsCodePlay folder -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet sln VsCodePlay.sln add Todo.API/Todo.API.csproj&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I've included a second console application in the solution. The Solution Explorer now reflects this addition, as shown below. You can manage the application from the Solution Explorer alike Visual Studio. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvqn6v3iw49sh6ep4u480.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvqn6v3iw49sh6ep4u480.png" alt="Solution Explorer" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You've already noticed that &lt;code&gt;dotnet new&lt;/code&gt; command can create the boilerplate for you with just a single command. For inspecting the available project types in .NET you can run -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet new list&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzt484n8fdrte23k424sl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzt484n8fdrte23k424sl.png" alt="Available Projects" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the .NET CLI, you can quickly generate a fully functional, basic project structure (boilerplate) for various project types, simply by using their short names.&lt;/p&gt;

&lt;p&gt;We are now well-prepared to organize our project using the .NET CLI. Let’s explore some essential commands that will help streamline and manage our development process effectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run the project in development mode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet run&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you don't want to restart the server manually for reflecting file changes then run the project in watch mode, It will continuously watch the file changes and automatically restart the server. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet watch run&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install a Nuget Package&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet add package Newtonsoft.Json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remove a Nuget Package&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet remove package Newtonsoft.Json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building a project for debugging&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cleaning an existing build&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet clean&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Publishing a project as DLL in production&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet publish "./Todo.API.csproj" -c Release -o ./publish&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Publishing an entire solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet publish -c Release&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command needs to be executed at the solution level.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run the project in production mode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; dotnet ./publish/Todo.API.dll&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;VS Code, complemented by essential extensions and the .NET CLI, is a powerful tool for cross-platform C# application development. The insights shared in this post can help developers embrace .NET development on macOS, unlocking opportunities for innovation and project success.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>vscode</category>
      <category>cli</category>
      <category>extensions</category>
    </item>
    <item>
      <title>Mount EBS Volume with EC2 Linux Instance</title>
      <dc:creator>Nahid Chowdhury</dc:creator>
      <pubDate>Thu, 25 Jan 2024 10:44:15 +0000</pubDate>
      <link>https://dev.to/ovichowdhury/mount-ebs-volume-with-ec2-linux-instance-17j2</link>
      <guid>https://dev.to/ovichowdhury/mount-ebs-volume-with-ec2-linux-instance-17j2</guid>
      <description>&lt;p&gt;Amazon Elastic Block Store (EBS) is an easy-to-use, scalable, high-performance block-storage service designed to work with EC2 instances. Unlike the instance store volume that comes with an EC2 instance, which is ephemeral and gets deleted when the instance is terminated, EBS volumes are independent of the instance and persist even after the instance is terminated. This means that any data written to the EBS volume will be preserved and can be accessed when the instance is restarted or launched again. There are several reasons why you might want to attach a separate EBS volume to an EC2 instance. Some of them are,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Data Persistence&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Scalability and flexibility&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Performance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this blog post, we will learn about mounting an EBS instance in a Ubuntu Linux machine.&lt;/p&gt;

&lt;p&gt;After attaching an EBS volume with a running EC2 instance, you can check it using the list block command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; lsblk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fem0fgwb62sawczvwmgx3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fem0fgwb62sawczvwmgx3.png" alt="Image" width="656" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that create a mount point in the &lt;code&gt;/mnt&lt;/code&gt; folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; sudo mkdir -p /mnt/db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;/mnt/db&lt;/code&gt; folder will be used for mounting the volume. So let's create the &lt;code&gt;ext4&lt;/code&gt; file system in the newly attached volume &lt;code&gt;xvdf&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; sudo mkfs -t ext4 /dev/xvdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fayst4fob8am7aivgjecz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fayst4fob8am7aivgjecz.png" alt="Image" width="704" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now our file system is ready in the volume &lt;code&gt;/dev/xvdf&lt;/code&gt;. For mounting it with &lt;code&gt;/mnt/db&lt;/code&gt; we just need to use the mount command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; sudo mount /dev/xvdf /mnt/db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations you have successfully mounted the volume and it's ready to use. For checking it you can use &lt;code&gt;lsblk&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdjdaswmz3lyir3dkvg9i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdjdaswmz3lyir3dkvg9i.png" alt="Image" width="603" height="229"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For initializing the attached volume after an instance reboot/restart we can use the Linux file systems table &lt;code&gt;/etc/fstab&lt;/code&gt;. Open the file &lt;code&gt;/etc/fstab&lt;/code&gt; using &lt;code&gt;nano&lt;/code&gt; and add the below-mentioned line at the end of the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/dev/xvdf   /mnt/db   ext4   defaults,nofail   0   2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save &amp;amp; Close the file. Enjoy!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>linux</category>
      <category>ec2</category>
      <category>ebs</category>
    </item>
    <item>
      <title>Generating Dynamic Breadcrumb Menus Using Tree Table &amp; Recursive CTE</title>
      <dc:creator>Nahid Chowdhury</dc:creator>
      <pubDate>Sat, 02 Dec 2023 15:19:34 +0000</pubDate>
      <link>https://dev.to/ovichowdhury/generating-dynamic-breadcrumb-menus-using-tree-table-recursive-cte-59i9</link>
      <guid>https://dev.to/ovichowdhury/generating-dynamic-breadcrumb-menus-using-tree-table-recursive-cte-59i9</guid>
      <description>&lt;p&gt;It's a quite common pattern to store application navigation information in the database to achieve flexibility and control over generating dynamic menus based on roles. Many enterprise applications including large Core Banking Solutions with thousands of hierarchical navigation information store hierarchical data in a database using &lt;strong&gt;Tree Data Structure&lt;/strong&gt;. However, there are a few different approaches that can be used to effectively store and manage tree data in an RDBMS. One of the common models is the &lt;strong&gt;Adjacency List Model&lt;/strong&gt;. The adjacency list model is a straightforward approach to representing trees in an RDBMS. It involves storing each node in a separate row in a table, along with a reference to its parent node.&lt;/p&gt;

&lt;p&gt;So In this blog post, we will learn about extracting the ancestor data of a menu item from RDBMS so that we can visualize the Breadcrumb Menu in the user interface. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fm56qzd8z84okgl5u4l94.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fm56qzd8z84okgl5u4l94.png" alt="Breadcrumb Menu Example" width="755" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
    &lt;em&gt;Example of a Breadcrumb Menu&lt;/em&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Postgres database installed in Local / Cloud machine.&lt;/li&gt;
&lt;li&gt;PgAdmin for executing the query and visualizing the result.&lt;/li&gt;
&lt;li&gt;Prior knowledge of Recursive CTE will be helpful.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Preparing the Data
&lt;/h2&gt;

&lt;p&gt;Let's create the Navigations Tree Table first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parent_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_on&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="n"&gt;NOW&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&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 are storing &lt;strong&gt;id&lt;/strong&gt; as a primary key and &lt;strong&gt;parent_id&lt;/strong&gt; as a foreign key referencing the primary key of another row in the same table. &lt;/p&gt;

&lt;p&gt;Our table is ready now. So let's insert some dummy data for navigation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'User'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Role'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'User Reports'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'User'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Daily Reports'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'User Reports'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'User Activity'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Daily Reports'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Role Reports'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Role'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the successful execution of Insert Statements, we will have our tree structure ready for running the Recursive CTE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7adg9w25mo7xqkc5oc8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7adg9w25mo7xqkc5oc8j.png" alt="Navigations Table" width="571" height="191"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;
    &lt;em&gt;Navigations Table&lt;/em&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  About Recursive CTE
&lt;/h2&gt;

&lt;p&gt;A recursive common table expression (CTE) is a powerful tool for performing recursive queries in SQL. It allows you to traverse hierarchical data structures, such as trees or graphs, and perform operations on the data at each level of the hierarchy.&lt;/p&gt;

&lt;p&gt;In Postgres, Recursive CTE can be defined using the &lt;code&gt;WITH RECURSIVE&lt;/code&gt; clause. It consists of mainly two parts &lt;strong&gt;Anchor Query&lt;/strong&gt; and &lt;strong&gt;Recursive Query&lt;/strong&gt; combined by the &lt;code&gt;UNION&lt;/code&gt; clause. &lt;/p&gt;

&lt;p&gt;So let's print 1 to 10 using Recursive CTE and learn its building blocks without spending more time on theory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;count&lt;/span&gt; &lt;span class="c1"&gt;-- Anchor query return 1 (R0)&lt;/span&gt;
    &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="c1"&gt;-- Combine R0 ... Rn&lt;/span&gt;
    &lt;span class="c1"&gt;-- Recursive Query increment count by 1&lt;/span&gt;
    &lt;span class="c1"&gt;-- Until its value reaches the limit / termination statement&lt;/span&gt;
    &lt;span class="c1"&gt;-- (R1 ... Rn)&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- (Output: 1,2,3,4 ... 10)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Preparing the query for Breadcrumb Menu
&lt;/h2&gt;

&lt;p&gt;So let's discuss the content of the Breadcrumb Menu. After clicking any menu item the user is usually routed to a new page/component. The Breadcrumb menu will visualize the path meaning the ancestor tree of a particular node. For instance, if a user clicks on the &lt;code&gt;User Activity&lt;/code&gt; menu item the breadcrumb menu will display -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User &amp;gt; User Reports &amp;gt; Daily Reports &amp;gt; User Activity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simple Recursive CTE can be used to extract data for equivalent output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;cte_get_parents&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;-- Anchor Query&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; 
        &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;parent_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;created_on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;level&lt;/span&gt;
    &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt;
    &lt;span class="c1"&gt;-- Title will be changed based on the user's click&lt;/span&gt;
    &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'User Activity'&lt;/span&gt;

    &lt;span class="k"&gt;UNION&lt;/span&gt;

    &lt;span class="c1"&gt;-- Recursive Query&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; 
        &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parent_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;created_on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;level&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;navigations&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;cte_get_parents&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parent_id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;parent_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;created_on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="k"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
             &lt;span class="k"&gt;ABS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;MIN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;level&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
             &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;cte_get_parents&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;level&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;cte_get_parents&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanations&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Anchor query returns the result of the node that we want to extract the ancestors. It also bootstraps the value of &lt;code&gt;level&lt;/code&gt; so that we can track the depth of each node in the tree.&lt;/li&gt;
&lt;li&gt;The recursive query takes the result of the anchor query and iterates the rows until &lt;code&gt;parent_id&lt;/code&gt; becomes &lt;code&gt;null&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;As we are matching the &lt;strong&gt;ID&lt;/strong&gt; of the table to the &lt;strong&gt;Parent ID&lt;/strong&gt; of the CTE (&lt;code&gt;ON n.id = c.parent_id&lt;/code&gt;), We are traversing the table bottom-up - One step towards the root after each iteration. &lt;/li&gt;
&lt;li&gt;The recursive query terminates when the &lt;code&gt;parent_id&lt;/code&gt; of an ancestor node becomes &lt;code&gt;null&lt;/code&gt; (because no rows with the id &lt;code&gt;null&lt;/code&gt; in the table). In our case the root node.&lt;/li&gt;
&lt;li&gt;After termination of the recursive statement &lt;code&gt;UNION&lt;/code&gt; will merge the results of Anchor and Recursive Statements.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;SELECT&lt;/code&gt; statement after the CTE expression will format the result in our desired format accessing the CTE result. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Final Result
&lt;/h2&gt;

&lt;p&gt;After executing the Recursive CTE we will get the desired output. Thus, We can expose an API on top of it and visualize the result using a Breadcrumb UI component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzem6uiiplubon1ehsdho.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzem6uiiplubon1ehsdho.png" alt="Recursive CTE Result" width="633" height="142"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
    &lt;em&gt;Output of the Recursive CTE&lt;/em&gt;
&lt;/p&gt;




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

&lt;p&gt;Tree tables are a fundamental data structure for representing hierarchical data in relational databases. They provide an efficient and organized way to store and manage parent-child relationships within a data model. Recursive CTEs (Common Table Expressions) emerged as a powerful tool for traversing and manipulating tree data structures in RDBMS. By leveraging the recursive nature of CTEs, users can effectively query and manipulate hierarchical data, enabling them to extract meaningful insights from complex data relationships. &lt;/p&gt;

&lt;p&gt;From Navigations to Chart-of-Accounts, there are endless use cases of hierarchical data processing using Recursive CTEs.&lt;/p&gt;

&lt;p&gt;Happy Coding!!!&lt;/p&gt;

</description>
      <category>cte</category>
      <category>sql</category>
      <category>breadcrumb</category>
      <category>tree</category>
    </item>
  </channel>
</rss>
