TestName
TestName

Reputation: 388

Correct way for communicating aggregates in DDD

I have 2 application services which related to 2 different aggregates.

I have some operations where I need to call another service for getting the data and run some actions. I do not want to do that because of transaction boundaries of different aggregates.

What can be the solutions instead of direct communication between services? Technically, it's monolithic spring application.

Upvotes: 3

Views: 2229

Answers (1)

Ankit Vijay
Ankit Vijay

Reputation: 4078

Domain Events could be your answer. From the Microsoft documentation

Use domain events to explicitly implement side effects of changes within your domain. In other words, and using DDD terminology, use domain events to explicitly implement side effects across multiple aggregates. Optionally, for better scalability and less impact in database locks, use eventual consistency between aggregates within the same domain.

For example: Consider two aggregates Customer and Order. When a Customer adds an item to the Cart you need to update the Order.

Let us assume the name of operation/ method on the Customer Domain is AddItemToCart. When the operation is complete, then you raise a domain event like ItemAdded from the Customer aggregate. This event is then handled by a Domain Event Handler. Within the EventHandler you would then perform the operation to update the Order aggregate.

Note that, Domain Events are different from Integration Events. Handling Domain Events is an application concern. Domain Events could be implemented in memory and within a single transaction. Integration Events, on the other hand, propagate the committed transactions to an external application or a different microservice (like Audit service).

Upvotes: 5

Related Questions