Documentation
¶
Index ¶
- Variables
- type Aggregate
- type AggregateBase
- func (a *AggregateBase) AggregateType() eh.AggregateType
- func (a *AggregateBase) AppendEvent(t eh.EventType, data eh.EventData, timestamp time.Time) eh.Event
- func (a *AggregateBase) EntityID() uuid.UUID
- func (a *AggregateBase) Events() []eh.Event
- func (a *AggregateBase) IncrementVersion()
- func (a *AggregateBase) Version() int
- type AggregateStore
- type ApplyEventError
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidAggregateType = errors.New("invalid aggregate type")
ErrInvalidAggregateType is when the aggregate does not implement event.Aggregte.
var ErrInvalidEventBus = errors.New("invalid event bus")
ErrInvalidEventBus is when a dispatcher is created with a nil event bus.
var ErrInvalidEventStore = errors.New("invalid event store")
ErrInvalidEventStore is when a dispatcher is created with a nil event store.
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 // Provides events to persist and publish from the aggregate. eh.EventSource // 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() // 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 uuid.UUID) *InvitationAggregate { return &UserAggregate{ AggregateBase: events.NewAggregateBase(UserAggregateType, id), } }
The aggregate must also be registered, in this case:
func init() { eh.RegisterAggregate(func(id uuid.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 uuid.UUID) *AggregateBase
NewAggregateBase creates an aggregate.
func (*AggregateBase) AggregateType ¶
func (a *AggregateBase) AggregateType() eh.AggregateType
AggregateType implements the AggregateType method of the eh.Aggregate interface.
func (*AggregateBase) AppendEvent ¶ added in v0.7.0
func (a *AggregateBase) AppendEvent(t eh.EventType, data eh.EventData, timestamp time.Time) eh.Event
AppendEvent appends an event for later retrieval by Events().
func (*AggregateBase) EntityID ¶
func (a *AggregateBase) EntityID() uuid.UUID
EntityID implements the EntityID method of the eh.Entity and eh.Aggregate interface.
func (*AggregateBase) Events ¶
func (a *AggregateBase) Events() []eh.Event
Events implements the Events method of the eh.EventSource interface.
func (*AggregateBase) IncrementVersion ¶
func (a *AggregateBase) IncrementVersion()
IncrementVersion implements the IncrementVersion method of the Aggregate interface.
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 and an event handler to handle resulting events.
func NewAggregateStore ¶
func NewAggregateStore(store eh.EventStore, eventHandler eh.EventHandler) (*AggregateStore, error)
NewAggregateStore creates a aggregate store with an event store and an event handler that will handle resulting events (for example by publishing them on an event bus).
func (*AggregateStore) Load ¶
func (r *AggregateStore) Load(ctx context.Context, aggregateType eh.AggregateType, id uuid.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.
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) Cause ¶ added in v0.8.0
func (a ApplyEventError) Cause() error
Cause returns the cause of this error.
func (ApplyEventError) Error ¶
func (a ApplyEventError) Error() string
Error implements the Error method of the error interface.