An Atypical ASP.NET Core 6 Design Patterns Guide: A SOLID adventure into architectural principles and design patterns using .NET 6 and C# 10, 2nd Edition

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

The professional developer's essential guide to building robust, maintainable, and flexible web apps by leveraging C# 10 and .NET 6 features and component- and application-scale design patterns

Key Features

  • Apply the SOLID architectural principles and software design patterns effectively with a focus on dependency injection
  • Discover modern application architectures such as vertical slice, clean architecture, and event-driven microservices
  • Explore full-stack ASP.NET Core with an overview of Blazor

Book Description

An Atypical ASP.NET Core 6 Design Patterns Guide, Second Edition approaches programming like playing with LEGO®: snapping small pieces together to create something beautiful. Thoroughly updated for ASP.NET Core 6, with further coverage of microservices patterns, data contracts, and event-driven architecture, this book gives you the tools to build and glue reliable components together to improve your programmatic masterpieces.

The chapters are organized based on scale and topic, allowing you to start small and build on a strong base, the same way that you would develop a program. You will begin by exploring basic design patterns, SOLID architectural principles, dependency injection, and other ASP.NET Core 6 mechanisms. You will explore component-scale patterns, and then move to higher level application-scale patterns and techniques to better structure your applications. Finally, you'll advance to the client side to connect the dots with tools like Blazor and make ASP.NET Core a viable full-stack web development framework.

You will supplement your learning with practical use cases and best practices, exploring a range of significant Gang of Four (GoF) design patterns along the way. By the end of the book, you will be comfortable combining and implementing patterns in different ways, and crafting software solutions of any scale.

What you will learn

  • Apply the SOLID principles for building flexible and maintainable software
  • Get to grasp .NET dependency Injection
  • Work with GoF design patterns such as strategy, decorator, facade, and composite
  • Explore the MVC patterns for designing web APIs and web applications using Razor
  • Discover layering techniques and tenets of clean architecture
  • Become familiar with CQRS and vertical slice architecture as an alternate to layering
  • Understand microservices and when they can benefit your applications
  • Build an ASP.NET user interfaces from server-side to client-side Blazor

Who this book is for

The book is intended for intermediate software and web developers with an understanding of .NET who want to write flexible, maintainable, and robust code for building scalable web applications. Knowledge of C# programming and an understanding of web concepts like HTTP is necessary.

Table of Contents

  1. Introduction
  2. Automated Testing
  3. Architectural Principles
  4. The MVC Pattern using Razor
  5. The MVC Pattern for Web APIs
  6. Understanding the Strategy, Abstract Factory, and Singleton Design Patterns
  7. Deep Dive into Dependency Injection
  8. Options and Logging Patterns
  9. Structural Patterns
  10. Behavioral Patterns
  11. Understanding the Operation Result Design Pattern
  12. Understanding Layering
  13. Getting Started with Object Mappers
  14. Mediator and CQRS Design Patterns
  15. Getting Started with Vertical Slice Architecture
  16. Introduction to Microservices Architecture
  17. ASP.NET Core User Interfaces
  18. A Brief Look into Blazor
  19. Appendix A
  20. Appendix B

Author(s): Carl-Hugo Marcotte
Edition: 2
Publisher: Packt Publishing
Year: 2022

Language: English
Commentary: True PDF
Pages: 678

Cover
Copyright
Contributors
Table of Contents
Preface
Section 1: Principles and Methodologies
Chapter 1: Introduction
What is a design pattern?
Anti-patterns and code smells
Anti-patterns
Anti-pattern – God Class
Code smells
Code smell – Control Freak
Code smell – Long Methods
Understanding the web – request/response
Getting started with .NET
.NET SDK versus runtime
.NET 5+ versus .NET Standard
Visual Studio Code versus Visual Studio versus the command-line interface
An overview of project templates
Running and building your program
Technical requirements
Summary
Questions
Further reading
Chapter 2: Automated Testing
An overview of automated testing
Unit testing
Integration testing
End-to-end testing
Other types of tests
Picking the right test style
Testing approaches
Refactoring
Technical debt
Testing .NET applications
Creating an xUnit test project
Getting started with xUnit
Facts
Assertions
Theories
Closing words
Arrange, Act, Assert
Organizing your tests
Unit tests
Integration tests
ASP.NET Core integration testing
Classic web application
Minimal hosting
Important testing principles
Summary
Questions
Further reading
Chapter 3: Architectural Principles
The SOLID principles
Single responsibility principle (SRP)
Project – BookStore
What is an interface?
Open/Closed principle (OCP)
Project – IAttacker
Liskov substitution principle (LSP)
Project – HallOfFame
Conclusion
Interface segregation principle (ISP)
Project – Ninja versus Pirate
Project – Bookstore update
Conclusion
Dependency inversion principle (DIP)
Direct dependency
Inverted dependency
Inverting subsystems using DIP
Project – Dependency inversion
Conclusion
What’s next?
Other important principles
Separation of concerns
Don’t repeat yourself (DRY)
Keep it simple, stupid (KISS)
Summary
Questions
Further reading
Section 2: Designing for ASP.NET Core
Chapter 4: The MVC Pattern Using Razor
The Model View Controller design pattern
Goal
Design
Anatomy of ASP.NET Core MVC
Directory structure
Controller
Model
View
Default routing
Conclusion
The View Model design pattern
Goal
Design
Project – View models (a list of students)
Conclusion
Summary
Questions
Further reading
Chapter 5: The MVC Pattern for Web APIs
An overview of REST
HTTP methods
Status code
HTTP headers
Versioning
Default versioning strategy
Versioning strategy
Wrapping up
The Model View Controller design pattern
Goal
Design
Anatomy of ASP.NET Core web APIs
The entry point
Directory structure
Controller
Returning values
Attribute routing
Conclusion
The Data Transfer Object design pattern
Goal
Design
Project – DTO
Conclusion
API contracts
Summary
Questions
Further reading
Chapter 6: Understanding the Strategy, Abstract Factory, and Singleton Design Patterns
The Strategy design pattern
Goal
Design
Project – Strategy
Conclusion
The Abstract Factory design pattern
Goal
Design
Project – AbstractVehicleFactory
Conclusion
The Singleton design pattern
Goal
Design
An alternate (better) way
Code smell – Ambient Context
Conclusion
Summary
Questions
Chapter 7: Deep Dive into Dependency Injection
What is dependency injection?
The composition root
Registering your features elegantly
Object lifetime
Code smell – Control Freak
Using external IoC containers
Revisiting the Strategy pattern
Constructor injection
Property injection
Method injection
Project – Strategy
Adding the View Model
Adding a guard clause
Conclusion
Revisiting the Singleton pattern
Project – Application state
Project – Wishlist
Conclusion
Understanding the Service Locator pattern
Project – ServiceLocator
Implementing method injection
Implementing constructor injection
Implementing a minimal API
Conclusion
Revisiting the Factory pattern
Project – Factory
Conclusion
Summary
Questions
Further reading
Chapter 8: Options and Logging Patterns
The Options pattern
Getting started
IOptionsMonitor
IOptionsFactory
IOptionsSnapshot
IOptions
Project – CommonScenarios
Named options
Using settings
Project – OptionsConfiguration
Implementing a configurator object
Using multiple configurator objects
Exploring other configuration possibilities
Project – OptionsValidation
Eager validation
Data annotations
Validation types
Project – OptionsValidationFluentValidation
Workaround – Injecting options directly
Conclusion
Becoming familiar with .NET logging abstractions
About logging
Writing logs
Log levels
Logging providers
Configuring logging
Structured logging
Conclusion
Summary
Questions
Further reading
Section 3: Designing at Component Scale
Chapter 9: Structural Patterns
Implementing the Decorator design pattern
Goal
Design
Project – Adding behaviors
DecoratorA
DecoratorB
Project – Decorator using Scrutor
Conclusion
Implementing the Composite design pattern
Goal
Design
Project – BookStore
Conclusion
Implementing the Adapter design pattern
Goal
Design
Project – Greeter
Conclusion
Implementing the Façade design pattern
Goal
Design
Project – The façades
Opaque façade
Transparent façade
The program
Flexibility in action
Alternative façade patterns
Conclusion
Summary
Questions
Further reading
Chapter 10: Behavioral Patterns
Implementing the Template Method pattern
Goal
Design
Project – Building a search machine
Conclusion
Implementing the Chain of Responsibility pattern
Goal
Design
Project – Message interpreter
Project – Improved message interpreter
Project – A final, finer-grained design
Conclusion
Summary
Questions
Chapter 11: Understanding the Operation Result Design Pattern
The Operation Result pattern
Goal
Design
Project – Implementing different Operation Result patterns
The consumer
The simplest form of the Operation Result pattern
A single error message
Adding a return value
Multiple error messages
Adding message severity
Sub-classes and factories
Advantages and disadvantages
Advantages
Disadvantages
Summary
Questions
Further reading
Section 4: Designing at Application Scale
Chapter 12: Understanding Layering
Introducing layering
Classic layering model
Splitting the layers
Layers versus tiers versus assemblies
What is an assembly?
Responsibilities of the common layers
Presentation
Domain
Rich domain model
Anemic domain model
Service layer
Data
Repository pattern
Unit of Work pattern
Abstract layers
Sharing the model
Clean Architecture
Implementing layering in real life
To be or not to be a purist?
Building a façade over a database
Summary
Questions
Further reading
Chapter 13: Getting Started with Object Mappers
Object mapper
Goal
Design
Project – Mapper
Code smell – Too many dependencies
Pattern – Aggregate Services
Pattern – Mapping Façade
Project – Mapping service
Project – AutoMapper
Conclusion
Summary
Questions
Further reading
Chapter 14: Mediator and CQRS Design Patterns
A high-level overview of Vertical Slice Architecture
Implementing the Mediator pattern
Goal
Design
Project – Mediator (IMediator)
Project – Mediator (IChatRoom)
Conclusion
Implementing the CQRS pattern
Goal
Design
Project – CQRS
Code smell – Marker Interfaces
Metadata
Dependency identifier
Conclusion
Using MediatR as a mediator
Project – Clean Architecture with MediatR
Conclusion
Summary
Questions
Further reading
Chapter 15: Getting Started with Vertical Slice Architecture
Vertical Slice Architecture
What are the advantages and disadvantages?
Anti-pattern – Big Ball of Mud
Project – Vertical Slice Architecture
Project organization
Exploring a feature
Request validation
Testing
Continuing your journey
Conclusion
Summary
Questions
Further reading
Chapter 16: Introduction to Microservices Architecture
What are microservices?
Cohesive unit of business
Ownership of data
Microservice independence
An introduction to event-driven architecture
Domain events
Integration events
Application events
Enterprise events
Conclusion
Getting started with message queues
Conclusion
Implementing the Publish-Subscribe pattern
Message brokers
The event sourcing pattern
Example
Conclusion
Introducing Gateway patterns
Gateway Routing pattern
Gateway Aggregation pattern
Backends for Frontends pattern
Mixing and matching gateways
Conclusion
Revisiting the CQRS pattern
Conclusion
Exploring the Microservice Adapter pattern
Adapting an existing system to another
Decommissioning a legacy application
Adapting an event broker to another
Conclusion
Summary
Questions
Further reading
Section 5: Designing the Client Side
Chapter 17: ASP.NET Core User Interfaces
Getting familiar with Razor Pages
Design
Routing
Conclusion
Organizing the user interface
Partial views
Project – Shared form
Conclusion
Tag Helpers
Built-in Tag Helpers
Creating a custom Tag Helper
Creating an RSS feed TagHelperComponent
Conclusion
View components
Project – Reusable employee count
Conclusion
Display and editor templates
Display templates
Editor templates
Project – Composite BookStore revisited
Conclusion
Summary
Questions
Further reading
Chapter 18: A Brief Look into Blazor
Overview of Blazor Server
Overview of Blazor WebAssembly
Getting familiar with Razor components
Creating Razor components
C#-only components
Razor-only components
Razor and C# hybrid components
CSS isolation
Component life cycle
Event handling
The Model-View-Update pattern
Goal
Design
Project – Counter
Conclusion
A medley of Blazor features
Summary
Questions
Further reading
An end is simply a new beginning
Appendices
Appendix A
Older C# features
The null-coalescing operator (C# 2.0)
Expression-bodied member (C# 6-7)
Throw expressions (C# 7.0)
Tuples (C# 7.0+)
Default literal expressions (C# 7.1)
Switch expressions (C# 8)
Discards (C# 7)
Async main (C# 7.1)
User-defined conversion operators (C# 1)
Local functions (C# 7) and a static local function (C# 8)
What’s new in .NET 5 and C# 9?
Top-level statements
Target-typed new expressions
Init-only properties
Record classes
Simplifying the record creation
The with keyword
Deconstruction
Equality comparison
Conclusion
What’s new in .NET 6 and C# 10?
File-scoped namespaces
Global using directives
Implicit using directives
Constant interpolated strings
Record struct
Minimal hosting model
Minimal APIs
Nullable reference types
Appendix B
An overview of containers
Docker
Docker Compose
Orchestration
Project Tye
Kubernetes
Scaling
Summary
Further reading
Assessment Answers
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Acronyms Lexicon
Other Books You May Enjoy
PacktPage
Other Books You May Enjoy
Index