How to define domain events?

How to define domain events?

A Domain Event is an event that is spawned from this model that is a result of a decision within the domain. Within the model our Aggregates have the role of maintaining business rules and this is where we implement decision points in our model, so the Aggregates are also responsible for creating the Domain Events.

When to use domain events?

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.

What is a ddd event?

In domain-driven design, domain events are described as something that happens in the domain and is important to domain experts. Such events typically occur regardless of whether or to what extent the domain is implemented in a software system. They are also independent of technologies.

How to handle domain events?

The complete process looks like this:

  1. Create database transaction.
  2. Get aggregate(s).
  3. Invoke aggregate method.
  4. Add domain events to Events collections.
  5. Publish domain events and handle them.
  6. Save changes to DB and commit transaction.
  7. Publish domain events notifications and handle them.

What is domain Service in DDD?

Domain Services (or just Services in DDD) is used to perform domain operations and business rules. In his DDD book, Eric Evans describes a good Service in three characteristics: The operation relates to a domain concept that is not a natural part of an Entity or Value Object.

How are domain events used to enforce consistency?

Domain events to enforce consistency between multiple aggregates within the same domain Figure 7-14 shows how consistency between aggregates is achieved by domain events. When the user initiates an order, the Order Aggregate sends an OrderStarted domain event.

How are domain events created in a model?

A Domain Event is an event that is spawned from this model that is a result of a decision within the domain. Within the model our Aggregates have the role of maintaining business rules and this is where we implement decision points in our model, so the Aggregates are also responsible for creating the Domain Events.

When to use asynchronous events in Domain Driven Design?

You might need to send asynchronous events to communicate and propagate changes from one original domain model (the original microservice or original bounded-context, for instance) to multiple subscribed microservices or even external subscribed applications.

When do domain events and side effects occur?

In short, in-memory Domain events and their side effects (event handler’s operations) need to occur within the same logical transaction. This model was clearly exposed by Udi Dahan, in several blog posts: