Implement design patterns in .NET 6 using the latest versions of the C# and F# languages. This book provides a comprehensive overview of the field of design patterns as they are used in today’s developer toolbox. In addition to the functional builder, asynchronous factory method, generic value adapter, and composite proxies, this new edition introduces topics such as Decorator Cycle Policies Functional Commands, a Transformer variation of the Visitor pattern, and factories that can perform Object Tracking and Bulk Replacement.
Using the C# and F# programming languages, Design Patterns in .NET 6 explores the classic design pattern implementations and discusses the applicability and relevance of specific language features for implementing patterns. You will learn by example, reviewing scenarios where patterns are applicable. Former C# MVP and patterns expert Dmitri Nesteruk demonstrates possible implementations of patterns, discusses alternatives and pattern relationships, and illustrates the way that a dedicated refactoring tool (JetBrains Rider) can be used to implement design patterns with ease.
What You Will Learn
- Become familiar with the latest pattern implementations available in C# 10 and F# 6
- Know how to better reason about software architecture
- Understand the process of refactoring code to patterns
- Refer to researched and proven variations of patterns
- Study complete, self-contained examples, including many that cover advanced scenarios
- Use the latest versions of C# and Visual Studio/Rider/ReSharper
Who This Book Is For
Developers who have some experience in the C# language and want to expand their comprehension of the art of programming by leveraging design approaches to solve modern problems
Author(s): Dmitri Nesteruk
Edition: 3
Publisher: Apress
Year: 2022
Language: English
Commentary: True PDF
Pages: 485
Table of Contents
About the Author
About the Technical Reviewer
Introduction
Part I: Introduction
Chapter 1: The SOLID Design Principles
Single Responsibility Principle
Open-Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Parameter Object
Dependency Inversion Principle
Chapter 2: The Functional Perspective
Function Basics
Functional Literals in C#
Storing Functions in C#
Functional Literals in F#
Composition
Functional-Related Language Features
Part II: Creational Patterns
Chapter 3: Builder
Scenario
Simple Builder
Fluent Builder
Static Initialization
Communicating Intent
Nested Builder and Immutability
Composite Builder
Builder Marker Interfaces
Stepwise Builder (Wizard)
Builder Parameter
Builder Extension with Recursive Generics
Lazy Functional Builder
Builder-Decorator
Scoping Builder Method
DSL Construction in F#
Summary
Chapter 4: Factories
Scenario
Factory Method
Asynchronous Factory Method
Factory
Inner Factory
Physical Separation
Abstract Factory
Delegate Factories in IoC
Object Tracking and Bulk Replacements
Object Tracking
Bulk Modifications
Functional Factory
Summary
Chapter 5: Prototype
Deep vs. Shallow Copying
ICloneable Is Bad
Deep Copying via Copy Construction
Note on Record Classes
Deep Copying with a Special Interface
Deep Copying and Inheritance
Deep Copying Guidelines
Trivially Copyable Types
Arrays
Common Collection Types
MemberwiseClone Is Not Terrible
Summary
Serialization
Prototype Factory
Source Generators
Summary
Chapter 6: Singleton
Singleton by Convention
Classic Implementation
Lazy Loading and Thread Safety
Reusable Base Class
The Trouble with Singleton
Per-Thread Singleton
Ambient Context
Uses in the .NET Framework
Singletons and Inversion of Control
Monostate
Multiton
Summary
Part III: Structural Patterns
Chapter 7: Adapter
Scenario
Adapter
Adapter Temporaries
The Problem with Hashing
Property Adapter (Surrogate)
Generic Value Adapter
Adapter in Dependency Injection
Bidirectional Adapter
Adapters in the .NET Framework
Summary
Chapter 8: Bridge
Conventional Bridge
Dynamic Prototyping Bridge
Summary
Chapter 9: Composite
Grouping Graphic Objects
Neural Networks
Shrink Wrapping the Composite
Composite Specification
Summary
Chapter 10: Decorator
The Basics of Delegation
Points and Lines
Adapter-Decorator
Simulating Multiple Inheritance
Multiple Inheritance with Interfaces
Multiple Inheritance with Default Interface Members
Dynamic Decorator Composition
Decorator Cycle Policies
Static Decorator Composition
Functional Decorator
Summary
Chapter 11: Façade
Magic Squares
Building a Trading Terminal
An Advanced Terminal
Where’s the Façade?
IoC Modules
Summary
Chapter 12: Flyweight
User Names
Text Formatting
Using Flyweights for Interop
Summary
Chapter 13: Proxy
Protection Proxy
Property Proxy
Composite Proxy: SoA/AoS
Composite Proxy with Array-Backed Properties
Virtual Proxy
Communication Proxy
Dynamic Proxy for Logging
Composite Proxy
Summary
Chapter 14: Value Object
Two-Dimensional Point
Percentage Value
Units of Measure
Summary
Part IV: Behavioral Patterns
Chapter 15: Chain of Responsibility
Scenario
Method Chain
Broker Chain
Functional Chain of Responsibility
Summary
Chapter 16: Command
Scenario
Implementing the Command Pattern
Undo Operations
Composite Commands (aka Macros)
Functional Command
Queries and Command-Query Separation
Summary
Chapter 17: Interpreter
Integer Parsing
Numeric Expression Evaluator
Lexing
Parsing
Using Lexer and Parser
Interpretation in the Functional Paradigm
Transpiler
Summary
Chapter 18: Iterator
Array-Backed Properties
Let’s Make an Iterator
Improved Iteration
Iterator Specifics
Iterator Adapter
Composite Iteration
Summary
Chapter 19: Mediator
Chat Room
Mediator with Events
Introduction to MediatR
Service Bus as Mediator
Summary
Chapter 20: Memento
Bank Account
Undo and Redo
Memento and Command
Summary
Chapter 21: Null Object
Scenario
Intrusive Approaches
Nullable Virtual Proxy
Null Object
Null Object Singleton
Dynamic Null Object
Drawbacks
Summary
Chapter 22: Observer
Events
Weak Event Pattern
Event Streams
Property Observers
Basic Change Notification
Bidirectional Bindings
Property Dependencies
Views
Case Study: Quadratic Equation Solver
Circular Recalculation Limitations
Observable Collections
Observable LINQ
Declarative Subscriptions in Autofac
Summary
Chapter 23: State
State-Driven State Transitions
Enum-Based State Machine
Switch-Based State Machine
Encoding Transitions with Switch Expressions
State Machines with Stateless
Types, Actions, and Ignoring Transitions
Reentrancy Again
Hierarchical States
More Features
Concurrent State Machines
Implicit State Machines
Summary
Chapter 24: Strategy
Dynamic Strategy
Static Strategy
Equality and Comparison Strategies
Functional Strategy
Declarative Strategies
Summary
Chapter 25: Template Method
Game Simulation
Template Method Mixin
Functional Template Method
Summary
Chapter 26: Visitor
Intrusive Visitor
Reflective Visitor
Extension Methods?
Functional Reflective Visitor (C#)
Functional Reflective Visitor (F#)
Improvements
What Is Dispatch?
Dynamic Visitor
Classic Visitor
Abstract Classes and Virtual Methods
Reducing Boilerplate
Implementing an Additional Visitor
Type Checks Are Unavoidable
Acyclic Visitor
Visitable Null Object
Visitor Adapter
Reductions and Transforms
Functional Visitor in F#
Summary
Index