Leanpub Header

Skip to main content

Domain-Driven Design in PHP

Discover DDD, Architectural Styles, Tactical Design Implementations, and Bounded Context Integration with PHP 7.4 examples

Master Domain-Driven Design Tactical patterns: Entities, Value Objects, Services, Domain Events, Aggregates, Factories, Repositories and Application Services; with real examples in PHP. Explore the advantages of Hexagonal Architecture and understand Strategic design with Bounded Contexts and their integration through REST and message queues.

Minimum price

$24.99

$34.99

You pay

$34.99

Authors earn

$27.99
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
WEB
3,172
Readers
385
Pages
76,993Words
About

About

About the Book

In 2014, after two years of reading about and working with Domain-Driven Design, Carlos and Christian, friends and workmates, traveled to Berlin to participate in Vaughn Vernon's Implementing Domain-Driven Design Workshop. The training was fantastic, and all the concepts that were swirling around in their minds prior to the trip suddenly became very real. However, they were almost no PHP developers in a room full of Java and .NET developers.

Around the same time, Keyvan co-founded Funddy, a crowdfunding platform for the masses built on top of the concepts and building blocks of Domain-Driven Design. Domain-Driven Design proved itself effective in the exploratory process and modeling of building an early-stage startup like Funddy. It also helped handle the complexity of the company, with its constantly changing environment and requirements. And after connecting with Carlos and Christian and discussing the book, Keyvan proudly signed on as the third writer.

Domain-Driven Design has arrived to the PHP community with lots of talk but no real code detailing how to implement Tactical DDD patterns or how to integrate Bounded Contexts with REST and/or messaging strategies. Without being in a Training session and no PHP real examples, learning DDD can be challenging.

Together, we've written the book we wanted to have when we started with Domain-Driven Design. It's full of examples, production-ready code, shortcuts, and our recommendations based on our experiences of what worked and what didn't for our respective teams. We arrived at Domain-Driven Design via its building blocks — Tactical Patterns — which is why this book is mainly about them. Reading it will help you learn them, write them, and implement them. You'll also discover how to integrate Bounded Contexts using synchronous and asynchronous approaches, which will open your world to strategic design — though the latter is a road you'll have to discover on your own.

Author

About the Authors

Carlos Buenosvinos

I am an Extreme Programmer (XP) and DevOps with more than 20 years of experience in developing Web and Mobile Applications. For the last ten years, I have played various leading roles such as Tech Lead, VP of Engineering and CTO. I have mentored engineering and product teams up to 150 members in multiple different markets such as E-commerce, E-Learning, Payment Processing, Classifieds, and Recruiting Market.

As an employee and consultant, I have contributed to the success of start-ups and well-established brands. Some examples are SEAT, XING, Atrápalo, PCComponentes, Emagister, eBay, Lowpost, Vendo, Riplife, Universitat International de Catalunya (UIC), and many more.

I am the happy creator of Ansistrano, the most starred Ansible Galaxy role. I am also the author of the book Domain-Driven Design in PHP. I am also a conference speaker, and since 2016, I have a video blog about Development Best Practices called Rigor Talks. I organized the DevOps Barcelona Conference and the PHP Barcelona Conference.

My main areas of expertise are the Agile Team Management (Scrum and Kanban), Best Development Practices (Extreme Programming, Domain-Driven Design, and Microservice Architectures) and Digital Transformation (Agile, XP, and DevOps).

Christian Soronellas

Christian is an Extreme Programmer and has over 15 years of experience helping tech companies succeed from a broad variety of roles, from Software Engineer to CTO. He has helped companies such as a Privalia, Emagister, Atrápalo, Enalquiler, PlanetaHuerto, PcComponentes or Opositatest. He is the author of the book Domain-Driven Design in PHP as well as a conference co-organizer of DevOps Barcelona Conference and PHP Barcelona Conference

Keyvan Akbary

Keyvan is an Engineering Leader and programmer with more than 15 years of experience crafting products customers love and helping teams succeed. He understands technology as a medium for providing value, not the end itself. He has a passion for Distributed Systems, Software fundamentals, SOLID principles, Clean Code, Design Patterns, Domain-Driven Design, and Testing; as well as being a sporadic Functional Programmer. For the last 7 years, he has also focused on growing teams in high scale-up product companies, advocating for customer-centric product development, Extreme Programming, DevOps, Lean and Kanban.

He has worked on countless projects as a freelancer, on video streaming at Youzee, tradesman marketplace at MyBuilder, in addition to founding his own crowdfunding startup Funddy, and leading FinTech teams at Wise. Currently, he is leading engineering in the ride-hailing space as Head of Engineering at Cabify.

He is also the author of Domain-Driven Design in PHP and CQRS by Example.

Contents

Table of Contents

Foreword by Matthias Noback

Preface

  1. Who Should Read This Book
  2. DDD and PHP Community
  3. Summary of Chapters
  4. Code and Examples
  5. Acknowledgements

About the Authors

  1. Carlos Buenosvinos
  2. Christian Soronellas
  3. Keyvan Akbary

Getting Started with Domain-Driven Design

  1. Why Domain-Driven Design Matters
  2. The Three Pillars of Domain-Driven Design
  3. Considering Domain-Driven Design
  4. The Tricky Parts
  5. Strategical Overview
  6. Related Movements: Microservices and Self-Contained Systems
  7. Exercise
  8. Wrap-Up

Architectural Styles

  1. Spaghetti Architecture
  2. Layered Architecture
  3. DTOs Instead of Model Instances?
  4. Hexagonal Architecture: Inverting Dependencies
  5. The Dependency Inversion Principle
  6. Command Query Responsibility Segregation (CQRS)
  7. Command Query Separation (CQS)
  8. Event Sourcing
  9. Wrap-Up

Value Objects

  1. Definition
  2. Exercise
  3. Value Object vs. Entity
  4. Exercise
  5. Currency and Money Example
  6. Extra Validations for Currency
  7. Characteristics
  8. static vs. self
  9. Exercise
  10. Basic Types
  11. Testing Value Objects
  12. Persisting Value Objects
  13. Why Doctrine?
  14. Constructors
  15. Surrogate Attributes
  16. Why Use XML Mapping?
  17. Time to Discuss
  18. Exercise
  19. Exercise
  20. Exercise
  21. Security
  22. Wrap-Up

Entities

  1. Introduction
  2. Objects vs. Primitive Types
  3. Identity Operation
  4. Exercise
  5. Persisting Entities
  6. What’s an Annotation?
  7. Testing Entities
  8. Validation
  9. Entities and Domain Events
  10. Wrap-Up

Services

  1. Application Services
  2. Domain Services
  3. Domain Services and Infrastructure Services
  4. Testing Domain Services
  5. Anemic Domain Models vs. Rich Domain Models
  6. Wrap-Up

Domain Events

  1. Introduction
  2. Definition
  3. Exercise
  4. Characteristics
  5. Symfony Event Dispatcher
  6. Modeling Events
  7. Why Not the Whole User Entity?
  8. Doctrine Events
  9. Persisting Domain Events
  10. Publishing Events from the Domain Model
  11. Other Strategy for Publishing Domain Events
  12. Exercise
  13. Spreading the News to Remote Bounded Contexts
  14. Why an Exchange Name?
  15. Exercise
  16. Wrap-Up

Modules

  1. General Overview
  2. Leverage Modules in PHP
  3. What about PHAR files?
  4. Bounded Contexts and Applications
  5. Can Two Bounded Contexts Be in the Same Application? What about the Other Way Around?
  6. Structuring Code in Modules
  7. Should We Place Repositories, Factories, Domain Events, and Services in Their Own Subfolders?
  8. Wrap-Up

Aggregates

  1. Introduction
  2. Key Concepts
  3. What Is an Aggregate?
  4. Why Aggregates?
  5. A Bit of History
  6. Anatomy of an Aggregate
  7. Aggregate Design Rules
  8. Sample Application Service: User and Wishes
  9. Render the Number of Wishes
  10. Transactions
  11. Wrap Up

Factories

  1. Factory Method on Aggregate Root
  2. Factory on Service
  3. Testing Factories
  4. Wrap-Up

Repositories

  1. Definition
  2. Repositories Are Not DAOs
  3. Collection-Oriented Repositories
  4. Exercise
  5. Persistence-Oriented Repository
  6. Extra Behavior
  7. Querying Repositories
  8. Managing Transactions
  9. Testing Repositories
  10. Testing Your Services with In-Memory Implementations
  11. Wrap-Up

Application

  1. Requests
  2. Anatomy of an Application Service
  3. Handling Exceptions
  4. Is It Bad to Use a Dependency Injection Container?
  5. Testing Application Services
  6. Transactions
  7. Security
  8. Domain Events
  9. Command Handlers
  10. Wrap-Up

Integrating Bounded Contexts

  1. Integration Through the Data Store
  2. Integration Relationships
  3. Implementing Bounded Context Integrations
  4. Wrap-Up

Appendix: 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?

The End

Bibliography

Contributor

About the Contributors

Edd Mann

Addicted to software development, who loves to share the knowledge he learns with others. Enjoys exploring many areas of the field, ranging from algorithms to software architectural design. Co-hosts a weekly development podcast found at ThreeDevsandAMaybe.com. Has worked in an agency setting, freelance and now at MyBuilder.

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub