Search code examples
domain-driven-designddd-service

DDD: using aggregates inside another aggregates


DDD: Can aggregates get other aggregates as parameters? According to this, its OK to use aggregates inside another aggregates. But its requires to change multiple aggregates at one transaction. So is it truth that this rule can be easily skipped and I can change multiple aggregates at one time (especially in case of Microservice). The only problem that I need to lock whole aggregates? Thx

I have a simple situation: User, Friendship and Friendship request entities. User can be aggregate root. DDD and Homogeneous Many-to-Many Relationship

But I would not like to use eventual consistency (especially inside on micro service) cause anyways when I handle that event (FriendshipRequestSent) I need to lock another dependant aggregate. And need to handle and write event on error.


Solution

  • So is it truth that this rule can be easily skipped and I can change multiple aggregates at one time (especially in case of Microservice).

    Yes, maybe.

    The only problem that I need to lock whole aggregates?

    No - there is the additional problem that, because you are modifying multiple aggregates (or more precisely, domain entities that belong to multiple aggregates) in the same transaction, you also need to be careful to design your persistent storage so that updates to all of the entities can be committed in the same "transaction".

    That is simple enough when, for example, the entities are all stored in a single relational database, and you can use general purpose operations in the relational database to control your writes.

    But if you are working with a different kind of data storage, where you cannot easily control the writes to all entities at the same time, then it gets a bit spooky.

    In an "ideal" world, we could pretend that all information is local, and storing it is just an implementation detail. In practice, the actual implementations we get to use only approximate this idea, and we have to be mindful of the differences.