An Atypical ASP.NET Core 6 Design Patterns Guide - Second Edition

Book description

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

Product information

  • Title: An Atypical ASP.NET Core 6 Design Patterns Guide - Second Edition
  • Author(s): Carl-Hugo Marcotte, Abdelhamid Zebdi
  • Release date: March 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803249841