ddd

package
v1.0.0-beta.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWrongAggregate is returned when an event is applied to the wrong aggregate.
	ErrWrongAggregate = errors.New("event does not belong to this aggregate")

	// ErrDuplicateEvent is returned when attempting to apply an event that has already been applied.
	ErrDuplicateEvent = errors.New("event has already been applied")

	// ErrInvalidEventSequenceNo is returned when an event sequence number is invalid.
	ErrInvalidEventSequenceNo = errors.New("event sequence number is invalid")
)

Functions

This section is empty.

Types

type BaseEntity

type BaseEntity struct {
	// contains filtered or unexported fields
}

BaseEntity provides event sourcing capabilities for domain entities. Entities should embed this struct to gain event tracking, sequence number management, and uncommitted event collection.

func NewBaseEntity

func NewBaseEntity(aggregateID string) *BaseEntity

NewBaseEntity creates a new BaseEntity with the given aggregate ID.

func RestoreBaseEntity

func RestoreBaseEntity(aggregateID string, sequenceNo int) *BaseEntity

RestoreBaseEntity creates a BaseEntity with a known sequence number, for use when loading an entity from a projection or read model. The sequenceNo should match the current version in the event store.

func (*BaseEntity) ApplyEvent

func (e *BaseEntity) ApplyEvent(ctx context.Context, event domain.EventEnvelope[any]) error

ApplyEvent applies an event to the entity, updating its state and sequence number. This method validates the event, checks for idempotency, and updates the sequence number. This is used for replaying events from the event store. The actual state mutation logic should be implemented by entities embedding BaseEntity.

func (*BaseEntity) ClearUncommittedEvents

func (e *BaseEntity) ClearUncommittedEvents()

ClearUncommittedEvents removes all uncommitted events, typically called after persistence.

func (*BaseEntity) GetID

func (e *BaseEntity) GetID() string

GetID returns the aggregate ID.

func (*BaseEntity) GetSequenceNo

func (e *BaseEntity) GetSequenceNo() int

GetSequenceNo returns the last event sequence number from when the entity was hydrated.

func (*BaseEntity) GetUncommittedEvents

func (e *BaseEntity) GetUncommittedEvents() []domain.EventEnvelope[any]

GetUncommittedEvents returns a copy of all uncommitted events.

func (*BaseEntity) RecordEvent

func (e *BaseEntity) RecordEvent(payload any, eventType string) error

RecordEvent records a new event by creating an EventEnvelope internally. The payload can be any type and will be stored in the event envelope. This method is thread-safe and validates that the event belongs to this aggregate.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL