Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Domain-Driven Design - True North PHP 2016

Domain-Driven Design - True North PHP 2016

Andrew Cassell

November 04, 2016
Tweet

More Decks by Andrew Cassell

Other Decks in Programming

Transcript

  1. DDD

  2. 18

  3. People don't want to buy a quarter-inch drill, they want

    a quarter-inch hole. Theodore Levitt
  4. 1. Naming Things 2. Cache Invalidation 3. Off By One

    Errors Top 10 Reasons Programming is Hard
  5. $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();
  6. $catalog = new LibraryCatalog(…); $book = $catalog->findOneBookByISBN($isbn); $members = new

    Members(…); $borrower = $members->findOneByAccountNumber($id); $borrower->borrow($book); $members->updateAccount($borrower);
  7. 7 Dirty Words When Meeting With a Domain Expert 1.Session

    2.Repository 3.Abstract 4.Interface 5.Class 6.Database 7.Foreign Key
  8. Domain Objects Repositories Commands & Command handlers Business rules/specifications Messages

    Domain events 
 Aggregates Entities Value Objects Invariants Processes
  9. Domain Objects Repositories Commands & Command handlers Business Rules/Specifications Messages

    Domain Events 
 Aggregates Entities Value Objects Invariants Processes
  10. Value Object Immutable No Identity (Only Values) Entity Mutable Has

    A Lifecycle Always Stable Contains Value Objects Aggregate Entity Responsible For Child Entities
  11. Plain PHP Objects (POPOs) •Declare Class Properties as Private •No

    Setters •All Parameters Are Injected in the Constructor •Do Not Store References to Mutable Objects (Private Scalars Okay) •Throw Exceptions If Something Is Wrong •Functions that Appear to Modify Should Return a New Object •Declare the Class Final so it can’t be Overridden (probably) Value Objects
  12. 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
  13. Mathias Verraes - Decoupling the Model from the Framework at

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

    Robert Martin https://www.youtube.com/watch?v=WpkDN78P884
  15. Domain Events •Something That Happened In The Past •Immutable Value

    Objects •Part of the Core Domain •Usually Created in an Aggregate •Probably Want To Persist Them •Passed to Event Bus (Application Layer) •Listeners are Application Services •Used To Cross Bounded Contexts •Do Not Contain Entities (Only Value Objects)
  16. Event Sourcing •Store the Events That Happen •Append Only Event

    Storage •Object Properties are Not Stored
  17. Event Sourcing •Avoids Data Mapping •Avoids Object-relational Impedance Mismatch •Reduces

    Database Table Counts (Related Tables) •Potentially Reduces Model Counts
  18. https://joind.in/talk/82ad0 Topics Covered •Ubiquitous Language •Event Storming •Modelling •Value Objects

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