aggregate

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package aggregate is the domain-facing side of event sourcing. Domain code imports only this package; it depends on nothing but the stdlib and uuid.

An Aggregate embeds Base by value and implements StreamType and Apply on its pointer type. Commands validate and call Raise, which runs the same Apply that rebuilds state from history:

type TaskID uuid.UUID

type Task struct {
	aggregate.Base[TaskID]
	done bool
}

func (*Task) StreamType() string { return "task" }

func (t *Task) Apply(e aggregate.Event) {
	switch e.(type) {
	case TaskCompleted:
		t.done = true
	}
}

func (t *Task) Complete() error {
	if t.done {
		return ErrAlreadyDone
	}
	aggregate.Raise(t, TaskCompleted{})
	return nil
}

Root is sealed: a type that does not embed Base cannot satisfy it, even if it declares StreamType, Version and Apply.

Keeping domain packages pure

Services can let golangci-lint (v2) enforce that their domain packages import only this package, the stdlib and uuid:

linters:
  enable:
    - depguard
  settings:
    depguard:
      rules:
        domain:
          list-mode: strict
          files:
            - "**/internal/domain/**"
          allow:
            - $gostd
            - github.com/google/uuid
            - github.com/deepworx/go-utils/pkg/eventsourcing/aggregate

Adjust the files glob to where the service keeps its domain code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Raise

func Raise[I ID](r Root[I], e Event)

Raise applies e to r, increments its version and records e as uncommitted, so the event store appends it on save.

Types

type Base

type Base[I ID] struct {
	// contains filtered or unexported fields
}

Base holds the event-sourcing bookkeeping of an Aggregate. Embed it by value. There is no constructor: the zero value of an Aggregate is its valid starting state, and the event store sets the ID when it allocates one.

func (*Base[I]) ID

func (b *Base[I]) ID() I

ID returns the Aggregate's typed identifier.

func (*Base[I]) Version

func (b *Base[I]) Version() int

Version returns the number of events applied, historical and raised.

type Event

type Event interface {
	EventType() string
}

Event is a domain event. EventType returns its permanent Event type name.

type ID

type ID interface {
	~[16]byte
}

ID constrains typed Aggregate identifiers such as `type TaskID uuid.UUID`, so a TaskID cannot be passed where an OrderID is expected.

type Root

type Root[I ID] interface {
	// StreamType names the kind of Aggregate. It must never change once
	// events have been written.
	StreamType() string
	// Version returns the number of events applied, historical and raised.
	Version() int
	// Apply changes state for one event. It is the single place where state
	// changes, for both historical and newly raised events, and must not
	// fail or have side effects.
	Apply(e Event)
	// contains filtered or unexported methods
}

Root is an Aggregate as the event store sees it. It is sealed: only pointers to types that embed Base satisfy it.

Directories

Path Synopsis
Package aggregatetest builds Aggregates from a history of events for domain unit tests, without a Repository, Codec or database.
Package aggregatetest builds Aggregates from a history of events for domain unit tests, without a Repository, Codec or database.

Jump to

Keyboard shortcuts

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