events

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidAggregateType = errors.New("invalid aggregate type")

ErrInvalidAggregateType is when the aggregate does not implement event.Aggregte.

View Source
var ErrInvalidEventBus = errors.New("invalid event bus")

ErrInvalidEventBus is when a dispatcher is created with a nil event bus.

View Source
var ErrInvalidEventStore = errors.New("invalid event store")

ErrInvalidEventStore is when a dispatcher is created with a nil event store.

View Source
var ErrMismatchedEventType = errors.New("mismatched event type and aggregate type")

ErrMismatchedEventType occurs when loaded events from ID does not match aggregate type.

Functions

This section is empty.

Types

type Aggregate

type Aggregate interface {
	// Provides all the basic aggregate data.
	eh.Aggregate

	// Version returns the version of the aggregate.
	Version() int
	// Increment version increments the version of the aggregate. It should be
	// called after an event has been successfully applied.
	IncrementVersion()

	// Events returns all uncommitted events that are not yet saved.
	Events() []eh.Event
	// ClearEvents clears all uncommitted events after saving.
	ClearEvents()

	// ApplyEvent applies an event on the aggregate by setting its values.
	// If there are no errors the version should be incremented by calling
	// IncrementVersion.
	ApplyEvent(context.Context, eh.Event) error
}

Aggregate is an interface representing a versioned data entity created from events. It receives commands and generates events that are stored.

The aggregate is created/loaded and saved by the Repository inside the Dispatcher. A domain specific aggregate can either implement the full interface, or more commonly embed *AggregateBase to take care of the common methods.

type AggregateBase

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

AggregateBase is a event sourced aggregate base to embed in a domain aggregate.

A typical example:

type UserAggregate struct {
    *events.AggregateBase

    name string
}

Using a new function to create aggregates and setting up the aggregate base is recommended:

func NewUserAggregate(id eh.UUID) *InvitationAggregate {
    return &UserAggregate{
        AggregateBase: events.NewAggregateBase(UserAggregateType, id),
    }
}

The aggregate must also be registered, in this case:

func init() {
    eh.RegisterAggregate(func(id eh.UUID) eh.Aggregate {
        return NewUserAggregate(id)
    })
}

The aggregate must return an error if the event can not be applied, or nil to signal success (which will increment the version).

func (a *Aggregate) ApplyEvent(event Event) error {
    switch event.EventType() {
    case AddUserEvent:
        // Apply the event data to the aggregate.
    }
}

See the examples folder for a complete use case.

func NewAggregateBase

func NewAggregateBase(t eh.AggregateType, id eh.UUID) *AggregateBase

NewAggregateBase creates an aggregate.

func (*AggregateBase) AggregateType

func (a *AggregateBase) AggregateType() eh.AggregateType

AggregateType implements the AggregateType method of the Aggregate interface.

func (*AggregateBase) ClearEvents

func (a *AggregateBase) ClearEvents()

ClearEvents implements the ClearEvents method of the Aggregate interface.

func (*AggregateBase) EntityID

func (a *AggregateBase) EntityID() eh.UUID

EntityID implements the EntityID method of the Entity and Aggregate interface.

func (*AggregateBase) Events

func (a *AggregateBase) Events() []eh.Event

Events implements the Events method of the Aggregate interface.

func (*AggregateBase) IncrementVersion

func (a *AggregateBase) IncrementVersion()

IncrementVersion increments the v of the aggregate and should be called after an event has been applied successfully in ApplyEvent.

func (*AggregateBase) StoreEvent

func (a *AggregateBase) StoreEvent(t eh.EventType, data eh.EventData, timestamp time.Time) eh.Event

StoreEvent stores an event for later retrieval by Events().

func (*AggregateBase) Version

func (a *AggregateBase) Version() int

Version implements the Version method of the Aggregate interface.

type AggregateStore

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

AggregateStore is an aggregate store using event sourcing. It uses an event store for loading and saving events used to build the aggregate.

func NewAggregateStore

func NewAggregateStore(store eh.EventStore, bus eh.EventBus) (*AggregateStore, error)

NewAggregateStore creates a repository that will use an event store and bus.

func (*AggregateStore) Load

func (r *AggregateStore) Load(ctx context.Context, aggregateType eh.AggregateType, id eh.UUID) (eh.Aggregate, error)

Load implements the Load method of the eventhorizon.AggregateStore interface. It loads an aggregate from the event store by creating a new aggregate of the type with the ID and then applies all events to it, thus making it the most current version of the aggregate.

func (*AggregateStore) Save

func (r *AggregateStore) Save(ctx context.Context, agg eh.Aggregate) error

Save implements the Save method of the eventhorizon.AggregateStore interface. It saves all uncommitted events from an aggregate to the event store.

type ApplyEventError

type ApplyEventError struct {
	// Event is the event that caused the error.
	Event eh.Event
	// Err is the error that happened when applying the event.
	Err error
}

ApplyEventError is when an event could not be applied. It contains the error and the event that caused it.

func (ApplyEventError) Error

func (a ApplyEventError) Error() string

Error implements the Error method of the error interface.

Jump to

Keyboard shortcuts

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