聊聊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


code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很...

11.8k 声望
2k 粉丝
0 条评论
推荐阅读
Java20的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft1阅读 927

Golang 中 []byte 与 string 转换
string 类型和 []byte 类型是我们编程时最常使用到的数据结构。本文将探讨两者之间的转换方式,通过分析它们之间的内在联系来拨开迷雾。

机器铃砍菜刀24阅读 58.5k评论 2

万字详解,吃透 MongoDB!
MongoDB 是一个基于 分布式文件存储 的开源 NoSQL 数据库系统,由 C++ 编写的。MongoDB 提供了 面向文档 的存储方式,操作起来比较简单和容易,支持“无模式”的数据建模,可以存储比较复杂的数据类型,是一款非常...

JavaGuide8阅读 1.8k

封面图
数据结构与算法:二分查找
一、常见数据结构简单数据结构(必须理解和掌握)有序数据结构:栈、队列、链表。有序数据结构省空间(储存空间小)无序数据结构:集合、字典、散列表,无序数据结构省时间(读取时间快)复杂数据结构树、 堆图二...

白鲸鱼9阅读 5.4k

PHP转Go实践:xjson解析神器「开源工具集」
我和劲仔都是PHP转Go,身边越来越多做PHP的朋友也逐渐在用Go进行重构,重构过程中,会发现php的json解析操作(系列化与反序列化)是真的香,弱类型语言的各种隐式类型转换,很大程度的减低了程序的复杂度。

王中阳Go11阅读 2.8k评论 4

封面图
Git操作不规范,战友提刀来相见!
年终奖都没了,还要扣我绩效,门都没有,哈哈。这波骚Git操作我也是第一次用,担心闪了腰,所以不仅做了备份,也做了笔记,分享给大家。问题描述小A和我在同时开发一个功能模块,他在优化之前的代码逻辑,我在开...

王中阳Go6阅读 2.9k评论 4

封面图
妙啊,空结构体还能这么用?Go语言的结构体看这篇就够了
本文详解了Go语言结构体的各个知识点,最后介绍了空结构体的3种妙用。希望对你有帮助。定义结构体,是一种自定义的数据类型,由多个数据类型组合而成。用于描述一类事物相关属性。定义方式: {代码...} 实例化结...

王中阳Go6阅读 1.3k

封面图

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很...

11.8k 声望
2k 粉丝
宣传栏