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

Domain-Driven Design in PHP

Domain-Driven Design in PHP

Wojciech Sznapka

September 27, 2014
Tweet

More Decks by Wojciech Sznapka

Other Decks in Programming

Transcript

  1. Once upon the time,
    in ancient Greece...

    View Slide

  2. Hercules got into to
    serious troubles...

    View Slide

  3. King Eurystheus ordered
    Hercules to clean up
    Augeas' stables

    View Slide

  4. The stables was complex
    and full of mud

    View Slide

  5. As accounted for
    real hero, Hercules used
    nifty trick

    View Slide

  6. He changed flow of a
    river and directed it into
    two holes in
    opposite walls

    View Slide

  7. The task has been solved
    in a single day

    View Slide

  8. Unfortunately there’s no
    such heroic attempts in
    history of software
    development

    View Slide

  9. Instead of heroes, you
    need ...

    View Slide

  10. Domain-Driven Design
    in PHP
    Wojciech Sznapka
    PHPCon 2014

    View Slide

  11. Hi! I'm Wojciech Sznapka
    * Head Of Development @ Cherry Poland
    * Coding since 10 years
    * Interested in architecture, Big Data, Ice Hockey

    View Slide

  12. DDD essentials

    View Slide

  13. Domain-Driven Design is
    a way of developing
    complex software ...

    View Slide

  14. ... by focusing on core
    domain first ...

    View Slide

  15. ... modeling and
    abstracting reality using
    “building blocks” and
    proven
    design patterns ...

    View Slide

  16. and delaying technical
    decisions as much as
    possible.

    View Slide

  17. It’s all about the domain

    View Slide

  18. You can’t build properly
    working software
    without deep
    domain knowledge

    View Slide

  19. But, you won’t
    understand the whole
    domain at once.
    It's an iterative process

    View Slide

  20. You need
    evolving model

    View Slide

  21. It’s crucial to have
    common understanding
    from day one...

    View Slide

  22. … and to speak
    Ubiquitous Language
    with stakeholders and
    Domain Experts

    View Slide

  23. The model

    View Slide

  24. Good model
    act as
    Ubiquitous Language

    View Slide

  25. View Slide

  26. It reflect domain logic as
    much as possible

    View Slide

  27. It's build using layers
    * User interface
    * Application layer
    * Domain layer
    * Infrastructure layer

    View Slide

  28. * source: Domain-Driven Design Quickly by InfoQ

    View Slide

  29. User interface presents
    and interprets user
    commands

    View Slide

  30. Application layer
    coordinates activities.
    It's ought to be very thin

    View Slide

  31. Domain layer is the
    heart of the system,
    responsible for
    business logic

    View Slide

  32. Infrastructure layer
    acts as support,
    providing persistence,
    tools, etc.

    View Slide

  33. Let’s speak about
    entities, DDD first class
    citizens

    View Slide

  34. Entity represents a
    business being

    View Slide

  35. Entity is not a simple
    Data Transfer Object or
    data container

    View Slide

  36. Therefore avoid
    anemic entities . Good
    entity should provide
    behavior, besides
    identity and attributes!

    View Slide

  37. Infrastructure layer helps
    with persisting entities
    (using a backing service* of your choice)
    * http://12factor.net/backing-services

    View Slide

  38. View Slide

  39. Entities are supported by
    value objects

    View Slide

  40. Value objects
    conceptually does not
    provide any identity, so
    they can be shared

    View Slide

  41. To achieve that, value
    objects need to be
    immutable*
    * http://blog.sznapka.pl/immutable-value-objects-in-php

    View Slide

  42. The important fact:
    value objects are
    type hintable

    View Slide

  43. View Slide

  44. Large graph of entities
    can be difficult to handle
    consistently

    View Slide

  45. DDD introduced a term
    Aggregate which is
    collection of objects ...

    View Slide

  46. ... bound with
    aggregate root
    and hiding underlying
    objects from the
    external world

    View Slide

  47. Aggregates provides a
    coherent interface to
    operate on sub-graph of
    the model

    View Slide

  48. During development of
    ubiquitous language
    you’ll find that some
    actions don’t belong to
    any object

    View Slide

  49. Such situations are
    solved with
    domain services

    View Slide

  50. Services employ various
    entities, value objects,
    factories, repositories to
    do their job

    View Slide

  51. View Slide

  52. When dealing with
    complex objects, their
    creation could be
    complex and it's good to
    hide that logic

    View Slide

  53. Well known factories
    will do the trick

    View Slide

  54. Factory hides
    instantiation complexity
    and enables
    interchangeability in
    the future

    View Slide

  55. View Slide

  56. While factories help with
    objects creation, it is
    equally important to
    abstract objects
    retrieval

    View Slide

  57. Repository design
    pattern abstracts objects
    retrieval from a storage

    View Slide

  58. It’s extremely important
    to place all
    data access logic
    in repositories

    View Slide

  59. Infrastructure layer
    helps a lot here

    View Slide

  60. It uses your ORM library
    to build specific queries
    or use other vendors to
    obtain objects

    View Slide

  61. More important fact is
    that repository* is
    replaceable
    * like the most of DDD building blocks

    View Slide

  62. Model
    boundaries

    View Slide

  63. It’s important to draw
    bold borders inside your
    system, as it grows and
    becomes complex

    View Slide

  64. View Slide

  65. View Slide

  66. View Slide

  67. When different
    stakeholders start
    to confuse you with
    vocabulary

    View Slide

  68. It's good time to think
    about
    bounded contexts

    View Slide

  69. “Bounded Context is a
    central pattern in
    Domain-Driven Design”*
    * http://martinfowler.com/bliki/BoundedContext.html

    View Slide

  70. It’s quite sure, that
    bounded contexts will
    share common concepts

    View Slide

  71. Therefore create
    context map to outline
    points of contact
    between them

    View Slide

  72. Also, distinguish
    core domain from
    generic ones...

    View Slide

  73. Choose proper bounded
    context collaboration
    model

    View Slide

  74. Shared kernel, when
    two teams work closely
    on subset of domain

    View Slide

  75. They acts as partners,
    and access each other
    bounded context with
    common code

    View Slide

  76. Customer-Supplier,
    when upstream team
    provides an API and
    downstream teams
    consume it

    View Slide

  77. Customer team can
    influence Supplier to
    modify model

    View Slide

  78. Open Host Service,
    when interface is
    defined by widely known
    protocol

    View Slide

  79. It's typical SOA approach

    View Slide

  80. Or:
    * Partner
    * Conformist
    * Separate ways
    * Anticorruption layer
    * Published language

    View Slide

  81. Taming the beast
    A.K.A. Summary

    View Slide

  82. OOP is not enough

    View Slide

  83. DDD is great
    for complex problems

    View Slide

  84. You can pick parts of
    DDD and still benefit
    out of it

    View Slide

  85. You should be aware of
    duplication and complex
    structures in DDD

    View Slide

  86. DDD doesn't care about
    your fancy:
    Symfony/Zend Framework/Laravel *
    * delete where inapplicable

    View Slide

  87. Be PRAGMATIC! *
    * take care about your core domain in the first place

    View Slide

  88. Thanks for your attention!
    Ask me anything
    blog.sznapka.pl
    [email protected]
    twitter.com/sznapka

    View Slide

  89. Your feedback is
    important!
    https://joind.in/11844

    View Slide