WHITECOLOR
WHITECOLOR

Reputation: 26142

Event sourcing: multiple aggregates for event

Is it possible that event has two aggregates, for example: UserPostCommentAdded refers to User and UserPost aggregates? Or I don't understand the aggregate concept.

Upvotes: 2

Views: 800

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57259

Is it possible that event has two aggregate?

Both yes and no.

Yes, it is common that an event will include references to more than one "aggregate".

UserPostCommentAdded: {
    userId: 001ae072-1834-4b73-90af-666ec0edc04a
    userPostId: 6c01a87c-e64f-466a-9f1a-b6af28049248 
}

What you don't normally see is two different aggregates that use the same event to update their own state. Which is to say, the relationship of the UserPost is the responsibility of the UserPost, or it is the responsibility of the User, but not both at the same time.

Usualy I see single aggregateId field in implementation, no?

You will sometimes see that; but unless you are doing something very specialized, "aggregate" won't be part of your domain's language, so it really doesn't make sense in your event data. You might lift the identifier of one of your domain entities into the meta data...

UserPostCommentAdded: {
    data: {
        userId: 001ae072-1834-4b73-90af-666ec0edc04a
        userPostId: 6c01a87c-e64f-466a-9f1a-b6af28049248
    }
    metadata: {
        aggregateId: 6c01a87c-e64f-466a-9f1a-b6af28049248
    } 
}

For instance, you might do something like this if you were using domain agnostic plumbing to route your events around. In this sense, you would probably only have a single aggregateId in the metadata, identifying the aggregate that wrote the event in the first place.

Upvotes: 3

Related Questions