<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Farzad Mohebi</title>
    <description>The latest articles on Forem by Farzad Mohebi (@farzad_fm).</description>
    <link>https://forem.com/farzad_fm</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%2F521724%2Fd27bae6b-c041-41e0-9ee7-06ea3392a19b.png</url>
      <title>Forem: Farzad Mohebi</title>
      <link>https://forem.com/farzad_fm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/farzad_fm"/>
    <language>en</language>
    <item>
      <title>From Messy Code to Clean Architecture: How I Finally Organized My Backend Projects</title>
      <dc:creator>Farzad Mohebi</dc:creator>
      <pubDate>Mon, 15 Sep 2025 20:14:36 +0000</pubDate>
      <link>https://forem.com/farzad_fm/from-messy-code-to-clean-architecture-how-i-finally-organized-my-backend-projects-381g</link>
      <guid>https://forem.com/farzad_fm/from-messy-code-to-clean-architecture-how-i-finally-organized-my-backend-projects-381g</guid>
      <description>&lt;p&gt;&lt;em&gt;The story of how I went from spaghetti code to a maintainable, scalable backend architecture&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem That Kept Me Up at Night
&lt;/h2&gt;

&lt;p&gt;I used to be that developer. You know the one – writing everything in the controller, mixing business logic with data access, and creating dependencies so tangled that changing one line of code felt like defusing a bomb. &lt;/p&gt;

&lt;p&gt;My projects worked, but they were nightmares to maintain. Adding new features was painful, testing was nearly impossible, and don't even get me started on trying to explain my code to teammates.&lt;/p&gt;

&lt;p&gt;Then I discovered Clean Architecture, and everything changed.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Game-Changing Patterns I Learned
&lt;/h2&gt;

&lt;p&gt;After rebuilding my latest school management system using proper architectural patterns, I want to share the exact approach that transformed my development process. Think of it like renovating a house – instead of having everything crammed into one room, I learned to create proper rooms for different purposes.&lt;/p&gt;

&lt;p&gt;Here are the key patterns that made all the difference:&lt;/p&gt;

&lt;h3&gt;
  
  
  🏛️ Clean Architecture (The Foundation)
&lt;/h3&gt;

&lt;p&gt;This became my north star. The golden rule is simple but powerful: &lt;strong&gt;all dependencies point inward&lt;/strong&gt;. The core business logic never knows about databases, APIs, or external services.&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Domain Layer&lt;/strong&gt;: The brain (pure business logic)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application Layer&lt;/strong&gt;: The nervous system (orchestrates use cases)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infrastructure Layer&lt;/strong&gt;: The hands and feet (handles external concerns)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Layer&lt;/strong&gt;: The face (communicates with the outside world)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🤝 CQRS + Mediator Pattern
&lt;/h3&gt;

&lt;p&gt;Imagine your application as a busy restaurant. Before CQRS, I had one overworked waiter (my controllers) trying to take orders, cook food, serve dishes, and handle payments. With CQRS, I separated the kitchen staff – some specialize in reading the menu to customers (Queries), others focus on preparing orders (Commands).&lt;/p&gt;

&lt;p&gt;The Mediator pattern is like having a head waiter who delegates tasks instead of letting customers shout directly at the kitchen staff:&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;// Clean, focused handlers - like specialized kitchen staff&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;GetStudentListQuery&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IRequest&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;StudentDto&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;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;CreateStudentCommand&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IRequest&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;StudentDto&lt;/span&gt;&lt;span class="p"&gt;&amp;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;Using MediatR meant my controllers became like a polite receptionist – they just take the request and pass it to the right department!&lt;/p&gt;

&lt;h3&gt;
  
  
  📦 Repository + Unit of Work
&lt;/h3&gt;

&lt;p&gt;Think of the Repository pattern like a librarian. Instead of letting everyone rummage through the book stacks (database) directly, the librarian provides a clean interface: "I need information about students" and they know exactly where to find it.&lt;/p&gt;

&lt;p&gt;The Unit of Work is like a shopping cart – you collect all your changes, and then either checkout everything successfully or abandon the cart entirely. No more half-completed transactions corrupting your data!&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;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetByIdAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&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;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetAllAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// ... other operations&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ✉️ DTOs (Data Transfer Objects)
&lt;/h3&gt;

&lt;p&gt;DTOs are like having a translator at the border between countries. Your internal entities speak "Database Language," but your API needs to speak "Frontend Language." The DTO translator ensures everyone understands each other without forcing anyone to change their native tongue.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Clean Architecture Structure Tree
&lt;/h2&gt;

&lt;p&gt;Here's the actual project structure I implemented – it's like having a well-organized office building where each floor has a specific purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;📁 CTN_School (The Building)
├── 📁 src (Main Floors)
│   ├── 🌐 Api (Reception Floor - 1st Contact)
│   │   ├── 📁 Controllers (Reception Desks)
│   │   ├── 📁 Extensions (Office Utilities)  
│   │   ├── 📁 Hubs (Communication Center)
│   │   ├── 📁 Services (Front Desk Services)
│   │   └── 📄 Program.cs (Building Manager)
│   │
│   ├── 🧠 Core (Executive Floors - The Brains)
│   │   ├── 📁 CTN_School.Application (Management Floor)
│   │   │   ├── 📁 Features (Department Offices)
│   │   │   │   ├── 📁 Auth (Security Department)
│   │   │   │   ├── 📁 Classes (Academic Department)  
│   │   │   │   ├── 📁 Grades (Assessment Department)
│   │   │   │   ├── 📁 Students (Student Affairs)
│   │   │   │   │   ├── 📁 Commands (Action Items)
│   │   │   │   │   ├── 📁 Notifications (Communication)
│   │   │   │   │   └── 📁 Queries (Information Requests)
│   │   │   │   └── 📁 Teachers (Faculty Department)
│   │   │   └── 📁 Common (Shared Resources)
│   │   │
│   │   └── 📁 CTN_School.Domain (CEO Floor - Core Rules)
│   │       ├── 📁 Entities (The Company Constitution)
│   │       └── 📁 Interfaces (Department Contracts)
│   │
│   ├── 🔧 Infrastructure (Basement - Technical Services)
│   │   ├── 📁 Migrations (Building Renovations)
│   │   ├── 📁 Persistence (Document Storage)
│   │   └── 📁 Search (Information Retrieval)
│   │
│   └── 🧪 tests (Quality Assurance Floor)
       └── 📁 CTN_School.Application.UnitTests
           └── 📁 Features (Department Testing)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Layer-by-Layer Breakdown
&lt;/h2&gt;

&lt;p&gt;Let me walk you through how I organized everything:&lt;/p&gt;

&lt;h3&gt;
  
  
  Domain Layer (The Core) - &lt;em&gt;The CEO's Office&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This is like the CEO's corner office where all the important business decisions are made. Everyone respects these rules, but the CEO doesn't worry about implementation details:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entities&lt;/strong&gt;: The company's core assets - &lt;code&gt;Student.cs&lt;/code&gt;, &lt;code&gt;Teacher.cs&lt;/code&gt;, &lt;code&gt;Grade.cs&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interfaces&lt;/strong&gt;: The contracts that everyone must follow - &lt;code&gt;IRepository&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;IUnitOfWork&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Business Rules&lt;/strong&gt;: The fundamental "what" that never changes, regardless of technology&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Zero dependencies&lt;/em&gt; - The CEO doesn't need to know about databases or websites to make business decisions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Application Layer (The Orchestrator) - &lt;em&gt;The Management Team&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Like department managers who coordinate between the CEO's vision and the workers on the ground:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CQRS Handlers&lt;/strong&gt;: Each manager specializes in specific tasks (GetStudentListQuery, CreateStudentCommand)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DTOs&lt;/strong&gt;: The formatted reports managers create for different audiences&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validation&lt;/strong&gt;: The quality control checklists (FluentValidation rules)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Service Interfaces&lt;/strong&gt;: The contracts with external vendors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Depends only on Domain&lt;/em&gt; - Managers take orders from the CEO but don't get their hands dirty with implementation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Infrastructure Layer (The Implementation) - &lt;em&gt;The Workers &amp;amp; Tools&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Like the maintenance crew, IT department, and administrative staff who make everything work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Access&lt;/strong&gt;: The filing system (EF Core context, repositories, migrations)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;External Services&lt;/strong&gt;: Connections to outside vendors (Redis, email services)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concrete Implementations&lt;/strong&gt;: The actual tools and processes that get work done&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Depends on Application&lt;/em&gt; - Workers follow the managers' specifications.&lt;/p&gt;

&lt;h3&gt;
  
  
  API Layer (The Gateway) - &lt;em&gt;The Reception Desk&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Like the friendly receptionist who greets visitors and directs them to the right department:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Controllers&lt;/strong&gt;: The reception desks that handle specific types of visitors&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration&lt;/strong&gt;: The office policies and security systems (JWT, CORS, SignalR)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web-specific Services&lt;/strong&gt;: The tools receptionists use (JWT tokens, real-time updates)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Depends on both Application and Infrastructure&lt;/em&gt; - Receptionists need to know company policies and have access to all departments.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Dependency Injection Magic ✨
&lt;/h2&gt;

&lt;p&gt;Instead of a messy &lt;code&gt;Program.cs&lt;/code&gt; that looked like a junk drawer, I created extension methods. Think of it like having specialized teams handle different aspects of setting up the office:&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;// Clean and organized - like having department managers handle their own setup&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="nf"&gt;AddApplicationServices&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;// Management team setup&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="nf"&gt;AddInfrastructureServices&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// IT and maintenance setup&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="nf"&gt;AddApiServices&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;            &lt;span class="c1"&gt;// Reception and security setup&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each layer organizes its own services, like departments managing their own budgets and resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Changed for Me
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Before Clean Architecture:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;😰 Scared to make changes&lt;/li&gt;
&lt;li&gt;🐛 Bug fixes breaking other features
&lt;/li&gt;
&lt;li&gt;🧪 Testing was a nightmare&lt;/li&gt;
&lt;li&gt;📈 Technical debt growing daily&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;After Clean Architecture:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Confident refactoring&lt;/li&gt;
&lt;li&gt;🎯 Features are isolated and focused&lt;/li&gt;
&lt;li&gt;🚀 Easy to test individual components&lt;/li&gt;
&lt;li&gt;📚 New team members understand the codebase quickly&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Bottom Line
&lt;/h2&gt;

&lt;p&gt;Clean Architecture isn't just about following patterns – it's about creating code that you'll thank yourself for writing six months later. Yes, it requires more initial setup, but the payoff in maintainability and scalability is enormous.&lt;/p&gt;

&lt;p&gt;Your future self (and your teammates) will thank you for taking the time to structure your projects properly from the start.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;What architectural patterns have saved your sanity? Drop a comment below – I'd love to hear about your experiences with Clean Architecture or other patterns that have transformed your development workflow!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>cleanarchitecture</category>
      <category>dotnet</category>
      <category>backend</category>
      <category>designpatterns</category>
    </item>
  </channel>
</rss>
