专栏首页码匠的流水账聊聊go.cqrs的AggregateRoot

聊聊go.cqrs的AggregateRoot

本文主要研究一下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

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2021-04-07

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊go.cqrs的AggregateRoot

    go.cqrs的AggregateRoot接口定义了AggregateID、OriginalVersion、CurrentVersion、IncrementVe...

    codecraft
  • 聊聊go.cqrs的DomainRepository

    go.cqrs的DomainRepository定义了Load、Save方法;GetEventStoreCommonDomainRepo实现了DomainRep...

    codecraft
  • 聊聊go.cqrs的DomainRepository

    go.cqrs的DomainRepository定义了Load、Save方法;GetEventStoreCommonDomainRepo实现了DomainRep...

    codecraft
  • 聊聊go.cqrs的Dispatcher

    go.cqrs的Dispatcher接口定义了Dispatch、RegisterHandler方法;InMemoryDispatcher定义了map[strin...

    codecraft
  • 聊聊go.cqrs的EventHandler

    go.cqrs的EventHandler定义了Handle方法;InternalEventBus的PublishEvent方法会遍历指定event.EventT...

    codecraft
  • 聊聊go.cqrs的Dispatcher

    go.cqrs的Dispatcher接口定义了Dispatch、RegisterHandler方法;InMemoryDispatcher定义了map[strin...

    codecraft
  • 聊聊go.cqrs的EventHandler

    go.cqrs的EventHandler定义了Handle方法;InternalEventBus的PublishEvent方法会遍历指定event.EventT...

    codecraft
  • TS与JS中的Getters和Setter究竟有什么用[每日前端夜话0x92]

    当 ECMAScript 5(2009)发布时,getters 和 setter(也称为访问器)被引入 JavaScript。

    疯狂的技术宅
  • 聊聊flink的TimeCharacteristic

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/Time...

    codecraft
  • 聊聊flink的SourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的CsvReader

    flink-java-1.6.2-sources.jar!/org/apache/flink/api/java/ExecutionEnvironment.jav...

    codecraft
  • 聊聊flink的BoltWrapper

    flink-storm_2.11-1.6.2-sources.jar!/org/apache/flink/storm/wrappers/BoltWrapper....

    codecraft
  • 聊聊flink的EventTime

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的ParallelIteratorInputFormat

    本文主要研究一下flink的ParallelIteratorInputFormat

    codecraft
  • 聊聊flink的RichParallelSourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的InputFormatSourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/envi...

    codecraft
  • 聊聊flink的MemoryStateBackend

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/StateBacken...

    codecraft
  • 聊聊flink的OperatorStateBackend

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/OperatorSta...

    codecraft
  • 聊聊flink的MemCheckpointStreamFactory

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/CheckpointS...

    codecraft

扫码关注云+社区

领取腾讯云代金券