Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Part I. Strategic Design 1. Analyzing Business Domains. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 What Is a Business Domain? What Is a Subdomain? Types of Subdomains Comparing Subdomains Identifying Subdomain Boundaries Domain Analysis Examples Gigmaster BusVNext Who Are the Domain Experts? Conclusion Exercises 3 4 4 7 11 14 14 15 17 18 18 2. Discovering Domain Knowledge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Business Problems Knowledge Discovery Communication What Is a Ubiquitous Language? Language of the Business 21 22 22 24 25 v Scenarios Consistency Model of the Business Domain What Is a Model? Effective Modeling Modeling the Business Domain Continuous Effort Tools Challenges Conclusion Exercises 25 26 27 27 28 28 29 29 30 31 32 3. Managing Domain Complexity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Inconsistent Models What Is a Bounded Context? Model Boundaries Ubiquitous Language Refined Scope of a Bounded Context Bounded Contexts Versus Subdomains Subdomains Bounded Contexts The Interplay Between Subdomains and Bounded Contexts Boundaries Physical Boundaries Ownership Boundaries Bounded Contexts in Real Life Semantic Domains Science Buying a Refrigerator Conclusion Exercises 33 35 36 37 37 38 39 39 39 41 41 42 42 43 43 44 46 46 4. Integrating Bounded Contexts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Cooperation Partnership Shared Kernel Customer–Supplier Conformist Anticorruption Layer Open-Host Service Separate Ways Communication Issues vi | Table of Contents 50 50 50 53 53 54 55 56 56 Generic Subdomains Model Differences Context Map Maintenance Limitations Conclusion Exercises 56 56 57 58 58 59 59 Part II. Tactical Design 5. Implementing Simple Business Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Transaction Script Implementation It’s Not That Easy! When to Use Transaction Script Active Record Implementation When to Use Active Record Be Pragmatic Conclusion Exercises 63 64 64 68 69 70 71 72 72 72 6. Tackling Complex Business Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 History Domain Model Implementation Building Blocks Managing Complexity Conclusion Exercises 75 76 77 77 94 95 96 7. Modeling the Dimension of Time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Event Sourcing Search Analysis Source of Truth Event Store Event-Sourced Domain Model Advantages Disadvantages Frequently Asked Questions 99 104 105 107 107 108 110 111 112 Table of Contents | vii Performance Deleting Data Why Can’t I Just…? Conclusion Exercises 112 114 114 115 116 8. Architectural Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Business Logic Versus Architectural Patterns Layered Architecture Presentation Layer Business Logic Layer Data Access Layer Communication Between Layers Variation When to Use Layered Architecture Ports & Adapters Terminology Dependency Inversion Principle Integration of Infrastructural Components Variants When to Use Ports & Adapters Command-Query Responsibility Segregation Polyglot Modeling Implementation Projecting Read Models Challenges Model Segregation When to Use CQRS Scope Conclusion Exercises 117 118 118 119 119 120 121 124 125 126 126 127 128 128 128 129 129 130 132 133 133 134 135 135 9. Communication Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Model Translation Stateless Model Translation Stateful Model Translation Integrating Aggregates Outbox Saga Process Manager Conclusion Exercises viii | Table of Contents 137 138 141 143 145 147 150 154 154 Part III. Applying Domain-Driven Design in Practice 10. Design Heuristics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Heuristic Bounded Contexts Business Logic Implementation Patterns Architectural Patterns Testing Strategy Testing Pyramid Testing Diamond Reversed Testing Pyramid Tactical Design Decision Tree Conclusion Exercises 159 160 161 163 164 165 165 165 166 167 167 11. Evolving Design Decisions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Changes in Domains Core to Generic Generic to Core Supporting to Generic Supporting to Core Core to Supporting Generic to Supporting Strategic Design Concerns Tactical Design Concerns Transaction Script to Active Record Active Record to Domain Model Domain Model to Event-Sourced Domain Model Generating Past Transitions Modeling Migration Events Organizational Changes Partnership to Customer–Supplier Customer–Supplier to Separate Ways Domain Knowledge Growth Subdomains Bounded Contexts Aggregates Conclusion Exercises 169 170 170 171 171 172 172 172 173 174 174 176 176 177 178 179 179 179 180 180 181 182 182 183 Table of Contents | ix 12. EventStorming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 What Is EventStorming? Who Should Participate in EventStorming? What Do You Need for EventStorming? The EventStorming Process Step 1: Unstructured Exploration Step 2: Timelines Step 3: Pain Points Step 4: Pivotal Events Step 5: Commands Step 6: Policies Step 7: Read Models Step 8: External Systems Step 9: Aggregates Step 10: Bounded Contexts Variants When to Use EventStorming Facilitation Tips Watch the Dynamics Remote EventStorming Conclusion Exercises 185 186 186 187 187 188 189 190 190 191 192 193 194 194 195 196 196 197 197 198 198 13. Domain-Driven Design in the Real World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Strategic Analysis Understand the Business Domain Explore the Current Design Modernization Strategy Strategic Modernization Tactical Modernization Cultivate a Ubiquitous Language Pragmatic Domain-Driven Design Selling Domain-Driven Design Undercover Domain-Driven Design Conclusion Exercises Part IV. 202 202 203 204 205 207 207 210 211 211 213 214 Relationships to Other Methodologies and Patterns 14. Microservices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 What Is a Service? x | Table of Contents 217 What Is a Microservice? Method as a Service: Perfect Microservices? Design Goal System Complexity Microservices as Deep Services Microservices as Deep Modules Domain-Driven Design and Microservices’ Boundaries Bounded Contexts Aggregates Subdomains Compressing Microservices’ Public Interfaces Open-Host Service Anticorruption Layer Conclusion Exercises 218 219 220 221 222 223 225 225 227 228 229 229 230 231 232 15. Event-Driven Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Event-Driven Architecture Events Events, Commands, and Messages Structure Types of Events Designing Event-Driven Integration Distributed Big Ball of Mud Temporal Coupling Functional Coupling Implementation Coupling Refactoring the Event-Driven Integration Event-Driven Design Heuristics Conclusion Exercises 233 234 234 235 236 241 241 242 243 243 243 245 246 247 16. Data Mesh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Analytical Data Model Versus Transactional Data Model Fact Table Dimension Table Analytical Models Analytical Data Management Platforms Data Warehouse Data Lake Challenges of Data Warehouse and Data Lake Architectures Data Mesh 249 250 252 253 254 254 257 258 259 Table of Contents | xi Decompose Data Around Domains Data as a Product Enable Autonomy Build an Ecosystem Combining Data Mesh and Domain-Driven Design Conclusion Exercises 259 261 262 262 263 264 265 Closing Words. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 A. Applying DDD: A Case Study. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 B. Answers to Exercise Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 xii | Table of Contents