Search code examples
domain-driven-designcqrsevent-sourcingaggregateroot

Can an aggregate issue its own commands?


Question

Can an AR issue its own commands, or it is better to issue them through a processor that listen event emitted by the outer command?

BTW: If you consider that this question could lead to "primarily opinionated” answers, I would still want to know whether or not it is considered a good practice, and the why.

PHP code sample

class PatchableComponent extends EventSourcedAggregateRoot
    implements Entity, ReconstitutableEventSourcedAggregateRoot
{
    ...

    public static function importFromCore(...): PatchableComponent
    {
        $patchableComponent = new self;

        $patchableComponent->applyPatchableComponentWasImportedFromCore(
            new PatchableComponentWasImportedFromCore(...)
        );

        // Here, the AR issue its own startLookingForPatches() command.
        $patchableComponent->startLookingForPatches();

        return $patchableComponent;
    }

    public function startLookingForPatches(): void
    {
        $this->applyPatchableComponentStartedLookingForPatches(
            new PatchableComponentStartedLookingForPatches(...)
        );
    }

    ...
}

Solution

  • Can an AR issue its own commands, or it is better to issue them through a processor that listen event emitted by the outer command?

    An aggregate can certainly call its own methods; adding extra layers of indirection is not normally necessary or desirable.