SlideShare a Scribd company logo
Clean Architecture
with ASP.NET Core
STEVE SMITH
ARDALIS.COM | @ARDALIS | STEVE@DEVIQ.COM
DEVIQ.COM
Learn More After Today
1) Pluralsight
◦ N-Tier Apps with C# http://bit.ly/PS-NTier1
◦ Domain-Driven Design Fundamentals http://bit.ly/ddd-fundamentals
2) DevIQ
◦ ASP.NET Core Quick Start http://aspnetcorequickstart.com
3) Microsoft FREE eBook/Sample App
◦ eShopOnWeb eCommerce Sample https://ardalis.com/architecture-ebook
4) Contact me for mentoring/training for your company/team
Questions
HOPEFULLY YOU’LL KNOW THE ANSWERS WHEN WE’RE DONE
Why do we separate applications into multiple
projects?
What are some principles we can apply when
organizing our software modules?
How does the organization of our application’s
solution impact coupling?
What problems result from certain common
approaches?
How does Clean Architecture address these
problems?
How does ASP.NET Core help?
Principles
A BIT OF GUIDANCE
Clean architecture with asp.net core
Separation of Concerns
Avoid mixing different code responsibilities in the
same (method | class | project)
The Big Three™
Data Access
Business Rules and Domain Model
User Interface
Separation of Concerns
The Big Three™
Data Access
Business Rules and Domain Model
User Interface
Clean architecture with asp.net core
Single Responsibility
Works in tandem with Separation of Concerns
Classes should focus on a single responsibility – a
single reason to change.
Clean architecture with asp.net core
Following Don’t Repeat Yourself…
Refactor repetitive code into functions
Group functions into cohesive classes
Group classes into folders and namespaces by
 Responsibility
 Level of abstraction
 Etc.
Further group class folders into projects
More Grouping Options to Explore
Aggregates
Features
Bounded Contexts
Clean architecture with asp.net core
Invert (and inject) Dependencies
Both high level classes and implementation-detail classes should
depend on abstractions (interfaces).
Invert (and inject) Dependencies
Classes should follow Explicit Dependencies Principle:
Request all dependencies via their constructor
Make your types honest, not deceptive
Invert (and inject) Dependencies
Corollary: Abstractions/interfaces must be defined somewhere accessible by:
Low level implementation services
High level business services
User interface entry points
Make the right thing easy
and the wrong thing hard.
FORCE DEVELOPERS INTO A “PIT OF SUCCESS”
Make the right thing easy and the wrong thing hard.
UI classes shouldn’t depend directly on infrastructure classes
◦ How can we structure our solution to help enforce this?
Make the right thing easy and the wrong thing hard.
Business/domain classes shouldn’t depend on infrastructure classes
◦ How can our solution design help?
Make the right thing easy and the wrong thing hard.
Repetition of (query logic, validation logic, policies, error handling, anything) is a problem
◦ What patterns can we apply to make avoiding repetition easier
than copy/pasting?
“Classic” N-Tier
Architecture
OR N-LAYER
Layer
Layer
Layer
Source: MSDN Website, 2001
N-Tier Benefits
Code Reuse
Team
Segmentation
Better
Maintainability
(slightly)
Looser
Coupling
N-Tier Drawbacks
Transitive
Dependencies
(some)
Complexity
(still)
Tight
Coupling
Transitive Dependencies
DB
Data Access
Layer
Business Logic
Layer
User Interface
Layer
Everything
Depends on the database
Domain-Centric
Design
AND THE CLEAN ARCHITECTURE
Core
Business
Logic
Everything
Else
Domain Model
Not just business logic, but also:
A model of the problem space composed of Entities, Interfaces, Services, and
more.
Interfaces define contracts for working with domain objects
Everything in the application (including infrastructure and data access) depends
on these interfaces and domain objects
Clean Architecture
Onion Architecture
Hexagonal Architecture
Ports and Adapters
Clean architecture with asp.net core
Clean architecture with asp.net core
Clean Architecture “Rules”
1. You do not talk about Clean Architecture.
Clean Architecture “Rules”
1. You do not talk about Clean Architecture.
Clean Architecture “Rules”
The Application Core contains the Domain Model
Clean Architecture “Rules”
All projects depend on the Core project;
dependencies point inward toward this core
Clean Architecture “Rules”
Inner projects define interfaces;
outer projects implement them
Clean Architecture “Rules”
Avoid direct dependency on the Infrastructure project
(except from Integration Tests)
Clean Architecture Features
Framework Independent
◦ You can use this architecture with ASP.NET (Core), Java, Python, etc.
◦ It doesn’t rely on any software library or proprietary codebase.
Clean Architecture Features
Database Independent
◦ The vast majority of the code has no knowledge of persistence details.
◦ This knowledge may exist in just one class, in one project that no other project references.
Clean Architecture Features
UI Independent
◦ Only the UI project cares about the UI.
◦ The rest of the system is UI-agnostic.
Clean Architecture Features
Testable
◦ Apps built using this approach, and especially the core domain model and its business rules, are easy to
test.
Refactoring to a Clean Architecture
Best to start from a properly organized solution
◦ See http://github.com/ardalis/CleanArchitecture
Next-best: Start from an application consisting of just a single project
Most difficult: Large, existing investment in multi-layer architecture without
abstractions or DI
The Core Project
Minimal dependencies – none on Infrastructure.
What Goes in Core:
Interfaces
Entities Value Objects Aggregates
Domain
Services
Domain Events
Exceptions
Specifications
Event Handlers
The Infrastructure Project
All dependencies on out-of-process resources.
What Goes in Infrastructure:
Repositories
EF (Core)
DbContext
Web API
Clients
File System
Accessors
Email/SMS
Sending
Logging
Adapters
System Clock
Other
Services
Cached
Repositories
Interfaces
The Web Project
All dependencies on out-of-process resources.
What Goes in Web:
Controllers Views
ViewModels
Filters Binders
Other Services
Razor
Pages
ApiModels BindingModels
Tag/Html
Helpers
Or
Interfaces
Sharing Between Solutions:
Shared Kernel
Common Types May Be Shared Between Solutions. Will be referenced by Core project(s).
Ideally distributed as Nuget Packages.
What Goes in Shared Kernel:
Base Entity
Base Domain
Event
Base
Specification
Common
Exceptions
Common
Interfaces
Common Auth
e.g. User class
Common DI
Common
Logging
Common
Guard Clauses
Guard Clauses?
Simple checks for input that use common rules and exceptions.
Nuget Package: Ardalis.GuardClauses (https://github.com/ardalis/GuardClauses)
Example:
public void ProcessOrder(Order order)
{
Guard.Against.Null(order, nameof(order));
// process order here
}
Solution Structure – Clean Architecture
Web
Core
Infrastructure
Shared Kernel
Unit Tests
Functional
Tests
Integration
Tests
Typical (Basic) Folder Structure
What belongs in actions/handlers?
Controller Actions (or Page Handlers) should:
1) Accept task-specific types (ViewModel, ApiModel, BindingModel)
2) Perform and handle model validation (ideally w/filters)
3) “Do Work” (More on this in a moment)
4) Create any model type required for response (ViewModel, ApiModel, etc.)
5) Return an appropriate Result type (View, Page, Ok, NotFound, etc.)
“Do Work” – Option One
Repositories and Entities
1) Get entity from an injected Repository
2) Work with the entity and its methods.
3) Update the entity’s state using the Repository
Great for simple operations
Great for CRUD work
Requires mapping between web models and domain model within controller
Clean architecture with asp.net core
“Do Work” – Option Two
Work with an application service.
1) Pass ApiModel types to service
2) Service internally works with repositories and domain model types.
3) Service returns a web model type
Better for more complex operations
Application Service is responsible for mapping between web models and domain model.
Keeps controllers lightweight, and with fewer injected dependencies.
Clean architecture with asp.net core
“Do Work” – Option Three
Work with commands and a tool like Mediatr.
1) Use ApiModel types that represent commands (e.g. RegisterUser)
2) Send model-bound instance of command to handler using _mediator.Send()
No need to inject separate services to different controllers – Mediatr becomes only dependency.
Instantiate Appropriate Command
Resolve Command w/Model Binding
Code Walkthrough
Resources
Online Courses (Pluralsight and DevIQ)
• SOLID Principles of OO Design http://bit.ly/SOLID-OOP
• N-Tier Architecture in C# http://bit.ly/PS-NTier1 and http://bit.ly/PS-NTier2
• DDD Fundamentals http://bit.ly/ddd-fundamentals
• ASP.NET Core Quick Start http://aspnetcorequickstart.com/
• Weekly Dev Tips Podcast http://www.weeklydevtips.com/
• Microsoft Architecture eBook/sample http://aka.ms/WebAppArchitecture
Ad

More Related Content

What's hot (20)

A Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation SlidesA Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation Slides
thinkddd
 
Clean architecture
Clean architectureClean architecture
Clean architecture
Lieven Doclo
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
NSCoder Mexico
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
Araf Karsh Hamid
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
Mattia Battiston
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipClean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software Craftsmanship
Ivan Paulovich
 
Solid principles
Solid principlesSolid principles
Solid principles
Monica Rodrigues
 
Domain Driven Design: Zero to Hero
Domain Driven Design: Zero to HeroDomain Driven Design: Zero to Hero
Domain Driven Design: Zero to Hero
Fabrício Rissetto
 
Introducing OpenAPI Version 3.1
Introducing OpenAPI Version 3.1Introducing OpenAPI Version 3.1
Introducing OpenAPI Version 3.1
SmartBear
 
Domain Driven Design 101
Domain Driven Design 101Domain Driven Design 101
Domain Driven Design 101
Richard Dingwall
 
Domain Driven Design (DDD)
Domain Driven Design (DDD)Domain Driven Design (DDD)
Domain Driven Design (DDD)
Tom Kocjan
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Giovanni Scerra ☃
 
How To be a Backend developer
How To be a Backend developer    How To be a Backend developer
How To be a Backend developer
Ramy Hakam
 
Domain driven design
Domain driven designDomain driven design
Domain driven design
Mustafa Dağdelen
 
A Pattern Language for Microservices
A Pattern Language for MicroservicesA Pattern Language for Microservices
A Pattern Language for Microservices
Chris Richardson
 
Modelling a complex domain with Domain-Driven Design
Modelling a complex domain with Domain-Driven DesignModelling a complex domain with Domain-Driven Design
Modelling a complex domain with Domain-Driven Design
Naeem Sarfraz
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven Design
Andriy Buday
 
Globant development week / Micro frontend
Globant development week / Micro frontendGlobant development week / Micro frontend
Globant development week / Micro frontend
Globant
 
Clean Architecture Applications in Python
Clean Architecture Applications in PythonClean Architecture Applications in Python
Clean Architecture Applications in Python
Subhash Bhushan
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
NexThoughts Technologies
 
A Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation SlidesA Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation Slides
thinkddd
 
Clean architecture
Clean architectureClean architecture
Clean architecture
Lieven Doclo
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
Mattia Battiston
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipClean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software Craftsmanship
Ivan Paulovich
 
Domain Driven Design: Zero to Hero
Domain Driven Design: Zero to HeroDomain Driven Design: Zero to Hero
Domain Driven Design: Zero to Hero
Fabrício Rissetto
 
Introducing OpenAPI Version 3.1
Introducing OpenAPI Version 3.1Introducing OpenAPI Version 3.1
Introducing OpenAPI Version 3.1
SmartBear
 
Domain Driven Design (DDD)
Domain Driven Design (DDD)Domain Driven Design (DDD)
Domain Driven Design (DDD)
Tom Kocjan
 
How To be a Backend developer
How To be a Backend developer    How To be a Backend developer
How To be a Backend developer
Ramy Hakam
 
A Pattern Language for Microservices
A Pattern Language for MicroservicesA Pattern Language for Microservices
A Pattern Language for Microservices
Chris Richardson
 
Modelling a complex domain with Domain-Driven Design
Modelling a complex domain with Domain-Driven DesignModelling a complex domain with Domain-Driven Design
Modelling a complex domain with Domain-Driven Design
Naeem Sarfraz
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven Design
Andriy Buday
 
Globant development week / Micro frontend
Globant development week / Micro frontendGlobant development week / Micro frontend
Globant development week / Micro frontend
Globant
 
Clean Architecture Applications in Python
Clean Architecture Applications in PythonClean Architecture Applications in Python
Clean Architecture Applications in Python
Subhash Bhushan
 

Similar to Clean architecture with asp.net core (20)

Elements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstElements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Enea Gabriel
 
L02 Architecture
L02 ArchitectureL02 Architecture
L02 Architecture
Ólafur Andri Ragnarsson
 
Isset Presentation @ EECI2009
Isset Presentation @ EECI2009Isset Presentation @ EECI2009
Isset Presentation @ EECI2009
Isset Internet Professionals
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
Harsh Jegadeesan
 
Azure presentation nnug dec 2010
Azure presentation nnug  dec 2010Azure presentation nnug  dec 2010
Azure presentation nnug dec 2010
Ethos Technologies
 
Over view of software artitecture
Over view of software artitectureOver view of software artitecture
Over view of software artitecture
ABDEL RAHMAN KARIM
 
The Magic Of Application Lifecycle Management In Vs Public
The Magic Of Application Lifecycle Management In Vs PublicThe Magic Of Application Lifecycle Management In Vs Public
The Magic Of Application Lifecycle Management In Vs Public
David Solivan
 
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
GlobalLogic Ukraine
 
Clean architecture with asp.net core by Ardalis
Clean architecture with asp.net core by ArdalisClean architecture with asp.net core by Ardalis
Clean architecture with asp.net core by Ardalis
Steven Smith
 
A Lightweight MDD Process Applied in Small Projects
A Lightweight MDD Process Applied in Small ProjectsA Lightweight MDD Process Applied in Small Projects
A Lightweight MDD Process Applied in Small Projects
Gabor Guta
 
Software Architecture for Agile Development
Software Architecture for Agile DevelopmentSoftware Architecture for Agile Development
Software Architecture for Agile Development
Hayim Makabee
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
Up2 Technology
 
Twelve Factor - Designing for Change
Twelve Factor - Designing for ChangeTwelve Factor - Designing for Change
Twelve Factor - Designing for Change
Eric Wyles
 
Best practices for creating modular Web applications
Best practices for creating modular Web applicationsBest practices for creating modular Web applications
Best practices for creating modular Web applications
peychevi
 
Yemo_Capstone_MS_Fairfield University
Yemo_Capstone_MS_Fairfield UniversityYemo_Capstone_MS_Fairfield University
Yemo_Capstone_MS_Fairfield University
Guillermo Julca
 
Gk1051 001 j2-ee_arch_tt425v1.1
Gk1051 001 j2-ee_arch_tt425v1.1Gk1051 001 j2-ee_arch_tt425v1.1
Gk1051 001 j2-ee_arch_tt425v1.1
vciampa
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdf
Kamal Acharya
 
Actively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net DeveloperActively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net Developer
Karthik Reddy
 
Actively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net DeveloperActively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net Developer
Karthik Reddy
 
ASAS 2014 - Simon Brown
ASAS 2014 - Simon BrownASAS 2014 - Simon Brown
ASAS 2014 - Simon Brown
Avisi B.V.
 
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstElements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Enea Gabriel
 
Azure presentation nnug dec 2010
Azure presentation nnug  dec 2010Azure presentation nnug  dec 2010
Azure presentation nnug dec 2010
Ethos Technologies
 
Over view of software artitecture
Over view of software artitectureOver view of software artitecture
Over view of software artitecture
ABDEL RAHMAN KARIM
 
The Magic Of Application Lifecycle Management In Vs Public
The Magic Of Application Lifecycle Management In Vs PublicThe Magic Of Application Lifecycle Management In Vs Public
The Magic Of Application Lifecycle Management In Vs Public
David Solivan
 
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
GlobalLogic Ukraine
 
Clean architecture with asp.net core by Ardalis
Clean architecture with asp.net core by ArdalisClean architecture with asp.net core by Ardalis
Clean architecture with asp.net core by Ardalis
Steven Smith
 
A Lightweight MDD Process Applied in Small Projects
A Lightweight MDD Process Applied in Small ProjectsA Lightweight MDD Process Applied in Small Projects
A Lightweight MDD Process Applied in Small Projects
Gabor Guta
 
Software Architecture for Agile Development
Software Architecture for Agile DevelopmentSoftware Architecture for Agile Development
Software Architecture for Agile Development
Hayim Makabee
 
Twelve Factor - Designing for Change
Twelve Factor - Designing for ChangeTwelve Factor - Designing for Change
Twelve Factor - Designing for Change
Eric Wyles
 
Best practices for creating modular Web applications
Best practices for creating modular Web applicationsBest practices for creating modular Web applications
Best practices for creating modular Web applications
peychevi
 
Yemo_Capstone_MS_Fairfield University
Yemo_Capstone_MS_Fairfield UniversityYemo_Capstone_MS_Fairfield University
Yemo_Capstone_MS_Fairfield University
Guillermo Julca
 
Gk1051 001 j2-ee_arch_tt425v1.1
Gk1051 001 j2-ee_arch_tt425v1.1Gk1051 001 j2-ee_arch_tt425v1.1
Gk1051 001 j2-ee_arch_tt425v1.1
vciampa
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdf
Kamal Acharya
 
Actively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net DeveloperActively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net Developer
Karthik Reddy
 
Actively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net DeveloperActively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net Developer
Karthik Reddy
 
ASAS 2014 - Simon Brown
ASAS 2014 - Simon BrownASAS 2014 - Simon Brown
ASAS 2014 - Simon Brown
Avisi B.V.
 
Ad

Recently uploaded (20)

PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Ad

Clean architecture with asp.net core

  • 1. Clean Architecture with ASP.NET Core STEVE SMITH ARDALIS.COM | @ARDALIS | [email protected] DEVIQ.COM
  • 2. Learn More After Today 1) Pluralsight ◦ N-Tier Apps with C# http://bit.ly/PS-NTier1 ◦ Domain-Driven Design Fundamentals http://bit.ly/ddd-fundamentals 2) DevIQ ◦ ASP.NET Core Quick Start http://aspnetcorequickstart.com 3) Microsoft FREE eBook/Sample App ◦ eShopOnWeb eCommerce Sample https://ardalis.com/architecture-ebook 4) Contact me for mentoring/training for your company/team
  • 3. Questions HOPEFULLY YOU’LL KNOW THE ANSWERS WHEN WE’RE DONE
  • 4. Why do we separate applications into multiple projects?
  • 5. What are some principles we can apply when organizing our software modules?
  • 6. How does the organization of our application’s solution impact coupling?
  • 7. What problems result from certain common approaches?
  • 8. How does Clean Architecture address these problems?
  • 9. How does ASP.NET Core help?
  • 12. Separation of Concerns Avoid mixing different code responsibilities in the same (method | class | project) The Big Three™ Data Access Business Rules and Domain Model User Interface
  • 13. Separation of Concerns The Big Three™ Data Access Business Rules and Domain Model User Interface
  • 15. Single Responsibility Works in tandem with Separation of Concerns Classes should focus on a single responsibility – a single reason to change.
  • 17. Following Don’t Repeat Yourself… Refactor repetitive code into functions Group functions into cohesive classes Group classes into folders and namespaces by  Responsibility  Level of abstraction  Etc. Further group class folders into projects
  • 18. More Grouping Options to Explore Aggregates Features Bounded Contexts
  • 20. Invert (and inject) Dependencies Both high level classes and implementation-detail classes should depend on abstractions (interfaces).
  • 21. Invert (and inject) Dependencies Classes should follow Explicit Dependencies Principle: Request all dependencies via their constructor Make your types honest, not deceptive
  • 22. Invert (and inject) Dependencies Corollary: Abstractions/interfaces must be defined somewhere accessible by: Low level implementation services High level business services User interface entry points
  • 23. Make the right thing easy and the wrong thing hard. FORCE DEVELOPERS INTO A “PIT OF SUCCESS”
  • 24. Make the right thing easy and the wrong thing hard. UI classes shouldn’t depend directly on infrastructure classes ◦ How can we structure our solution to help enforce this?
  • 25. Make the right thing easy and the wrong thing hard. Business/domain classes shouldn’t depend on infrastructure classes ◦ How can our solution design help?
  • 26. Make the right thing easy and the wrong thing hard. Repetition of (query logic, validation logic, policies, error handling, anything) is a problem ◦ What patterns can we apply to make avoiding repetition easier than copy/pasting?
  • 31. Transitive Dependencies DB Data Access Layer Business Logic Layer User Interface Layer Everything Depends on the database
  • 32. Domain-Centric Design AND THE CLEAN ARCHITECTURE Core Business Logic Everything Else
  • 33. Domain Model Not just business logic, but also: A model of the problem space composed of Entities, Interfaces, Services, and more. Interfaces define contracts for working with domain objects Everything in the application (including infrastructure and data access) depends on these interfaces and domain objects
  • 34. Clean Architecture Onion Architecture Hexagonal Architecture Ports and Adapters
  • 37. Clean Architecture “Rules” 1. You do not talk about Clean Architecture.
  • 38. Clean Architecture “Rules” 1. You do not talk about Clean Architecture.
  • 39. Clean Architecture “Rules” The Application Core contains the Domain Model
  • 40. Clean Architecture “Rules” All projects depend on the Core project; dependencies point inward toward this core
  • 41. Clean Architecture “Rules” Inner projects define interfaces; outer projects implement them
  • 42. Clean Architecture “Rules” Avoid direct dependency on the Infrastructure project (except from Integration Tests)
  • 43. Clean Architecture Features Framework Independent ◦ You can use this architecture with ASP.NET (Core), Java, Python, etc. ◦ It doesn’t rely on any software library or proprietary codebase.
  • 44. Clean Architecture Features Database Independent ◦ The vast majority of the code has no knowledge of persistence details. ◦ This knowledge may exist in just one class, in one project that no other project references.
  • 45. Clean Architecture Features UI Independent ◦ Only the UI project cares about the UI. ◦ The rest of the system is UI-agnostic.
  • 46. Clean Architecture Features Testable ◦ Apps built using this approach, and especially the core domain model and its business rules, are easy to test.
  • 47. Refactoring to a Clean Architecture Best to start from a properly organized solution ◦ See http://github.com/ardalis/CleanArchitecture Next-best: Start from an application consisting of just a single project Most difficult: Large, existing investment in multi-layer architecture without abstractions or DI
  • 48. The Core Project Minimal dependencies – none on Infrastructure. What Goes in Core: Interfaces Entities Value Objects Aggregates Domain Services Domain Events Exceptions Specifications Event Handlers
  • 49. The Infrastructure Project All dependencies on out-of-process resources. What Goes in Infrastructure: Repositories EF (Core) DbContext Web API Clients File System Accessors Email/SMS Sending Logging Adapters System Clock Other Services Cached Repositories Interfaces
  • 50. The Web Project All dependencies on out-of-process resources. What Goes in Web: Controllers Views ViewModels Filters Binders Other Services Razor Pages ApiModels BindingModels Tag/Html Helpers Or Interfaces
  • 51. Sharing Between Solutions: Shared Kernel Common Types May Be Shared Between Solutions. Will be referenced by Core project(s). Ideally distributed as Nuget Packages. What Goes in Shared Kernel: Base Entity Base Domain Event Base Specification Common Exceptions Common Interfaces Common Auth e.g. User class Common DI Common Logging Common Guard Clauses
  • 52. Guard Clauses? Simple checks for input that use common rules and exceptions. Nuget Package: Ardalis.GuardClauses (https://github.com/ardalis/GuardClauses) Example: public void ProcessOrder(Order order) { Guard.Against.Null(order, nameof(order)); // process order here }
  • 53. Solution Structure – Clean Architecture Web Core Infrastructure Shared Kernel Unit Tests Functional Tests Integration Tests
  • 55. What belongs in actions/handlers? Controller Actions (or Page Handlers) should: 1) Accept task-specific types (ViewModel, ApiModel, BindingModel) 2) Perform and handle model validation (ideally w/filters) 3) “Do Work” (More on this in a moment) 4) Create any model type required for response (ViewModel, ApiModel, etc.) 5) Return an appropriate Result type (View, Page, Ok, NotFound, etc.)
  • 56. “Do Work” – Option One Repositories and Entities 1) Get entity from an injected Repository 2) Work with the entity and its methods. 3) Update the entity’s state using the Repository Great for simple operations Great for CRUD work Requires mapping between web models and domain model within controller
  • 58. “Do Work” – Option Two Work with an application service. 1) Pass ApiModel types to service 2) Service internally works with repositories and domain model types. 3) Service returns a web model type Better for more complex operations Application Service is responsible for mapping between web models and domain model. Keeps controllers lightweight, and with fewer injected dependencies.
  • 60. “Do Work” – Option Three Work with commands and a tool like Mediatr. 1) Use ApiModel types that represent commands (e.g. RegisterUser) 2) Send model-bound instance of command to handler using _mediator.Send() No need to inject separate services to different controllers – Mediatr becomes only dependency.
  • 64. Resources Online Courses (Pluralsight and DevIQ) • SOLID Principles of OO Design http://bit.ly/SOLID-OOP • N-Tier Architecture in C# http://bit.ly/PS-NTier1 and http://bit.ly/PS-NTier2 • DDD Fundamentals http://bit.ly/ddd-fundamentals • ASP.NET Core Quick Start http://aspnetcorequickstart.com/ • Weekly Dev Tips Podcast http://www.weeklydevtips.com/ • Microsoft Architecture eBook/sample http://aka.ms/WebAppArchitecture