SOLID & Design Patterns Interview Question #74
What is a Domain Event?
Advanced Patterns, CQRS & Domain-Driven Design Fundamentals Mid-Level Intermediate
Quick Interview Answer
A Domain Event represents something meaningful that has happened within the business domain, such as OrderConfirmed or PaymentReceived. It allows other parts of the application to react without placing every secondary action directly inside the originating entity.
Detailed Explanation
A Domain Event represents something meaningful that has happened within the business domain, such as OrderConfirmed or PaymentReceived. It allows other parts of the application to react without placing every secondary action directly inside the originating entity.
Domain events can improve decoupling, but teams must decide carefully whether handling is synchronous or asynchronous and what consistency guarantees are required.
Code Example
public record OrderConfirmed(
Guid OrderId,
DateTime ConfirmedAtUtc);
public void Confirm()
{
Status = OrderStatus.Confirmed;
AddDomainEvent(
new OrderConfirmed(Id, DateTime.UtcNow));
}