Answer the question
In order to leave comments, you need to log in
How to interact with different contexts through events?
Hello everyone, I'm studying ddd and I have a question about events. Let's say there are 2 contexts: user, panel. We create a user and an event is created, UserWasCreatedEvent
we throw this event somewhere in the queue. Next, in another panel context, we listen to an event UserWasCreatedEvent
from the user context and do something. And it turns out that the panel context has a direct dependence on the user context. Is this how it should be? How it is possible to get rid of this dependence to other context?
Sample code:
namespace User\Domain\Event;
class UserWasCreatedEvent
{
// event data
}
---------------------------------------------
namespace Panel\Domain\Event;
use User\Domain\Event;
class UserWasCreatedEventHandler
{
public function __invoke(UserWasCreatedEvent $createdEvent)
{
// do something
}
}
---------------------------------------------
namespace Shared\Infrastructure\EventSubscriber\Doctrine;
class DomainEventSubscriber
{
...
public function publish(): void
{
foreach ($this->entities as $entity) {
foreach ($entity->releaseEvents() as $event) {
$this->eventBus->dispatch($event);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Next, in another panel context, we listen to the UserWasCreatedEvent event from the user context and do something.
namespace Shared\Infrastructure\EventSubscriber\Doctrine;
use User\Domain\Event\UserWasCreatedEvent;
use Panel\Domain\SomeHandler;
use Panel\Domain\SomeCommand;
class UserWasCreatedEventHandler
{
private SomeHandler $handler;
public function __construct(SomeHandler $handler)
{
$this->handler = $handler;
}
public function __invoke(UserWasCreatedEvent $createdEvent)
{
$this->handler->handle(new SomeCommand(
$createdEvent->getId(),
$createdEvent->getSomeInfo()
));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question