go.cqrs中AggregateRoot的用法

这篇文章主要讲解了“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方法。

感谢各位的阅读,以上就是“go.cqrs中AggregateRoot的用法”的内容了,经过本文的学习后,相信大家对go.cqrs中AggregateRoot的用法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!

相关推荐:

go.cqrs中DomainRepository的作用是什么

go.cqrs中DomainRepository的作用是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。DomainRepository// DomainRepository is the in...

go.cqrs中EventHandler的作用是什么

本篇文章为大家展示了go.cqrs中EventHandler的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。EventHandlertype EventHandler interface { Handle(EventM...

学习Linux如何

这期内容当中小编将会给大家带来有关学习Linux如何,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。  Linux作为当今中高端服务器的主要操作系统,具有高效率、易于裁剪、应用广等优势。并且可安装在各种计算机硬件设备中,比如手机、平板电脑、路由器、视频游戏控制台、台...

Spring常见的十大错误,78%的老程序员都踩过这些坑!

首先我们来看一下,Spring常见错误有那些 太过关注底 内部结构“泄露” 缺乏关注点分离 缺乏异常处理或处理不当 多线程处理不当 不使用基于注解的验证 (依旧)使用基于xml的配置 忽略profile 无法接受依赖项注入 缺乏测试,或测试不当 接下来就一一介绍这些常见的错误1.错误一:太过关注底层...

好程序员Java学习路线分享Spring创建Bean的3种方式

好程序员Java学习路线分享Spring创建Bean的3种方式,本文讲解了在Spring应用中创建Bean的多种方式,包括自动创建,以及手动创建注入方式,实际开发中可以根据业务场景选择合适的方案。方式1:使用SpringXML方式配置,该方式用于在纯Spring应用中,适用于简单的小应用,当应用变得...

追踪JVM中的本地内存

1.概述 有没有想过为什么Java应用程序通过众所周知的-Xms和-Xmx调优标志消耗的内存比指定数量多得多?出于各种原因和可能的优化,JVM可以分配额外的本机内存。这些额外的分配最终会使消耗的内存超出-Xmx限制。 在本教程中,我们将列举JVM中的一些常见内存分配源,以及它们的大小调整标志,然后学...

在用Spring boot搭建项目的时候出现 “无法访问此网站“ 是什么情况

这篇文章主要讲解了“在用Springboot搭建项目的时候出现“无法访问此网站“是什么情况”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“在用Springboot搭建项目的时候出现“无法访问此网站“是什么情况”吧!在用Springboot搭建项目的时候...