Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Clean Architecture with .NET
  • Table Of Contents Toc
Clean Architecture with .NET

Clean Architecture with .NET

By : Casey Crouse, Steve "Ardalis" Smith
1 (1)
close
close
Clean Architecture with .NET

Clean Architecture with .NET

1 (1)
By: Casey Crouse, Steve "Ardalis" Smith

Overview of this book

Learn how to apply Clean Architecture in real-world .NET applications with Azure in this guide. Designed around an e-commerce project - Project Odyssey, you’ll design and build every architectural layer, from core domain logic and application orchestration to infrastructure and presentation. This book equips you to apply DDD, CQRS, MediatR, and dependency injection, enabling loosely coupled, testable systems. You’ll build reusable and modular code structures that scale with your application’s complexity and learn how to persist data using EF Core with a code-first approach, managing schema evolution with migrations while keeping infrastructure concerns isolated from your domain logic. Learn how to develop secure systems by integrating Azure External ID, Azure Key Vault, and application-level authentication and authorization. Gain practical experience with Blazor Server, Razor components, and MudBlazor UI, all integrated within a layered architecture. Learn to enforce business logic boundaries, reduce boilerplate with generic request handlers, and use real-world use cases like payment processing and shopping cart management. Whether you're new to Clean Architecture or scaling up enterprise solutions, this book provides the patterns, tools, and mindset to succeed. *Email sign-up and proof of purchase required
Table of Contents (23 chapters)
close
close
Lock Free Chapter
1
Part 1: Introduction and Overview of Clean Architecture
4
Part 2: Design and Implementation of Clean Architecture
12
Part 3: Advanced Topics in Clean Architecture
17
Part 4: Additional Topics and Resources
21
Other Books You May Enjoy
22
Index

Preface

Clean Architecture is one of those ideas that tends to stick with you once it clicks.

I was first introduced to it around 2017, early in the .NET Core days. I remember seeing how Jason Taylor was structuring his projects and comparing that to the state of the codebases I had been working in at the time. The difference was immediate. Where I was used to seeing tightly coupled, hard-to-navigate code, this approach brought clarity. Responsibilities were separated. The structure made sense. It was what Clean Architecture describes as "screaming architecture"—the design made the intent of the system obvious.

I was already familiar with Robert C. Martin's work, so diving deeper into Clean Architecture felt like a natural next step. Over the years, I applied these ideas across multiple projects and teams. In many cases, the results were exactly what you'd hope for—cleaner code, clearer intent, and teams that could move faster with more confidence.

But I also saw the other side of it.

I worked on teams where Clean Architecture became a source of friction. Where too much ceremony slowed things down. Where developers debated structure more than they delivered value. I saw codebases where the architecture was applied in ways that didn't fit the problem, and others where it was abandoned altogether.

Those experiences shaped how I think about architecture today.

To me, Clean Architecture isn't just a pattern—it's a collection of well-established engineering practices, organized in a way that emphasizes separation of concerns, consistency, and long-term maintainability. It's designed to help systems last. But like any approach, it comes with tradeoffs. It introduces structure, and with that structure comes overhead. If applied blindly, it can become just as problematic as the chaos it's meant to prevent.

This book is my attempt to strike that balance.

Rather than focusing on theory, the goal here is to show how Clean Architecture can be applied in a practical, real-world .NET application. You'll see how a system evolves, how decisions are made, and how to adapt the architecture as requirements change. Just as importantly, you'll learn when it makes sense to apply these patterns—and when it might not.

By the end, you should not only understand why Clean Architecture is widely adopted, but also why it's sometimes criticized—and how to navigate both sides. Even if you don't apply every layer or pattern shown here, the underlying principles—separation of concerns, clear boundaries, and adaptability—are what truly matter.

Writing this book was not easy.

Over the course of the past two and a half years, I experienced a number of personal and professional challenges, including the loss of both of my parents, periods of burnout, and the broader uncertainty that has come with shifts in the industry. There were times when progress slowed, and times when continuing required significantly more effort than I expected.

But finishing this book was important to me.

I wanted to make sure that what you're reading is not just complete, but thoughtful and intentional—something that reflects the care and discipline that Clean Architecture itself requires.

I'm also grateful to have worked alongside Steve "Ardalis" Smith as a co-author on this book. Steve introduces the core concepts and foundations of Clean Architecture in the opening chapters and helps bring everything full circle in the conclusion. His feedback throughout the process was invaluable and played a significant role in shaping the quality of this book.

If you take one thing away from this, I hope it's this:

