聊聊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 条评论
推荐阅读
2022年终总结
最近两年开始陷入颓废中,博客也写的越来越少了。究其原因,主要还是陷入了职业倦怠期,最近一次跳槽感觉颇为失败,但是碍于给的薪资高,为了五斗米折腰,又加上最近行情不好,想要往外跳也跳不了,就这样子一直...

codecraft阅读 858

前端如何入门 Go 语言
类比法是一种学习方法,它是通过将新知识与已知知识进行比较,从而加深对新知识的理解。在学习 Go 语言的过程中,我发现,通过类比已有的前端知识,可以更好地理解 Go 语言的特性。

robin22阅读 3.2k评论 4

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

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

年度最佳【golang】map详解
这篇文章主要讲 map 的赋值、删除、查询、扩容的具体执行过程,仍然是从底层的角度展开。结合源码,看完本文一定会彻底明白 map 底层原理。

去去100216阅读 11.5k评论 2

年度最佳【golang】GMP调度详解
Golang最大的特色可以说是协程(goroutine)了, 协程让本来很复杂的异步编程变得简单, 让程序员不再需要面对回调地狱, 虽然现在引入了协程的语言越来越多, 但go中的协程仍然是实现的是最彻底的. 这篇文章将通过分析...

去去100215阅读 11.8k评论 4

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

JavaGuide8阅读 1.6k

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

白鲸鱼9阅读 5.2k

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

11.8k 声望
2k 粉丝
宣传栏