An Atypical ASP.NET Core 5 Design Patterns Guide

Book description

A .NET developer's guide to crafting robust, maintainable, and flexible web apps by leveraging C# 9 and .NET 5 features and component-scale and application-scale design patterns

Key Features

  • Apply software design patterns effectively, starting small and progressing to cloud-scale
  • Discover modern application architectures such as vertical slice, clean architecture, and event-driven microservices
  • Explore ASP.NET design patterns, from options to full-stack web development using Blazor

Book Description

Design patterns are a set of solutions to many of the common problems occurring in software development. Knowledge of these design patterns helps developers and professionals to craft software solutions of any scale.

ASP.NET Core 5 Design Patterns starts by exploring basic design patterns, architectural principles, dependency injection, and other ASP.NET Core mechanisms. You'll explore the component scale as you discover patterns oriented toward small chunks of the software, and then move to application-scale patterns and techniques to understand higher-level patterns and how to structure the application as a whole. The book covers a range of significant GoF (Gangs of Four) design patterns such as strategy, singleton, decorator, facade, and composite. The chapters are organized based on scale and topics, allowing you to start small and build on a strong base, the same way that you would develop a program. With the help of use cases, the book will show you how to combine design patterns to display alternate usage and help you feel comfortable working with a variety of design patterns. Finally, you'll advance to the client side to connect the dots and make ASP.NET Core a viable full-stack alternative.

By the end of the book, you'll be able to mix and match design patterns and have learned how to think about architecture and how it works.

What you will learn

  • Apply the SOLID principles for building flexible and maintainable software
  • Get to grips with .NET 5 dependency injection
  • Work with GoF design patterns such as strategy, decorator, 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 alternative to layering
  • Understand microservices, what they are, and what they are not
  • Build ASP.NET UI from server-side to client-side Blazor

Who this book is for

