
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-05-23 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” (@dddbook) by @theUniC, @keyvanakbary and @buenosvinos https://leanpub.com/ddd-in-php #ddd #php 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 .............................................. ii Vaughn Vernon ........................................ ii Matthias Noback ........................................ ii Preface ............................................... iii Who should read this book .................................. iv DDD and PHP Community .................................. iv Summary of Chapters ..................................... v Chapter 1: Getting Started with DDD ......................... v Chapter 2: Architectural Styles ............................. v Chapter 3: Value Objects ................................ v Chapter 4: Entities .................................... v Chapter 5: Domain Services ............................... v Chapter 6: Domain Events ............................... vi Chapter 7: Modules ................................... vi Chapter 8: Aggregates .................................. vi Chapter 9: Factories ................................... vi Chapter 10: Repositories ................................. vi Chapter 11: Application ................................. vi Chapter 12: Integrating Bounded Contexts ....................... vi Appendix A: Hexagonal Architecture with PHP . vii Code and examples ...................................... vii Acknowledgements ........................................ viii About the Authors ......................................... ix Carlos Buenosvinos ...................................... ix Christian Soronellas ...................................... ix Keyvan Akbary ........................................ ix 1. Getting Started with DDD .................................. 1 1.1 Why Domain-Driven Design? ............................ 1 1.2 How Domain-Driven Design helps? ......................... 1 1.2.1 Ubiquitous Language .............................. 2 CONTENTS 1.3 Should I start considering Domain-Driven Design as an option? .......... 2 1.4 Main challenges of applying Domain-Driven Design ................ 3 1.5 The business value of using Domain-Driven Design ................ 3 1.6 Strategical overview .................................. 3 1.7 Other related movements: Microservices and Self-Contained Systems . 4 1.8 Wrap-up ........................................ 6 2. Architectural Styles ...................................... 7 2.1 The Good Old Times ................................. 7 2.2 Layered Architecture ................................. 10 2.2.1 Model-View-Controller ............................. 11 2.2.2 Example of Layered Architecture ........................ 12 2.2.2.1 The Model ................................. 12 2.2.2.2 The View ................................. 15 2.2.2.3 The Controller ............................... 16 2.3 Inverting Dependencies: Hexagonal Architecture . 17 2.3.1 The Dependency Inversion Principle (DIP) ................... 18 2.3.2 Applying Hexagonal Architecture ........................ 19 2.4 Command Query Responsibility Segregation .................... 21 2.4.1 The Write Model ................................. 22 2.4.2 The Read Model ................................. 26 2.4.3 Synchronizing the Write Model with the Read Model . 28 2.5 Event Sourcing ..................................... 34 2.6 Wrap-up ........................................ 40 3. Value Objects ......................................... 42 3.1 Definition ....................................... 42 3.2 Value Object vs Entity ................................. 43 3.3 Currency and Money Example ............................ 43 3.4 Characteristics ..................................... 45 3.4.1 Measures, Quantifies, or Describes ....................... 46 3.4.2 Immutability ................................... 46 3.4.3 Conceptual Whole ................................ 48 3.4.4 Value Equality .................................. 49 3.4.5 Replaceability .................................. 51 3.4.6 Side-Effect-Free Behaviour ........................... 51 3.5 Basic Types ...................................... 53 3.6 Testing Value Objects ................................. 54 3.7 Persisting Value Objects ................................ 55 3.7.1 Persisting Single Value Objects ......................... 56 3.7.1.1 Embedded Value with an ad-hoc ORM . 57 3.7.1.2 Embedded Value (Embeddables) with Doctrine >= 2.5.* . 59 3.7.1.3 Embedded Value with Doctrine <= 2.4.* . 62 CONTENTS 3.7.1.4 Serialized LOB and ad-hoc ORM ..................... 64 3.7.1.4.1 Improved Serialization with JMS Serializer . 66 3.7.1.5 Serialized LOB with Doctrine ...................... 67 3.7.1.5.1 Doctrine Object Mapping Type . 67 3.7.1.5.2 Doctrine Custom Types ..................... 69 3.7.2 Persisting a Collection of Value Objects ..................... 72 3.7.2.1 Collection Serialized into a Single Column . 73 3.7.2.2 Collection backed by a Join Table .................... 74 3.7.2.2.1 Collection backed by a Join Table with Doctrine . 75 3.7.2.2.2 Collection backed by a Join Table with ad-hoc ORM . 79 3.7.2.3 Collection backed by a Database Entity . 79 3.7.3 NoSQL ...................................... 79 3.7.3.1 MySQL JSON Type and PostgreSQL JSONB . 79 3.8 Security ........................................ 80 3.9 Wrap-up ........................................ 80 4. Entities ............................................. 81 4.1 Introduction ...................................... 81 4.2 Objects vs Primitive types ............................... 83 4.3 Identity Operation ................................... 85 4.3.1 Persistence Mechanism Generates Identity ................... 85 4.3.1.1 Surrogate Identity ............................. 86 4.3.1.2 Active Record vs Data Mapper for Rich Domain Models . 88 4.3.2 Client Provides Identity ............................. 88 4.3.3 Application Generates Identity ......................... 90 4.3.4 Other Bounded Context Generates Identity . 92 4.4 Persisting Entities ................................... 93 4.4.1 Setting Up Doctrine ............................... 93 4.4.2 Mapping Entities ................................. 93 4.4.2.1 Mapping Entities Using Annotated Code . 94 4.4.2.2 Mapping Entities using XML ....................... 96 4.4.2.3 Mapping Entity Identity ......................... 97 4.4.2.4 Final Mapping file ............................. 99 4.5 Testing Entities .................................... 99 4.6 Validation .......................................103 4.6.1 Attribute Validation ...............................103 4.6.2 Entire Object Validation .............................105 4.6.2.1 Decoupling Validation Messages . 108 4.6.3 Validating Object Compositions . 109 4.7 Entities and Domain Events .............................109 4.8 Wrap-up ........................................111 5. Services ............................................ 113 CONTENTS 5.1 Application Services ..................................113 5.2 Domain Services ....................................116 5.3 Domain Services and Infrastructure Services . 118 5.3.1 An Issue on Code Reuse .............................121 5.4 Testing Domain Services ...............................123 5.5 Anemic Domain Models vs Rich Domain Models . 125 5.5.1 Anemic Domain Model Breaks Encapsulation . 130 5.5.2 Anemic Domain Model Brings a False Sense of Code Reuse . 130 5.5.3 How to Avoid Anemic Domain Model . 131 5.6 Wrap-up ........................................131 6. Domain Events ........................................ 133 6.1 Introduction ......................................133 6.2 Definition .......................................134 6.2.1 Short story ....................................134 6.2.2 Metaphor .....................................135 6.2.3 Real Worldâ„¢ Example .............................135 6.3 Characteristics .....................................137 6.3.1 Naming Conventions ..............................138 6.3.2 Domain Events and Ubiquitous Language . 138 6.3.3 Immutability ...................................138 6.4 Modeling Events ....................................138 6.5 Doctrine Events ....................................141 6.6 Persisting Domain Events ...............................143 6.6.1 Event Store ....................................143 6.7 Publishing Events from the Domain Model . 147 6.7.1 Publishing a Domain Event from an Entity . 147 6.7.2 Publishing your Domain Events from Domain or
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages361 Page
-
File Size-