0%

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
    3. Section 1, Principles and Methodologies
    4. Section 2, Designing for ASP.NET Core
    5. Section 3, Designing at Component Scale
    6. Section 4, Designing at Application Scale
    7. Section 5, Designing the Client Side
    8. To get the most out of this book
    9. Download the example code files
    10. Conventions used
    11. Get in touch
    12. 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
    3. Anti-patterns
    4. Code smells
    5. Understanding the web – Request/Response
    6. Getting started with .NET
    7. .NET SDK versus runtime
    8. .NET 5 versus .NET Standard
    9. Visual Studio Code versus Visual Studio versus the command-line interface (CLI)
    10. Technical requirements
    11. Summary
    12. Questions
    13. 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
    4. How do you create an xUnit test project?
    5. Basic features of xUnit
    6. How to organize your tests
    7. How is it easier?
    8. Summary
    9. Questions
    10. Further reading
  12. Chapter 3: Architectural Principles
    1. The SOLID principles
    2. Single responsibility principle (SRP)
    3. Open/Closed principle (OCP)
    4. Liskov substitution principle (LSP)
    5. Interface segregation principle (ISP)
    6. Dependency inversion principle (DIP)
    7. Other important principles
    8. Separation of concerns
    9. Don't repeat yourself (DRY)
    10. Summary
    11. 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
    3. Directory structure
    4. Structure of a controller
    5. Default routing
    6. Project: MVC
    7. Conclusion
    8. View Model design pattern
    9. Goal
    10. Design
    11. Project: View models (a list of students)
    12. Project: View models (a student form)
    13. Conclusion
    14. Summary
    15. Questions
    16. Further reading
  15. Chapter 5: The MVC Pattern for Web APIs
    1. An overview of REST
    2. Request HTTP methods
    3. Response status code
    4. Anatomy of a web API
    5. Setting up a web API
    6. Attribute routing
    7. Returning values
    8. C# features
    9. Class conversion operators (C#)
    10. Local functions (C# 7) and a static local function (C# 8)
    11. The Data Transfer Object design pattern
    12. Goal
    13. Design
    14. Project – DTO
    15. API contracts
    16. Analyzing the DTO sample
    17. Project – OpenAPI
    18. Project – API contracts
    19. Idea – Creating a typed client library
    20. One last observation
    21. Summary
    22. Questions
    23. Further reading
  16. Chapter 6: Understanding the Strategy, Abstract Factory, and Singleton Design Patterns
    1. The Strategy design pattern
    2. Goal
    3. Design
    4. Project: Strategy
    5. Conclusion
    6. A brief look at a few C# features
    7. Default literal expressions (C# 7.1)
    8. Switch expressions (C# 8)
    9. Discards (C# 7)
    10. The Abstract Factory design pattern
    11. Goal
    12. Design
    13. Project: AbstractVehicleFactory
    14. Project: MiddleEndVehicleFactory
    15. Conclusion
    16. The Singleton design pattern
    17. Goal
    18. Design
    19. An alternate (better) way
    20. Code smell: Ambient Context
    21. Conclusion
    22. Summary
    23. Questions
  17. Chapter 7: Deep Dive into Dependency Injection
    1. What is Dependency Injection?
    2. The composition root
    3. Extending IServiceCollection
    4. Object lifetime
    5. Code smell: Control Freak
    6. Using external IoC containers
    7. Revisiting the Strategy pattern
    8. Constructor injection
    9. Property injection
    10. Method injection
    11. Project: Strategy
    12. Revisiting the Singleton pattern
    13. The application state
    14. Project: Wishlist
    15. Tuples (C# 7+)
    16. Understanding the Service Locator pattern
    17. Project: ServiceLocator
    18. Project: ServiceLocatorFixed
    19. Conclusion
    20. Revisiting the Factory pattern
    21. Factory mixed with method injection
    22. HomeViewModelFactory
    23. Summary
    24. Questions
    25. Further reading
  18. Chapter 8: Options and Logging Patterns
    1. An overview of the Options pattern
    2. Getting started
    3. Project – CommonScenarios
    4. Project – OptionsConfiguration
    5. Project – OptionsValidation
    6. Injecting options directly
    7. Conclusion
    8. Getting familiar with .NET logging abstractions
    9. About logging
    10. Writing logs
    11. Log levels
    12. Logging providers
    13. Configuring logging
    14. Conclusion
    15. Summary
    16. Questions
    17. Further reading
  19. Section 3: Designing at Component Scale
  20. Chapter 9: Structural Patterns
    1. Implementing the Decorator design pattern
    2. Goal
    3. Design
    4. Project: Adding behaviors
    5. Project: Decorator using Scrutor
    6. Conclusion
    7. Implementing the Composite design pattern
    8. Goal
    9. Design
    10. Project: BookStore
    11. Conclusion
    12. Implementing the Adapter design pattern
    13. Goal
    14. Design
    15. Project: Greeter
    16. Conclusion
    17. Implementing the Façade design pattern
    18. Goal
    19. Design
    20. Project: The façades
    21. Conclusion
    22. Summary
    23. Questions
    24. Further reading
  21. Chapter 10: Behavioral Patterns
    1. Implementing the Template Method pattern
    2. Goal
    3. Design
    4. Project – Building a search machine
    5. Conclusion
    6. Implementing the Chain of Responsibility pattern
    7. Goal
    8. Project – Message interpreter
    9. Project – Improved message interpreter
    10. Project – A final, finer-grained design
    11. Conclusion
    12. Summary
    13. Questions
  22. Chapter 11: Understanding the Operation Result Design Pattern
    1. Goal
    2. Design
    3. Project – Implementing different Operation Result patterns
    4. The consumer
    5. Its simplest form
    6. A single error message
    7. Adding a return value
    8. Multiple error messages
    9. Adding message severity
    10. Sub-classes and factories
    11. Advantages and disadvantages
    12. Advantages
    13. Disadvantages
    14. Summary
    15. Questions
    16. Further reading
  23. Section 4: Designing at Application Scale
  24. Chapter 12: Understanding Layering
    1. Introduction to layering
    2. Splitting the layers
    3. Layers versus tiers versus assemblies
    4. To be or not to be a purist?
    5. Sharing the model
    6. The reality of small- to medium-sized enterprises
    7. Responsibilities of the common layers
    8. Presentation
    9. Domain
    10. Data
    11. Abstract data layer
    12. Shared rich model
    13. Clean Architecture
    14. Summary
    15. Questions
    16. Further reading
  25. Chapter 13: Getting Started with Object Mappers
    1. Overview of object mapping
    2. Goal
    3. Design
    4. Project: Mapper
    5. Code smell: Too many dependencies
    6. Pattern – Aggregate Services
    7. Pattern – Mapping Façade
    8. Project – Mapping service
    9. Project – AutoMapper
    10. Summary
    11. Questions
    12. Further reading
  26. Chapter 14: Mediator and CQRS Design Patterns
    1. A high-level overview of Vertical Slice Architecture
    2. Implementing the Mediator pattern
    3. Goal
    4. Design
    5. Project – Mediator (IMediator)
    6. Project – Mediator (IChatRoom)
    7. Conclusion
    8. Implementing the CQRS pattern
    9. Goal
    10. Design
    11. Project: CQRS
    12. Code smell – marker interfaces
    13. Conclusion
    14. Using MediatR as a mediator
    15. Project – Clean Architecture with MediatR
    16. Conclusion
    17. Summary
    18. Questions
    19. Further reading
  27. Chapter 15: Getting Started with Vertical Slice Architecture
    1. Vertical Slice Architecture
    2. What are the advantages and disadvantages?
    3. Anti-pattern: Big Ball of Mud
    4. Project: Vertical Slice Architecture
    5. Continuing your journey
    6. Summary
    7. Questions
    8. Further reading
  28. Chapter 16: Introduction to Microservices Architecture
    1. What are microservices?
    2. Cohesive unit of business
    3. Own its data
    4. Independence
    5. Getting started with message queues
    6. Conclusion
    7. An overview of events
    8. Domain events
    9. Integration events
    10. Implementing the Publish-Subscribe pattern
    11. Message brokers
    12. The event sourcing pattern
    13. Example
    14. Conclusion
    15. Introducing Gateway patterns
    16. Gateway Routing pattern
    17. Gateway Aggregation pattern
    18. Backends for Frontends pattern
    19. Mixing and matching gateways
    20. Conclusion
    21. Revisiting the CQRS pattern
    22. Conclusion
    23. An overview of containers
    24. Docker
    25. Docker Compose
    26. Orchestration
    27. Scaling
    28. Conclusion
    29. Summary
    30. Questions
    31. Further reading
  29. Section 5: Designing the Client Side
  30. Chapter 17: ASP.NET Core User Interfaces
    1. Getting familiar with Razor Pages
    2. Design
    3. Routing
    4. Conclusion
    5. Organizing the user interface
    6. Partial views
    7. Tag Helpers
    8. View components
    9. C# 9 features
    10. Top-level statements (C# 9)
    11. Target-typed new expressions (C# 9)
    12. Init-only properties (C# 9)
    13. Record classes (C# 9)
    14. Conclusion
    15. Display and Editor Templates
    16. Display Templates
    17. Editor Templates
    18. Project : Composite BookStore revisited
    19. Conclusion
    20. Summary
    21. Questions
    22. 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
    4. Creating Razor components
    5. CSS isolation
    6. Component life cycle
    7. Event handling
    8. The Model-View-Update pattern
    9. Goal
    10. Design
    11. Project: Counter
    12. Conclusion
    13. A medley of Blazor features
    14. Summary
    15. Questions
    16. Further reading
    17. 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