Domain-driven Design Deconstructed

Domain-driven Design Deconstructed

Presented at php[tek] 2016

Once you try domain-driven design (DDD), you will never design software in the same way again. We will avoid silly buzzwords and break DDD down into easy-to-understand components. We will start by discussing what it means to use a ubiquitous language, encapsulate logic in value objects, and use bounded contexts, entities, and aggregate roots to manage state and protect invariants. We will also cover more-advanced topics in the DDD world, such as event sourcing and command query responsibility segregation.

Be9645e99392d37fd0e8018f22122fc3?s=128

Andrew Cassell

May 26, 2016
Tweet

Transcript

  1. Domain-driven Design Deconstructed Domain-driven Design Deconstructed Andrew Cassell

  2. None
  3. None
  4. Domain-driven Design

  5. None
  6. None
  7. None
  8. None
  9. None
  10. Domain-driven Design

  11. DDD

  12. None
  13. None
  14. People don't want to buy a quarter-inch drill, they want

    a quarter-inch hole. Theodore Levitt
  15. None
  16. None
  17. None
  18. #DDD4ME

  19. None
  20. None
  21. None
  22. None
  23. Common Sense Software Development

  24. Domain

  25. None
  26. http://www.sei.cmu.edu/reports/95tr021.pdf

  27. None
  28. DDD BALL OF MUD/CRUD Correctness Testability Usability Maintainability Modifiability

  29. Performance

  30. Security

  31. DDD Common Misconceptions

  32. None
  33. None
  34. None
  35. None
  36. None
  37. None
  38. None
  39. None
  40. CRUD is okay-ish

  41. None
  42. None
  43. None
  44. None
  45. Ubiquitous Language

  46. Ubiquitous Language Developers Domain Experts

  47. None
  48. None
  49. Ubiquitous Language

  50. Customer Client Member Affiliate

  51. 1. Naming Things 2. Cache Invalidation 3. Off By One

    Errors Top 10 Reasons Programming is Hard
  52. DDD CRUD/MUD Members Users LibraryCatalog BookRepository ISBN String DeweyDecimal Float(7,3)

  53. $catalog = new LibraryCatalog(…); $book = $catalog->findOneBookByISBN($isbn); $members = new

    Members(…); $borrower = $members->findOneByAccountNumber($id); $borrower->borrow($book); $members->updateAccount($borrower);
  54. $user = User::findOrFail($uuid);
 $book = Books::where('isbn', '=', $isbn)->take(1)->get();
 
 


    if (!$user->userBookLinks()->contains($book->id) && $user->borrowLinks->count() < 5) {
 
 $user->userBookLinks()->attach($book->id, ['date_due' => new DateTime('+2 Weeks')];
 
 }
 
 
 $user->save();
  55. None
  56. $catalog = new LibraryCatalog(…); $book = $catalog->findOneBookByISBN($isbn); $members = new

    Members(…); $borrower = $members->findOneByAccountNumber($id); $borrower->borrow($book)
  57. $borrower->borrow($book); $borrower->loan($book); $borrower->obtain($book); $borrower->take($book);

  58. DDD BALL OF MUD/CRUD Correctness ✓ Testability Usability Maintainability Modifiability

  59. None
  60. None
  61. Sales Marketing Finance Production

  62. Customer Lead Payee ShipTo

  63. Slide: Vaughn Vernon

  64. None
  65. None
  66. None
  67. Alberto Brandolini

  68. http://ziobrando.blogspot.be/

  69. None
  70. None
  71. None
  72. None
  73. None
  74. 7 Dirty Words When Meeting With a Domain Expert 1.Session

    2.Repository 3.Abstract 4.Interface 5.Class 6.Database 7.Foreign Key
  75. None
  76. None
  77. Photo: Mathias Verraes

  78. None
  79. None
  80. None
  81. None
  82. None
  83. None
  84. None
  85. None
  86. None
  87. None
  88. None
  89. None
  90. None
  91. Domain Objects Repositories Commands & Command handlers Business rules/specifications Messages

    Domain events 
 Aggregates Entities Value Objects Invariants Processes
  92. Domain Objects Repositories Commands & Command handlers Business rules/specifications Messages

    Domain events 
 Aggregates Entities Value Objects Invariants Processes
  93. Value Object Immutable No Identity only Values Entity Mutable Has

    A Lifecycle Always Stable Contains Value Objects Aggregate Entity Responsible For Child Entities
  94. Value Objects Amount Paid Email Address BookTitle Dewey Decimal Number

    ISBN Number Domain Event
  95. Value Objects

  96. Immutability Avoid Spooky Action at a Distance

  97. Value Objects

  98. Not Just Static Typing https://github.com/Fiedzia/type-system-research/blob/master/README.md Value Objects

  99. “Gateway Drug to Test Driven Development” Value Objects #MYTESTSDONTPASS

  100. Plain PHP Objects (POPOs) That are Immutable •Declare Class Properties

    as Private •No Setters •All Parameters Are Injected in the Constructor •Do Not Store References to Mutable Objects (Private Scalars Okay) •Functions that Appear to Modify Should Return a New Object •Declare the Class Final so it can’t be Overridden (probably) Value Objects
  101. Example BookTitle

  102. None
  103. None
  104. None
  105. None
  106. None
  107. None
  108. None
  109. Example BookCondition

  110. None
  111. None
  112. None
  113. None
  114. ISBN DeweyDecimal …

  115. Example DateBorrowed

  116. None
  117. Example LoanPeriod

  118. None
  119. DDD CRUD/MUD BookTitle String BookCondition String ISBN String DeweyDecimal Float(7,3)

    DateBorrowed DateTime
  120. DDD BALL OF MUD/CRUD Correctness ✓ Testability ✓ Usability Maintainability

    Modifiability
  121. Identifiable Has State and is Mutable Never Invalid State Required

    Parameters Passed in Constructor Operates Using Value Objects No Security or Permission Checks Storage Agnostic Entities
  122. Entities Librarian Book

  123. None
  124. None
  125. None
  126. SETTERS ARE BAD

  127. None
  128. None
  129. None
  130. None
  131. None
  132. None
  133. Doctrine

  134. None
  135. •Special Kind of Entity •Manages Child Entities •Transactionally Safe Aggregates

  136. Aggregates Member Fines Invoice

  137. None
  138. None
  139. None
  140. None
  141. None
  142. Task Based UIs

  143. None
  144. None
  145. DDD BALL OF MUD/CRUD Correctness ✓ Testability ✓ Usability ✓

    Maintainability Modifiability
  146. Security

  147. Domain (Business Logic) Application Services Controller Persistence Event/Command Bus Security

    Templates
  148. Controller Domain Framework

  149. None
  150. None
  151. None
  152. None
  153. Controller Domain Framework

  154. Mathias Verraes - Decoupling the Model from the Framework at

    Laracon EU 2014 https://www.youtube.com/watch?v=QaIGN_cTcc8
  155. Ruby Midwest 2011 - Keynote: Architecture the Lost Years by

    Robert Martin https://www.youtube.com/watch?v=WpkDN78P884
  156. Domain (Business Logic) Application Services Controller Persistence Event/Command Bus Security

    Templates
  157. DDD BALL OF MUD/CRUD Correctness ✓ Testability ✓ Usability ✓

    Maintainability ✓ Modifiability
  158. Domain Events

  159. Domain Events •Something That Happened In The Past •Immutable •Part

    of the Core Domain •Usually Created in Either an Aggregate or as Part of Application Service •Probably Want To Persist Them •Passed to Event Bus (Application Service) •Listeners are Application Services •Used To Cross Bounded Contexts
  160. None
  161. None
  162. BookWasOverdue

  163. None
  164. None
  165. DDD BALL OF MUD/CRUD Correctness ✓ Testability ✓ Usability ✓

    Maintainability ✓ Modifiability ✓
  166. Command Query Responsibility Segregation

  167. CQRS

  168. Slide: Microsoft

  169. None
  170. None
  171. None
  172. None
  173. https://gnugat.github.io/ 2016/05/11/towards-cqrs- command-bus.html

  174. None
  175. None
  176. None
  177. Controller Domain Framework

  178. Event Sourcing

  179. Event Sourcing •Store the Events That Happen •Append Only Event

    Storage •Object Properties are Not Stored
  180. None
  181. None
  182. None
  183. Event Sourcing •Avoids Data Mapping •Avoids Object-relational Impedance Mismatch •Reduces

    Database Table Counts (Related Tables) •Potentially Reduces Model Counts
  184. Slide: Microsoft

  185. None
  186. None
  187. None
  188. None
  189. Libraries •https://github.com/szjani/predaddy •https://github.com/prooph •https://github.com/qandidate-labs/broadway

  190. None
  191. None
  192. Learning More

  193. None
  194. DevBookClub https://www.youtube.com/user/devbookclub

  195. None
  196. None
  197. None
  198. None
  199. None
  200. DDDinPHP Google Group http://DDDinPHP.org

  201. https://joind.in/talk/99c3a Topics Covered •Ubiquitous Language •Event Storming •Modelling •Value Objects

    •Entities •Aggregates •Hexagonal Architecture •CQRS •Event Sourcing