Domain Events should be published when the fact they represent occurs. For instance, when a new user has been registered, a new UserRegistered
Event should be published.
Following the newspaper metaphor:
- Modeling a Domain Event is like writing a news article
- Publishing a Domain Event is like printing the article in the paper
- Spreading a Domain Event is like delivering the newspaper so everyone can read the article
The recommended approach for publishing Domain Events is to use a simple Listener-Observer pattern to implement a DomainEventPublisher
.
Continuing with the example of a new user who has been registered in our application, let's see how the corresponding Domain Event can be published:
class User { protected $userId; protected $email ; protected $password; public function __construct(UserId $userId, $email, $password) { $this->setUserId($userId); $this->setEmail($email...