An object has passed through several methods and services. What changed its state along the way? Which values are unchanged, and which have been overwritten? Immutable Data Design in C# starts with these everyday development challenges. The book shows you how to leave existing data unchanged and explicitly create a new version when an update is needed. This reduces the effort involved in tracking state, debugging, and testing.
.
Using an order processing system as a running example, this book guides you through gradually refactoring mutable objects into immutable data models. You will use C# features such as record, init, required, with expressions, immutable collections, and pattern matching to structure data creation, validation, and transformation, then apply these techniques to API inputs and outputs, application configuration objects, and model mapping.
.
As the examples progress, you will also update deeply nested data, assess performance costs, and consider the constraints imposed by ORMs and serialization. You will learn how to introduce immutable design incrementally into existing projects and evaluate whether AI-suggested refactorings solve real problems. An advanced chapter explores how state-specific types and union types can make invalid states unrepresentable. The goal is to help you understand when immutable design is appropriate and how to weigh its benefits against its costs.
.
Who This Book Is For
This book is for developers with C# experience who want to improve their data models, API design, and approach to state management. No background in functional programming, advanced type theory, or domain-driven design (DDD) is required.
.
About the Author
Michael Tsai has worked in software development and technical writing for many years and has been named a Microsoft .NET MVP for 21 consecutive years. He has written books on .NET 10 asynchronous programming, modern C#, and .NET dependency injection.
.
Table of Contents
Chapter 1: From mutable objects to immutable data
Chapter 2: C# records, init, and immutable collections
Chapter 3: Replacing in-place mutation with data transformation
Chapter 4: Data models for APIs, DTOs, and system boundaries
Chapter 5: Trade-offs and adoption of immutable design
Chapter 6: Ruling out invalid states with types (optional advanced reading)
Appendix A: Refactoring a project with immutable design