温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

go.cqrs中AggregateRoot的用法

发布时间:2021-07-12 11:15:07 来源:亿速云 阅读:134 作者:chen 栏目:编程语言

这篇文章主要讲解了“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的用法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI