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(...)
        );
    }

    ...
}

Upvotes: 0

Views: 236

Answers (2)

CPerson
CPerson

Reputation: 1222

I know there is an accepted answer for this question, but wanted to chip in my own 2 cents.

When you state that an Aggregate is issuing a Command, your code sample doesn't actually do that. Your example is that of an Aggregate performing certain behavior. The concept of a "Command" is that of a message that encapsulates a user's intent (Use Case). A Command would be typically (and hopefully) handled by a CommandHandler which would then call methods on the Aggregate to perform the work. The Aggregate doesn't really know about Use Cases.

If you separate the concept of the Command from the concept of the Aggregate then you are free to implement Behavior in a way that makes your domain flexible. You are able to add new Use Cases (Commands) and new Behaviors (in your Aggregate) independently of each other.

Upvotes: 1

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57194

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.

Upvotes: 2

Related Questions