commanded / eventstore

Event store using PostgreSQL for persistence
MIT License
1.06k stars 146 forks source link

[question] Fetching multiple aggregates when handling a command #158

Closed mwillema closed 5 years ago

mwillema commented 5 years ago

How can I fetch multiple aggregates when handling a command? For instance let's say we have the following:

The OpenTopic command is handled by the Topic aggregate and opens / creates a new topic within a given forum. However in order to check invariants or perform some business logic, the Topic aggregate needs the specified Forum. So, we need to resolve the relationship between a topic and its forum.

In a pure DDD way my command handler would fetch the corresponding Forum from ForumRepository and pass it as a parameter to Topic. This way Topic can perform any business logic with the help of Forum before being saved, ie. change events being persisted.

slashdotdash commented 5 years ago

Commanded enforces that one command can only target one aggregate.

Where you have an invariant that affects more than one aggregate you need to consider:

Have a look at the Domain modelling resources on the Commanded Wiki for more guidance.

mwillema commented 5 years ago

IMO this is quite a limitation and may add accidental complexity. Application services / command handlers often need to orchestrate things and fetch multiple aggregates in order to execute a command. We agree that only one aggregate should be modified within a given transaction (other aggregates will be eventually consistent, process managers are used for that) but resolving relations is very important.

Event sourcing encourages us to model small aggregates, which increases the need to fetch the related ones to handle a given command.

I used this a lot in every past project I've been involved with :-)

slashdotdash commented 5 years ago

Do you have an example which couldn't be acheived by querying a read model for related data?

Commanded is unlikely to ever support this feature.

slashdotdash commented 5 years ago

Closing issue, but feel free to add any further comments @mwillema.