Architecture is not about following rules—it's about making thoughtful decisions that help your system and your team succeed over time.

Who this book is for

This book is intended for developers who are already comfortable building applications and are ready to think more deeply about how those applications are structured.

If you're a mid-level developer looking to grow, or a senior engineer evaluating architectural approaches, this book will provide a practical and grounded perspective on applying Clean Architecture in real-world .NET systems.

What this book covers

This book takes a practical, end-to-end approach to applying Clean Architecture in a .NET application. Rather than focusing on theory alone, you'll follow the evolution of a real system—from tightly coupled beginnings to a scalable, maintainable architecture that can adapt over time.

Each chapter builds on the last, showing not just how to implement Clean Architecture, but how to evolve it, refine it, and sustain it in real-world scenarios.

Chapter 1, Understanding the Limitations of Tightly Coupled Architectures, explores the challenges of tightly coupled systems and sets the stage for why a more structured approach is needed.

Chapter 2, Introduction to Clean Architecture, introduces the core principles, components, and tradeoffs of Clean Architecture, including when it may not be the right choice.

Chapter 3, Adapting Clean Architecture to .NET, demonstrates how to structure a modern .NET application using Clean Architecture principles, including layering, cross-cutting concerns, and dependency management.

Chapter 4, Designing a Clean Architecture Application, walks through how to analyze requirements, define use cases, and design a system that aligns with Clean Architecture from the start.

Chapter 5, Implementing the Core Layer, focuses on building use cases and domain logic, along with testing strategies to ensure correctness and maintainability.

Chapter 6, Implementing the Infrastructure Layer, covers integrating external services, configuring dependency injection, and applying patterns like the Options pattern in a clean and testable way.

Chapter 7, Implementing the Persistence Layer, introduces data access using Entity Framework Core, along with strategies for mapping, configuration, and testing persistence concerns.

Chapter 8, Implementing the Presentation Layer with Identity, shows how to compose the application at the edge using Blazor Server, including authentication with Microsoft Entra External ID.

Chapter 9, Building Secure, Testable UI Components for Use Cases, focuses on implementing real application features in the UI, securing them with authorization policies, and testing them effectively.

Chapter 10, Improving Clean Architecture in .NET with Structured Service Composition, introduces a structured approach to dependency registration, improving clarity, ordering, and reuse across multiple application hosts.

Chapter 11, Securing a Clean Architecture Application, explores practical security implementations including multi-factor authentication, secret management, and secure logging practices.

Chapter 12, Reducing Boilerplate in Clean Architecture Applications, demonstrates how to simplify repetitive patterns using MediatR, FluentValidation, CQRS, and generic handlers while preserving clarity.

Chapter 13, Deploying a Clean Architecture Application in Azure, walks through deploying the application to Azure, including environment setup, configuration, and production readiness.

Chapter 14, Managing and Evolving a Clean Architecture Application, focuses on observability, extending the system with new presentation layers like Azure Functions, and maintaining architectural integrity as the system grows.

Chapter 15, Conclusion, reflects on the journey, reinforces key principles, and provides guidance for applying Clean Architecture in real-world projects moving forward.

To get the most out of this book

To get the most value from this book, you should have a working understanding of .NET and experience building applications with C#. Familiarity with concepts such as dependency injection, APIs, and basic architectural patterns will help you follow along more easily. While prior experience with Clean Architecture is not required, a willingness to think critically about structure, tradeoffs, and long-term maintainability will help you get the most out of the material.

Software/hardware covered in the book

Operating system requirements

Latest .NET SDK

Windows, macOS, or Linux

Visual Studio or Visual Studio Code

Windows, macOS, or Linux

PowerShell (for running scripts)

Windows, macOS, or Linux

Docker for Desktop

Windows, macOS, or Linux

Microsoft Azure

Cloud (Azure subscription required)

You can download the full source code for this book from the GitHub repository provided in the next section. Cloning the repository and following along with the examples is recommended to avoid issues that can arise from manually copying code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Clean-Architecture-with-.NET. If there's an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781805128533.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: "The IServiceCollection is used to register services such as AddOpenTelemetry in your application's startup configuration."

A block of code is set as follows:

public class Product
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int StockLevel { get; set; }
}

Any command-line input or output is written as follows:

dotnet build
dotnet run

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: "Register your services in the Application layer to maintain proper separation of concerns."

Warnings or important notes appear like this.

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book or have any general feedback, please email us at customercare@packt.com and mention the book's title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packt.com/submit-errata, click Submit Errata, and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packt.com/.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Clean Architecture with .NET
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon