Clean Architecture With ASP - Net Core
Clean Architecture With ASP - Net Core
Clean Architecture With ASP - Net Core
2) DevIQ
◦ ASP.NET Core Quick Start http://aspnetcorequickstart.com
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.
UI classes shouldn’t depend directly on infrastructure classes
◦ How can we structure our solution to help enforce this?
(slightly)
Better
Looser
Maintainability
Coupling
N-Tier Drawbacks
Transitive (some)
Dependencies Complexity
(still)
Tight
Coupling
Transitive Dependencies
User Interface
Layer
Business Logic
Layer
Everything
Depends on the database
Data Access
Layer
DB
Domain-Centric Design
AND THE CLEAN ARCHITECTURE
Domain Model
Not just “business logic”
A model of the problem space composed of Entities, Interfaces, Services, and more.
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 “Rules”
The Application Core contains the Domain Model
All projects depend on the Core project; dependencies point inward toward this core
Database Independent
◦ The vast majority of the code has no knowledge of what database, if any, might be used by the
application. Often, this knowledge will exist in a single class, in a single project that no other project
references.
UI Independent
◦ Only the UI project cares about the UI. The rest of the system is UI-agnostic.
Testable
◦ Apps built using this approach, and especially the core domain model and its business rules, are
extremely testable.
Refactoring to a Clean Architecture
Best to start from a properly organized solution
◦ See http://github.com/ardalis/CleanArchitecture
Event Handlers
Entities Value Objects Aggregates
Domain
Services
Specifications
Exceptions
The Infrastructure Project
All dependencies on out-of-process resources.
What Goes in Infrastructure:
EF (Core) Cached
Repositories System Clock
DbContext Repositories
Web API
Clients
Email/SMS Other
Sending Interfaces
Services
The Web Project
All dependencies on out-of-process resources.
What Goes in Web:
Razor
Controllers Views Or
Pages
Tag/Html
Filters Binders
Helpers
Common Common
Exceptions Interfaces
Shared Kernel
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
No need to inject separate services to different controllers – Mediatr becomes only dependency.
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/ DEVINTFALL17 20% OFF!