序
本文次要钻研一下 go.cqrs 的 AggregateRoot
AggregateRoot
//AggregateRoot is the interface that all aggregates should implement
type AggregateRoot interface {AggregateID() string
OriginalVersion() int
CurrentVersion() int
IncrementVersion()
Apply(events EventMessage, isNew bool)
TrackChange(EventMessage)
GetChanges() []EventMessage
ClearChanges()}
AggregateRoot 接口定义了 AggregateID、OriginalVersion、CurrentVersion、IncrementVersion、Apply、TrackChange、GetChanges、ClearChanges 办法
AggregateBase
// AggregateBase is a type that can be embedded in an AggregateRoot
// implementation to handle common aggragate behaviour
//
// All required methods to implement an aggregate are here, to implement the
// Aggregate root interface your aggregate will need to implement the Apply
// method that will contain behaviour specific to your aggregate.
type AggregateBase struct {
id string
version int
changes []EventMessage}
// NewAggregateBase contructs a new AggregateBase.
func NewAggregateBase(id string) *AggregateBase {
return &AggregateBase{
id: id,
changes: []EventMessage{},
version: -1,
}
}
// AggregateID returns the AggregateID
func (a *AggregateBase) AggregateID() string {return a.id}
// OriginalVersion returns the version of the aggregate as it was when it was
// instantiated or loaded from the repository.
//
// Importantly an aggregate with one event applied will be at version 0
// this allows the aggregates to match the version in the eventstore where
// the first event will be version 0.
func (a *AggregateBase) OriginalVersion() int {return a.version}
// CurrentVersion returns the version of the aggregate as it was when it was
// instantiated or loaded from the repository.
//
// Importantly an aggregate with one event applied will be at version 0
// this allows the aggregates to match the version in the eventstore where
// the first event will be version 0.
func (a *AggregateBase) CurrentVersion() int {return a.version + len(a.changes)
}
// IncrementVersion increments the aggregate version number by one.
func (a *AggregateBase) IncrementVersion() {a.version++}
// TrackChange stores the EventMessage in the changes collection.
//
// Changes are new, unpersisted events that have been applied to the aggregate.
func (a *AggregateBase) TrackChange(event EventMessage) {a.changes = append(a.changes, event)
}
// GetChanges returns the collection of new unpersisted events that have
// been applied to the aggregate.
func (a *AggregateBase) GetChanges() []EventMessage {return a.changes}
//ClearChanges removes all unpersisted events from the aggregate.
func (a *AggregateBase) ClearChanges() {a.changes = []EventMessage{}}
AggregateBase 定义了 id、version、changes 属性;AggregateID 办法返回 id;OriginalVersion 办法返回 version;CurrentVersion 返回 version+len(a.changes);IncrementVersion 递增 version;TrackChange 办法往 changes 减少 event;GetChanges 办法返回 changes;ClearChanges 会清空 changes
EventMessage
// EventMessage is the interface that a command must implement.
type EventMessage interface {
// AggregateID returns the ID of the Aggregate that the event relates to
AggregateID() string
// GetHeaders returns the key value collection of headers for the event.
//
// Headers are metadata about the event that do not form part of the
// actual event but are still required to be persisted alongside the event.
GetHeaders() map[string]interface{}
// SetHeader sets the value of the header specified by the key
SetHeader(string, interface{})
// Returns the actual event which is the payload of the event message.
Event() interface{}
// EventType returns a string descriptor of the command name
EventType() string
// Version returns the version of the event
Version() *int}
EventMessage 接口定义了 AggregateID、GetHeaders、SetHeader、Event、EventType、Version 办法
EventDescriptor
// EventDescriptor is an implementation of the event message interface.
type EventDescriptor struct {
id string
event interface{}
headers map[string]interface{}
version *int
}
// NewEventMessage returns a new event descriptor
func NewEventMessage(aggregateID string, event interface{}, version *int) *EventDescriptor {
return &EventDescriptor{
id: aggregateID,
event: event,
headers: make(map[string]interface{}),
version: version,
}
}
// EventType returns the name of the event type as a string.
func (c *EventDescriptor) EventType() string {return typeOf(c.event)
}
// AggregateID returns the ID of the Aggregate that the event relates to.
func (c *EventDescriptor) AggregateID() string {return c.id}
// GetHeaders returns the headers for the event.
func (c *EventDescriptor) GetHeaders() map[string]interface{} {return c.headers}
// SetHeader sets the value of the header specified by the key
func (c *EventDescriptor) SetHeader(key string, value interface{}) {c.headers[key] = value
}
// Event the event payload of the event message
func (c *EventDescriptor) Event() interface{} {return c.event}
// Version returns the version of the event
func (c *EventDescriptor) Version() *int {return c.version}
EventDescriptor 定义了 id、event、headers、version 属性;其 EventType 返回 typeOf(c.event);AggregateID 返回 id;GetHeaders 返回 headers;SetHeader 会设置 header;Event 办法返回 event;Version 返回 version
小结
go.cqrs 的 AggregateRoot 接口定义了 AggregateID、OriginalVersion、CurrentVersion、IncrementVersion、Apply、TrackChange、GetChanges、ClearChanges 办法。
doc
- go.cqrs