O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Domain-Driven Design in PHP

Book Description

Real examples written in PHP showcasing DDD Architectural Styles, Tactical Design, and Bounded Context Integration

About This Book

  • Focuses on practical code rather than theory
  • Full of real-world examples that you can apply to your own projects
  • Shows how to build PHP apps using DDD principles

Who This Book Is For

This book is for PHP developers who want to apply a DDD mindset to their code. You should have a good understanding of PHP and some knowledge of DDD. This book doesn’t dwell on the theory, but instead gives you the code that you need.

What You Will Learn

  • Correctly design all design elements of Domain-Driven Design with PHP
  • Learn all tactical patterns to achieve a fully worked-out Domain-Driven Design
  • Apply hexagonal architecture within your application
  • Integrate bounded contexts in your applications
  • Use REST and Messaging approaches

In Detail

Domain-Driven Design (DDD) has arrived in the PHP community, but for all the talk, there is very little real code. Without being in a training session and with no PHP real examples, learning DDD can be challenging. This book changes all that. It details how to implement tactical DDD patterns and gives full examples of topics such as integrating Bounded Contexts with REST, and DDD messaging strategies. In this book, the authors show you, with tons of details and examples, how to properly design Entities, Value Objects, Services, Domain Events, Aggregates, Factories, Repositories, Services, and Application Services with PHP. They show how to apply Hexagonal Architecture within your application whether you use an open source framework or your own.

Style and approach

This highly practical book shows developers how to apply domain-driven design principles to PHP. It is full of solid code examples to work through.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Foreword
  2. Acknowledgments
  3. Dedication
  4. Preface
    1. Who Should Read This Book
    2. DDD and PHP Community
    3. Summary of Chapters
      1. Chapter 1: Getting Started with Domain-Driven Design
      2. Chapter 2: Architectural Styles
      3. Chapter 3: Value Objects
      4. Chapter 4: Entities
      5. Chapter 5: Domain Services
      6. Chapter 6: Domain-Events
      7. Chapter 7: Modules
      8. Chapter 8: Aggregates
      9. Chapter 9: Factories
      10. Chapter 10: Repositories
      11. Chapter 11: Application
      12. Chapter 12: Integrating Bounded Contexts
      13. Appendix: Hexagonal Architecture with PHP
    4. Code and Examples
  5. Getting Started with Domain-Driven Design
    1. Why Domain-Driven Design Matters
    2. The Three Pillars of Domain-Driven Design
      1. Ubiquitous Language
        1. Event Storming
    3. Considering Domain-Driven Design
    4. The Tricky Parts
    5. Strategical Overview
    6. Related Movements: Microservices and Self-Contained Systems
    7. Wrap-Up
  6. Architectural Styles
    1. The Good Old Days
    2. Layered Architecture
      1. Model-View-Controller
      2. Example of Layered Architecture
        1. The View
        2. The Controller
      3. Inverting Dependencies: Hexagonal Architecture
        1. The Dependency Inversion Principle (DIP)
        2. Applying Hexagonal Architecture
      4. Command Query Responsibility Segregation (CQRS)
        1. The Write Model
        2. The Read Model
        3. Synchronizing the Write Model with the Read Model
    3. Event Sourcing
    4. Wrap-Up
  7. Value Objects
    1. Definition
    2. Value Object vs. Entity
    3. Currency and Money Example
    4. Characteristics
      1. Measures, Quantifies, or Describes
      2. Immutability
      3. Conceptual Whole
      4. Value Equality
      5. Replaceability
      6. Side-Effect-Free Behavior
    5. Basic Types
    6. Testing Value Objects
    7. Persisting Value Objects
      1. Persisting Single Value Objects
        1. Embedded Value with an Ad Hoc ORM
        2. Embedded Value (Embeddables) with Doctrine >= 2.5.*
        3. Embedded Value with Doctrine <= 2.4.*
      2. Serialized LOB and Ad Hoc ORM
        1. Improved Serialization with JMS Serializer
      3. Serialized LOB with Doctrine
        1. Doctrine Object Mapping Type
        2. Doctrine Custom Types
        3. Persisting a Collection of Value Objects
        4. Collection Serialized into a Single Column
        5. Collection Backed by a Join Table
          1. Collection Backed by a Join Table with Doctrine
          2. Collection Backed by a Join Table with an Ad Hoc ORM
        6. Collection Backed by a Database Entity
      4. NoSQL
        1. PostgreSQL JSONB and MySQL JSON Type
    8. Security
    9. Wrap-Up
  8. Entities
    1. Introduction
    2. Objects Vs. Primitive Types
    3. Identity Operation
      1. Persistence Mechanism Generates Identity
        1. Surrogate Identity
        2. Active Record Vs. Data Mapper for Rich Domain Models
      2. Client Provides Identity
      3. Application Generates Identity
      4. Other Bounded Context Generates Identity
    4. Persisting Entities
      1. Setting Up Doctrine
      2. Mapping Entities
        1. Mapping Entities Using Annotated Code
        2. Mapping Entities Using XML
        3. Mapping Entity Identity
        4. Final Mapping File
    5. Testing Entities
      1. DateTimes
        1. Passing All Dates as Parameters
        2. Test Class
        3. External Fake
        4. Reflection
    6. Validation
      1. Attribute Validation
      2. Entire Object Validation
        1. Decoupling Validation Messages
      3. Validating Object Compositions
    7. Entities and Domain Events
    8. Wrap-Up
  9. Services
    1. Application Services
    2. Domain Services
    3. Domain Services and Infrastructure Services
      1. An Issue of Code Reuse
    4. Testing Domain Services
    5. Anemic Domain Models Vs Rich Domain Models
      1. Anemic Domain Model Breaks Encapsulation
      2. Anemic Domain Model Brings a False Sense of Code Reuse
      3. How to Avoid Anemic Domain Models
    6. Wrap-Up
  10. Domain-Events
    1. Introduction
    2. Definition
      1. Short Story
      2. Metaphor
      3. Real-World Example
    3. Characteristics
      1. Naming Conventions
      2. Domain Events and Ubiquitous Language
      3. Immutability
    4. Modeling Events
    5. Doctrine Events
    6. Persisting Domain Events
      1. Event Store
    7. Publishing Events from the Domain Model
      1. Publishing a Domain Event from an Entity
      2. Publishing your Domain Events from Domain or Application Services
      3. How the Domain Event Publisher Works
      4. Setting Up DomainEventListeners
      5. Testing Domain Events
    8. Spreading the news to Remote Bounded Contexts
      1. Messaging
      2. Syncing Domain Services with REST
    9. Wrap-Up
  11. Modules
    1. General Overview
    2. Leverage Modules in PHP
      1. First-Level Namespacing
      2. PEAR-Style Namespacing
        1. PSR-0 and PSR-4 Namespacing
    3. Bounded Contexts and Applications
    4. Structuring Code in Modules
      1. Design Guidelines
      2. Modules in the Infrastructure Layer
      3. Mixing Different Technologies
      4. Modules in the Application Layer
    5. Wrap-Up
  12. Aggregates
    1. Introduction
    2. Key Concepts
      1. ACID
      2. Transactions
      3. Isolation Levels
      4. Referential Integrity
      5. Locking
      6. Concurrency
        1. Pessimistic Concurrency Control (PCC)
          1. With Doctrine
        2. Optimistic Concurrency Control
          1. With Elasticsearch
          2. With Doctrine
    3. What Is an Aggregate?
      1. What Martin Fowler Says...
      2. What Wikipedia Says...
    4. Why Aggregates?
    5. A Bit of History
    6. Anatomy of an Aggregate
    7. Aggregate Design Rules
      1. Design Aggregates Based in Business True Invariants
      2. Small Aggregates Vs. Big Aggregates
      3. Reference Other Entities by Identity
      4. Modify One Aggregate Per Transaction and Request
    8. Sample Application Service: User and Wishes
      1. No Invariant, Two Aggregates
      2. No More Than Three Wishes Per User
        1. Pessimistic Concurrency Control
        2. Optimistic Concurrency Control
    9. Transactions
    10. Wrap Up
  13. Factories
    1. Factory Method on Aggregate Root
      1. Forcing Invariants
    2. Factory on Service
      1. Building Specifications
      2. Building Aggregates
    3. Testing Factories
      1. Object Mother
      2. Test Data Builder
    4. Wrap-Up
  14. Repositories
    1. Definition
    2. Repositories Are Not DAOs
    3. Collection-Oriented Repositories
      1. In-Memory Implementation
      2. Doctrine ORM
        1. Object Mapping
          1. Doctrine Custom Mapping Types
        2. Entity Manager
        3. DQL Implementation
    4. Persistence-Oriented Repository
      1. Redis Implementation
      2. SQL Implementation
      3. Extra Behavior
    5. Querying Repositories
      1. Specification Pattern
        1. In-Memory Implementation
        2. SQL Implementation
    6. Managing Transactions
    7. Testing Repositories
    8. Testing Your Services with In-Memory Implementations
    9. Wrap-Up
  15. Application
    1. Requests
      1. Building Application Service Requests
      2. Request Design
        1. Use Primitives
        2. Serializable
        3. No Business Logic
        4. No Tests
    2. Anatomy of an Application Service
      1. Dependency Inversion
      2. Instantiating Application Services
        1. Customize an Application Service
      3. Execution
        1. One Class Per Application Service
        2. Multiple Application Service Methods per Class
      4. Returning Values
        1. DTO from Aggregate Instances
        2. Data Transformers
      5. Multiple Application Services on Compound Layouts
        1. AJAX Content Integration
        2. ESI Content Integration
        3. Symfony Sub Requests
        4. One Controller, Multiple Application Services
    3. Testing Application Services
    4. Transactions
    5. Security
    6. Domain Events
    7. Command Handlers
      1. Tactician Library and Other Options
    8. Wrap-Up
  16. Integrating Bounded Contexts
    1. Integration Through the Data Store
    2. Integration Relationships
      1. Separate Ways
      2. Conformist
    3. Implementing Bounded Context Integrations
      1. Modern RPC
      2. Message Queues
    4. Wrap-Up
  17. Hexagonal Architecture with PHP
    1. Introduction
    2. First Approach
    3. Repositories and the Persistence Edge
    4. Decoupling Business and Persistence
    5. Migrating our Persistence to Redis
    6. Decouple Business and Web Framework
    7. Rating an Idea Using the API
    8. Console App Rating
    9. Testing Rating an Idea UseCase
    10. Testing Infrastructure
    11. Arggg, So Many Dependencies!
    12. Domain Services and Notification Hexagon Edge
    13. Let's Recap
    14. Hexagonal Architecture
    15. Key Points
    16. What's Next?
  18. Bibliography
  19. The End