DomainEvent

Represents a domain event published by an aggregate in response to a command.

Domain events capture significant business occurrences that have happened within an aggregate. They are immutable facts about past actions and serve as the primary mechanism for communicating state changes to other parts of the system.

Domain events follow declarative design principles (idempotent, similar to Kubernetes apply or Docker image layers). During event sourcing, aggregates should simply apply domain events as overlay layers without complex logic or conditional checks, eliminating the need for explicit sourcing functions.

Key characteristics:

  • Immutable: Events represent facts that cannot be changed

  • Named: Each event has a specific business meaning

  • Versioned: Events carry version information for compatibility

  • Sequenced: Events maintain order within an aggregate's lifecycle

Author

ahoo wang

Parameters

T

The type of the event body payload

See also

for base messaging capabilities

for aggregate identification

for versioning information

Properties

Link copied to clipboard
abstract override val aggregateId: AggregateId

The unique identifier of the aggregate that published this domain event.

Link copied to clipboard
abstract val aggregateName: String

The name of the aggregate.

Link copied to clipboard
abstract val body: T

The typed payload of the message.

Link copied to clipboard
abstract val commandId: String

A unique identifier for this command instance.

Link copied to clipboard
abstract val contextName: String

The name of the bounded context this entity belongs to.

Link copied to clipboard
abstract override val createTime: Long

The timestamp when this message was created, as a Unix timestamp in milliseconds.

Link copied to clipboard
abstract val header: Header

The header containing metadata about this message.

Link copied to clipboard
abstract val id: String

Represents a unique identifier for the implementing entity.

Link copied to clipboard

A computed property that indicates whether the entity has been initialized. Returns true if the version is greater than UNINITIALIZED_VERSION. This property is ignored during JSON serialization.

Link copied to clipboard

A computed property that indicates whether the entity is at its initial version. Returns true if the version equals INITIAL_VERSION. This property is ignored during JSON serialization.

Link copied to clipboard
open val isLast: Boolean

Indicates whether this is the last event in the current event stream.

Link copied to clipboard

Indicates whether this message is in read-only mode.

Link copied to clipboard
abstract val name: String

The name of the entity.

Link copied to clipboard
abstract val ownerId: String

The unique identifier of the resource owner.

Link copied to clipboard
open override val revision: String

The revision/version of the aggregate when this event was published.

Link copied to clipboard
open val sequence: Int

The sequence number of this event within the aggregate's event stream.

Link copied to clipboard
abstract val version: Int

The current version number of the entity as an integer. Versions are monotonically increasing and start from 1 for initialized entities. A version of 0 indicates an uninitialized state.

Functions

Link copied to clipboard

Checks if two aggregates belong to the same context and have the same aggregate name.

Link copied to clipboard

Checks if this entity belongs to the same bounded context as another entity.

Link copied to clipboard
open fun withHeader(additionalSource: Map<String, String>): DomainEvent<T>

Adds all key-value pairs from the provided map to the message header and returns the message for method chaining.

open fun withHeader(key: String, value: String): DomainEvent<T>

Adds a key-value pair to the message header and returns the message for method chaining.

Link copied to clipboard

Marks this message as read-only and returns it for method chaining.