Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
What to Upload to SlideShare
What to Upload to SlideShare
Loading in …3
×
1 of 72

Clean architecture with asp.net core by Ardalis

8

Share

Download to read offline

Clean Architecture with ASP.NET Core
ASP.NET Core provides better support for clean, testable, maintainable architectures than any of its predecessors. Learn the underlying principles that apply to project/solution organization, how best to arrange your projects to support clean code, and how to refactor existing solutions toward this architecture. Examples will use ASP.NET Core but the principles covered apply to all languages and platforms.

About Steve
Steve Smith (@ardalis) is an entrepreneur and software developer with a passion for building quality software as effectively as possible. Steve has published several courses on Pluralsight, covering DDD, SOLID, design patterns, and software architecture. He’s a Microsoft ASP.NET MVP and Insider, a frequent speaker at developer conferences, an author, and a trainer. Steve works with companies that want to avoid the trap of technical debt by helping their teams deliver quality software, quickly. Steve and his team have been described by clients as a “force multiplier”, amplifying the value of existing development teams. Steve’s client list includes Microsoft, Quicken Loans, Celina Insurance, and many other satisfied customers.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Clean architecture with asp.net core by Ardalis

  1. 1. Applying Clean Architecture to ASP.NET Core Apps STEVE SMITH ARDALIS.COM | @ARDALIS | STEVE@ARDALIS.COM MENTOR | TRAINER | COACH | FORCE MULTIPLIER Applying Clean Architecture to ASP.NET Core | @ardalis
  2. 2. Welcome SLC-NET Folks! Applying Clean Architecture to ASP.NET Core | @ardalis
  3. 3. Literally 10 minutes before starting… Applying Clean Architecture to ASP.NET Core | @ardalis
  4. 4. Literally 10 minutes before starting… Applying Clean Architecture to ASP.NET Core | @ardalis
  5. 5. Learn More After Today 1) Pluralsight – FREE in April 2020 ◦ Domain-Driven Design Fundamentals https://www.pluralsight.com/courses/domain-driven-design-fundamentals 2) Microsoft FREE eBook/Sample App (updated for .NET Core 3.1) ◦ eShopOnWeb eCommerce Sample https://ardalis.com/architecture-ebook 3) Contact me for mentoring/training for your company/team 4) Developer Career Mentoring at devBetter.com Applying Clean Architecture to ASP.NET Core | @ardalis
  6. 6. Weekly Dev Tips Podcast and Newsletter (and stream)  Ardalis.com/tips  WeeklyDevTips.com Streaming at twitch.tv/ardalis Fridays Applying Clean Architecture to ASP.NET Core | @ardalis
  7. 7. Questions HOPEFULLY YOU’LL KNOW THE ANSWERS WHEN WE’RE DONE Applying Clean Architecture to ASP.NET Core | @ardalis
  8. 8. Why do we separate applications into multiple projects? Applying Clean Architecture to ASP.NET Core | @ardalis
  9. 9. What are some principles we can apply when organizing our software modules? Applying Clean Architecture to ASP.NET Core | @ardalis
  10. 10. How does the organization of our application’s solution impact coupling? Applying Clean Architecture to ASP.NET Core | @ardalis
  11. 11. What problems result from certain common approaches? Applying Clean Architecture to ASP.NET Core | @ardalis
  12. 12. How does Clean Architecture address these problems? Applying Clean Architecture to ASP.NET Core | @ardalis
  13. 13. How does ASP.NET Core help? Applying Clean Architecture to ASP.NET Core | @ardalis
  14. 14. Principles A BIT OF GUIDANCE
  15. 15. Separation of Concerns Avoid mixing different code responsibilities in the same (method | class | project) Applying Clean Architecture to ASP.NET Core | @ardalis
  16. 16. Separation of Concerns The Big Three™ Data Access Business Rules and Domain Model User Interface Applying Clean Architecture to ASP.NET Core | @ardalis
  17. 17. Applying Clean Architecture to ASP.NET Core | @ardalis
  18. 18. Single Responsibility Works in tandem with Separation of Concerns Classes should focus on a single responsibility – a single reason to change. Applying Clean Architecture to ASP.NET Core | @ardalis
  19. 19. Applying Clean Architecture to ASP.NET Core | @ardalis
  20. 20. 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 Applying Clean Architecture to ASP.NET Core | @ardalis
  21. 21. Applying Clean Architecture to ASP.NET Core | @ardalis
  22. 22. Invert (and inject) Dependencies Both high level classes and implementation-detail classes should depend on abstractions (interfaces). Applying Clean Architecture to ASP.NET Core | @ardalis
  23. 23. Invert (and inject) Dependencies Classes should follow Explicit Dependencies Principle: Request all dependencies via their constructor Make your types honest, not deceptive Applying Clean Architecture to ASP.NET Core | @ardalis
  24. 24. 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 Applying Clean Architecture to ASP.NET Core | @ardalis
  25. 25. Applying Clean Architecture to ASP.NET Core | @ardalis
  26. 26. Applying Clean Architecture to ASP.NET Core | @ardalis
  27. 27. Make the right thing easy and the wrong thing hard FORCE DEVELOPERS INTO A “PIT OF SUCCESS” Applying Clean Architecture to ASP.NET Core | @ardalis
  28. 28. Applying Clean Architecture to ASP.NET Core | @ardalis
  29. 29. 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? Applying Clean Architecture to ASP.NET Core | @ardalis
  30. 30. 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? Applying Clean Architecture to ASP.NET Core | @ardalis
  31. 31. 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? Applying Clean Architecture to ASP.NET Core | @ardalis
  32. 32. “Classic” N-Tier Architecture OR N-LAYER Layer Layer Layer Applying Clean Architecture to ASP.NET Core | @ardalis
  33. 33. Source: MSDN Website, 2001 Applying Clean Architecture to ASP.NET Core | @ardalis
  34. 34. Transitive Dependencies DB Data Access Layer Business Logic Layer User Interface Layer Everything Depends on the database Applying Clean Architecture to ASP.NET Core | @ardalis
  35. 35. Applying Clean Architecture to ASP.NET Core | @ardalis
  36. 36. Domain-Centric Design AND THE CLEAN ARCHITECTURE Core Business Logic Everything Else Applying Clean Architecture to ASP.NET Core | @ardalis
  37. 37. 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 Applying Clean Architecture to ASP.NET Core | @ardalis
  38. 38. Clean Architecture Onion Architecture Hexagonal Architecture Ports and Adapters Applying Clean Architecture to ASP.NET Core | @ardalis
  39. 39. Applying Clean Architecture to ASP.NET Core | @ardalis
  40. 40. Applying Clean Architecture to ASP.NET Core | @ardalis
  41. 41. Clean Architecture “Rules” 1. You do not talk about Clean Architecture. Applying Clean Architecture to ASP.NET Core | @ardalis
  42. 42. Clean Architecture “Rules” 1. You do not talk about Clean Architecture. Applying Clean Architecture to ASP.NET Core | @ardalis
  43. 43. Clean Architecture “Rules” The Application Core contains the Domain Model Applying Clean Architecture to ASP.NET Core | @ardalis
  44. 44. Clean Architecture “Rules” All projects depend on the Core project; dependencies point inward toward this core Applying Clean Architecture to ASP.NET Core | @ardalis
  45. 45. Clean Architecture “Rules” Inner projects define interfaces; Outer projects implement them Applying Clean Architecture to ASP.NET Core | @ardalis
  46. 46. Clean Architecture “Rules” Avoid direct dependency on the Infrastructure project (except from Integration Tests and possibly Startup.cs) Applying Clean Architecture to ASP.NET Core | @ardalis
  47. 47. 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. Applying Clean Architecture to ASP.NET Core | @ardalis
  48. 48. 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. Applying Clean Architecture to ASP.NET Core | @ardalis
  49. 49. Clean Architecture Features UI Independent ◦ Only the UI project cares about the UI. ◦ The rest of the system is UI-agnostic. Applying Clean Architecture to ASP.NET Core | @ardalis
  50. 50. Clean Architecture Features Testable ◦ Apps built using this approach, and especially the core domain model and its business rules, are easy to test. Applying Clean Architecture to ASP.NET Core | @ardalis
  51. 51. Refactoring to a Clean Architecture Best to start from a properly organized solution ◦ See https://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 Applying Clean Architecture to ASP.NET Core | @ardalis
  52. 52. The Core Project (domain model) Minimal dependencies – none on Infrastructure. What Goes in Core: Interfaces Entities Value Objects Aggregates Domain Services Domain Events Exceptions Specifications Event Handlers Applying Clean Architecture to ASP.NET Core | @ardalis
  53. 53. 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 Applying Clean Architecture to ASP.NET Core | @ardalis
  54. 54. 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 Applying Clean Architecture to ASP.NET Core | @ardalis
  55. 55. 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 Applying Clean Architecture to ASP.NET Core | @ardalis
  56. 56. Guard Clauses? BAD EXAMPLE public void ProcessOrder(Order order, Custom customer) { if(order != null) { if(customer != null) { // process order here } else { throw new ArgumentNullException(nameof(customer), customer); } } else { throw new ArgumentNullException(nameof(order), order); } } Applying Clean Architecture to ASP.NET Core | @ardalis
  57. 57. Guard Clauses? public void ProcessOrder(Order order, Customer customer) { if(order == null) throw new ArgumentNullException(nameof(order), order); if(customer==null) throw new ArgumentNullException(nameof(customer), customer); // process order here } Applying Clean Architecture to ASP.NET Core | @ardalis
  58. 58. 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, Customer customer) { Guard.Against.Null(order, nameof(order)); Guard.Against.Null(customer, nameof(customer)); // process order here } Applying Clean Architecture to ASP.NET Core | @ardalis
  59. 59. Solution Structure – Clean Architecture Web Core Infrastructure Shared Kernel Unit Tests Functional Tests Integration Tests Applying Clean Architecture to ASP.NET Core | @ardalis
  60. 60. Typical (Basic) Folder Structure Applying Clean Architecture to ASP.NET Core | @ardalis
  61. 61. 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.) Applying Clean Architecture to ASP.NET Core | @ardalis
  62. 62. “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 Applying Clean Architecture to ASP.NET Core | @ardalis
  63. 63. Applying Clean Architecture to ASP.NET Core | @ardalis
  64. 64. “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 models Keeps controllers lightweight, and with fewer injected dependencies Applying Clean Architecture to ASP.NET Core | @ardalis
  65. 65. Applying Clean Architecture to ASP.NET Core | @ardalis
  66. 66. “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. Applying Clean Architecture to ASP.NET Core | @ardalis
  67. 67. Instantiate Appropriate Command Applying Clean Architecture to ASP.NET Core | @ardalis
  68. 68. Resolve Command w/Model Binding Applying Clean Architecture to ASP.NET Core | @ardalis
  69. 69. Code Walkthrough GITHUB.COM/ARDALIS/CLEANARCHITECTURE Applying Clean Architecture to ASP.NET Core | @ardalis
  70. 70. Resources Clean Architecture Solution Template https://github.com/ardalis/cleanarchitecture For Worker Services: https://github.com/ardalis/CleanArchitecture.WorkerService Online Courses (Pluralsight and DevIQ) • SOLID Principles of OO Design https://ardalis.com/ps-stevesmith • N-Tier Architecture in C# https://ardalis.com/ps-stevesmith • DDD Fundamentals https://ardalis.com/ps-stevesmith • ASP.NET Core Quick Start http://aspnetcorequickstart.com/ Weekly Dev Tips Podcast http://www.weeklydevtips.com/ Microsoft Architecture eBook/sample http://aka.ms/WebAppArchitecture Group Coaching for Developers https://devbetter.com/ Applying Clean Architecture to ASP.NET Core | @ardalis
  71. 71. Thanks! Steve Smith steve@ardalis.com @ardalis Applying Clean Architecture to ASP.NET Core | @ardalis

Editor's Notes

  • Photo by Rodrigo Soares on Unsplash
  • Otherwise they’re likely to end up in …
  • Working on such systems, with all of their tight coupling, can be draining…
  • Often worthwhile to split tests into separate projects by type. Can make it easier to run certain sets of tests based on context/environment.
  • ×