This design patterns book is for intermediate-level software and web developers with some knowledge 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. An Atypical ASP.NET Core 5 Design Patterns Guide
  2. Why subscribe?
  3. Foreword
  4. Contributors
  5. About the author
  6. About the reviewers
  7. Packt is searching for authors like you
  8. Preface
    1. Who this book is for
    2. What this book covers
      1. Section 1, Principles and Methodologies
      2. Section 2, Designing for ASP.NET Core
      3. Section 3, Designing at Component Scale
      4. Section 4, Designing at Application Scale
      5. Section 5, Designing the Client Side
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Reviews
  9. Section 1: Principles and Methodologies
  10. Chapter 1: Introduction to .NET
    1. What is a design pattern?
    2. Anti-patterns and code smells
      1. Anti-patterns
      2. Code smells
    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 (CLI)
      4. Technical requirements
    5. Summary
    6. Questions
    7. Further reading
  11. Chapter 2: Testing Your ASP.NET Core Application
    1. Overview of automated testing and how it applies to ASP.NET Core
    2. Test-driven development (TDD)
    3. Testing made easy through ASP.NET Core
      1. How do you create an xUnit test project?
      2. Basic features of xUnit
      3. How to organize your tests
      4. How is it easier?
    4. Summary
    5. Questions
    6. Further reading
  12. Chapter 3: Architectural Principles
    1. The SOLID principles
      1. Single responsibility principle (SRP)
      2. Open/Closed principle (OCP)
      3. Liskov substitution principle (LSP)
      4. Interface segregation principle (ISP)
      5. Dependency inversion principle (DIP)
    2. Other important principles
      1. Separation of concerns
      2. Don't repeat yourself (DRY)
    3. Summary
    4. Questions
  13. Section 2: Designing for ASP.NET Core
  14. Chapter 4: The MVC Pattern using Razor
    1. The Model View Controller design pattern
    2. MVC using Razor
      1. Directory structure
      2. Structure of a controller
      3. Default routing
      4. Project: MVC
      5. Conclusion
    3. View Model design pattern
      1. Goal
      2. Design
      3. Project: View models (a list of students)
      4. Project: View models (a student form)
      5. Conclusion
    4. Summary
    5. Questions
    6. Further reading
  15. Chapter 5: The MVC Pattern for Web APIs
    1. An overview of REST
      1. Request HTTP methods
      2. Response status code
    2. Anatomy of a web API
      1. Setting up a web API
      2. Attribute routing
      3. Returning values
    3. C# features
      1. Class conversion operators (C#)
      2. Local functions (C# 7) and a static local function (C# 8)
    4. The Data Transfer Object design pattern
      1. Goal
      2. Design
      3. Project – DTO
    5. API contracts
      1. Analyzing the DTO sample
      2. Project – OpenAPI
      3. Project – API contracts
      4. Idea – Creating a typed client library
      5. One last observation
    6. Summary
    7. Questions
    8. Further reading
  16. Chapter 6: Understanding the Strategy, Abstract Factory, and Singleton Design Patterns
    1. The Strategy design pattern
      1. Goal
      2. Design
      3. Project: Strategy
      4. Conclusion
    2. A brief look at a few C# features
      1. Default literal expressions (C# 7.1)
      2. Switch expressions (C# 8)
      3. Discards (C# 7)
    3. The Abstract Factory design pattern
      1. Goal
      2. Design
      3. Project: AbstractVehicleFactory
      4. Project: MiddleEndVehicleFactory
      5. Conclusion
    4. The Singleton design pattern
      1. Goal
      2. Design
      3. An alternate (better) way
      4. Code smell: Ambient Context
      5. Conclusion
    5. Summary
    6. Questions
  17. Chapter 7: Deep Dive into Dependency Injection
    1. What is Dependency Injection?
      1. The composition root
      2. Extending IServiceCollection
      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
    4. Revisiting the Singleton pattern
      1. The application state
      2. Project: Wishlist
      3. Tuples (C# 7+)
    5. Understanding the Service Locator pattern
      1. Project: ServiceLocator
      2. Project: ServiceLocatorFixed
      3. Conclusion
    6. Revisiting the Factory pattern
      1. Factory mixed with method injection
      2. HomeViewModelFactory
    7. Summary
    8. Questions
    9. Further reading
  18. Chapter 8: Options and Logging Patterns
    1. An overview of the Options pattern
      1. Getting started
      2. Project – CommonScenarios
      3. Project – OptionsConfiguration
      4. Project – OptionsValidation
      5. Injecting options directly
      6. Conclusion
    2. Getting familiar with .NET logging abstractions
      1. About logging
      2. Writing logs
      3. Log levels
      4. Logging providers
      5. Configuring logging
      6. Conclusion
    3. Summary
    4. Questions
    5. Further reading
  19. Section 3: Designing at Component Scale
  20. Chapter 9: Structural Patterns
    1. Implementing the Decorator design pattern
      1. Goal
      2. Design
      3. Project: Adding behaviors
      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
      4. Conclusion
    5. Summary
    6. Questions
    7. Further reading
  21. Chapter 10: 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. Project – Message interpreter
      3. Project – Improved message interpreter
      4. Project – A final, finer-grained design
      5. Conclusion
    3. Summary
    4. Questions
  22. Chapter 11: Understanding the Operation Result Design Pattern
    1. Goal
    2. Design
    3. Project – Implementing different Operation Result patterns
      1. The consumer
      2. Its simplest form
      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
    5. Summary
    6. Questions
    7. Further reading
  23. Section 4: Designing at Application Scale
  24. Chapter 12: Understanding Layering
    1. Introduction to layering
      1. Splitting the layers
      2. Layers versus tiers versus assemblies
      3. To be or not to be a purist?
      4. Sharing the model
      5. The reality of small- to medium-sized enterprises
    2. Responsibilities of the common layers
      1. Presentation
      2. Domain
      3. Data
    3. Abstract data layer
    4. Shared rich model
    5. Clean Architecture
    6. Summary
    7. Questions
    8. Further reading
  25. Chapter 13: Getting Started with Object Mappers
    1. Overview of object mapping
      1. Goal
      2. Design
    2. Project: Mapper
      1. Code smell: Too many dependencies
      2. Pattern – Aggregate Services
      3. Pattern – Mapping Façade
    3. Project – Mapping service
    4. Project – AutoMapper
    5. Summary
    6. Questions
    7. Further reading
  26. Chapter 14: 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
      5. Conclusion
    4. Using MediatR as a mediator
      1. Project – Clean Architecture with MediatR
      2. Conclusion
    5. Summary
    6. Questions
    7. Further reading
  27. Chapter 15: Getting Started with Vertical Slice Architecture
    1. Vertical Slice Architecture
      1. What are the advantages and disadvantages?
      2. Anti-pattern: Big Ball of Mud
    2. Project: Vertical Slice Architecture
    3. Continuing your journey
    4. Summary
    5. Questions
    6. Further reading
  28. Chapter 16: Introduction to Microservices Architecture
    1. What are microservices?
      1. Cohesive unit of business
      2. Own its data
      3. Independence
    2. Getting started with message queues
      1. Conclusion
    3. An overview of events
      1. Domain events
      2. Integration events
    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. An overview of containers
      1. Docker
      2. Docker Compose
      3. Orchestration
      4. Scaling
      5. Conclusion
    8. Summary
    9. Questions
    10. Further reading
  29. Section 5: Designing the Client Side
  30. Chapter 17: 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
      2. Tag Helpers
      3. View components
    3. C# 9 features
      1. Top-level statements (C# 9)
      2. Target-typed new expressions (C# 9)
      3. Init-only properties (C# 9)
      4. Record classes (C# 9)
      5. Conclusion
    4. Display and Editor Templates
      1. Display Templates
      2. Editor Templates
      3. Project : Composite BookStore revisited
      4. Conclusion
    5. Summary
    6. Questions
    7. Further reading
  31. Chapter 18: A Brief Look into Blazor
    1. An overview of Blazor Server
    2. Overview of Blazor WebAssembly
    3. Getting familiar with Razor components
      1. Creating Razor 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
  32. 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
  33. Acronyms Lexicon
  34. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: An Atypical ASP.NET Core 5 Design Patterns Guide
  • Author(s): Carl-Hugo Marcotte, Abdelhamid Zebdi
  • Release date: December 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781789346091