Domain-Driven Design in PHP Real Examples Written in PHP Showcasing DDD Architectural Styles, Tactical Design, and Bounded Context Integration
Total Page:16
File Type:pdf, Size:1020Kb
Domain-Driven Design in PHP Real examples written in PHP showcasing DDD Architectural Styles, Tactical Design, and Bounded Context Integration Carlos Buenosvinos, Christian Soronellas and Keyvan Akbary This book is for sale at http://leanpub.com/ddd-in-php This version was published on 2016-10-07 ISBN 978-0-9946084-1-3 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2014 - 2016 Carlos Buenosvinos, Christian Soronellas and Keyvan Akbary Tweet This Book! Please help Carlos Buenosvinos, Christian Soronellas and Keyvan Akbary by spreading the word about this book on Twitter! The suggested tweet for this book is: I just bought “Domain-Driven Design in #PHP” by @buenosvinos, @theUniC and @keyvanakbary https://leanpub.com/ddd-in- php?utm_source=social&utm_medium=twitter&utm_campaign=book_buy @dddbook #DDDesign The suggested hashtag for this book is #DDDinPHP. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#DDDinPHP Contents Foreword by Matthias Noback .................................. ii Preface ............................................... iv Who Should Read This Book ................................. v DDD and PHP Community .................................. v Summary of Chapters ..................................... vi Code and Examples ......................................viii Acknowledgements ........................................ ix About the Authors ......................................... xi Carlos Buenosvinos ...................................... xi Christian Soronellas ...................................... xi Keyvan Akbary ........................................ xii Getting Started with Domain-Driven Design ......................... 1 Why Domain-Driven Design Matters ............................. 1 The Three Pillars of Domain-Driven Design ......................... 2 Considering Domain-Driven Design ............................. 3 The Tricky Parts ........................................ 4 Strategical Overview ..................................... 4 Related Movements: Microservices and Self-Contained Systems .............. 5 Wrap-Up ............................................ 7 Architectural Styles ........................................ 8 The Good Old Days ...................................... 8 Layered Architecture ..................................... 11 Inverting Dependencies: Hexagonal Architecture ...................... 18 Command Query Responsibility Segregation (CQRS) .................... 22 Event Sourcing ......................................... 35 Wrap-Up ............................................ 42 Value Objects ........................................... 44 Definition ........................................... 44 Value Object vs. Entity .................................... 45 CONTENTS Currency and Money Example ................................ 45 Characteristics ......................................... 47 Basic Types ........................................... 55 Testing Value Objects ..................................... 56 Persisting Value Objects .................................... 57 Security ............................................. 82 Wrap-Up ............................................ 83 Entities ............................................... 84 Introduction .......................................... 84 Objects vs. Primitive Types .................................. 86 Identity Operation ....................................... 88 Persisting Entities ....................................... 96 Testing Entities .........................................103 Validation ...........................................114 Entities and Domain Events ..................................120 Wrap-Up ............................................122 Services ............................................... 123 Application Services ......................................123 Domain Services ........................................126 Domain Services and Infrastructure Services . 128 Testing Domain Services ...................................133 Anemic Domain Models vs. Rich Domain Models . 135 Wrap-Up ............................................141 Domain Events ........................................... 143 Introduction ..........................................143 Definition ...........................................144 Characteristics .........................................147 Modeling Events ........................................148 Doctrine Events ........................................151 Persisting Domain Events ...................................153 Publishing Events from the Domain Model . 157 Spreading the News to Remote Bounded Contexts . 166 Wrap-Up ............................................179 Modules .............................................. 180 General Overview .......................................180 Leverage Modules in PHP ...................................180 Bounded Contexts and Applications .............................184 Structuring Code in Modules .................................186 Wrap-Up ............................................197 CONTENTS Aggregates ............................................. 198 Introduction ..........................................198 Key Concepts ..........................................199 What Is an Aggregate? ....................................210 Why Aggregates? .......................................212 A Bit of History ........................................214 Anatomy of an Aggregate ...................................214 Aggregate Design Rules ....................................216 Sample Application Service: User and Wishes . 221 Transactions ..........................................242 Wrap Up ............................................242 Factories .............................................. 244 Factory Method on Aggregate Root ..............................244 Factory on Service .......................................246 Testing Factories ........................................255 Wrap-Up ............................................261 Repositories ............................................ 262 Definition ...........................................262 Repositories Are Not DAOs ..................................262 Collection-Oriented Repositories ...............................264 Persistence-Oriented Repository ...............................276 Extra Behavior .........................................282 Querying Repositories .....................................284 Managing Transactions ....................................288 Testing Repositories ......................................291 Testing Your Services with In-Memory Implementations . 294 Wrap-Up ............................................295 Application ............................................. 296 Requests ............................................296 Anatomy of an Application Service ..............................300 Testing Application Services .................................315 Transactions ..........................................317 Security .............................................319 Domain Events .........................................320 Command Handlers ......................................320 Wrap-Up ............................................322 Integrating Bounded Contexts .................................. 323 Integration Through the Data Store ..............................323 Integration Relationships ...................................324 Implementing Bounded Context Integrations . 326 CONTENTS Wrap-Up ............................................336 The End ............................................... 337 Bibliography ............................................ 338 Appendix: Hexagonal Architecture with PHP ......................... 339 Introduction ..........................................339 First Approach .........................................339 Repositories and the Persistence Edge ............................341 Decoupling Business and Persistence .............................344 Migrating our Persistence to Redis ..............................345 Decouple Business and Web Framework . 347 Rating an idea using the API .................................350 Console app rating .......................................351 Testing Rating an Idea UseCase ................................353 Testing Infrastructure .....................................357 Arggg, So Many Dependencies! ................................359 Domain Services and Notification Hexagon Edge . 360 Let’s Recap ...........................................361 Hexagonal Architecture ....................................362 Key Points ...........................................362 What’s Next? ..........................................362 CONTENTS i This book is dedicated to my dearest Vanessa, and to Valentina and Gabriela. Thanks for your love, your support, and your patience. –Carlos To my dear Elena. Without your encouragement, your love, and your patience, this book would not have been possible. –Christian To my parents, John and Mercedes, who raised me free of constraints. This will be the first book of many. To my love, Clara, for your unconditional support and infinite patience. –Keyvan Foreword