Book Image

Domain-Driven Design in PHP

By : Keyvan Akbary, Carlos Buenosvinos, Christian Soronellas
Book Image

Domain-Driven Design in PHP

By: Keyvan Akbary, Carlos Buenosvinos, Christian Soronellas

Overview of this book

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.
Table of Contents (24 chapters)
Title Page
Credits
Foreword
About the Authors
Acknowledgments
www.PacktPub.com
Customer Feedback
Dedication
Preface
14
Bibliography
15
The End

Collection-Oriented Repositories


Repositories mimic a collection by implementing their common interface characteristics. As a collection, a Repository shouldn't leak any intentions of persistence behavior, such as the notion of saving to a store.

The underlying persistence mechanism has to support this need. You shouldn't be required to handle changes to the objects over their lifetime. The collection references the most recent changes to the object, meaning that upon each access, you get the latest object state.

Repositories implement a concrete collection type, the Set. A Set is a data structure with an invariant that doesn't contain duplicate entries. If you try to add an element that's already present to a Set, it won't be added. This is useful in our use case, as each Aggregate has a unique identity that's associated with the Root Entity.

Consider, for example, that we have the following Domain Model:

namespace Domain\Model;

class Post
{
    const EXPIRE_EDIT_TIME = 120; // seconds

 ...