聊聊go.cqrs的EventHandler

本文解析了go.cqrs库中EventHandler的概念及其工作原理。EventHandler定义了一个Handle方法用于处理事件,而EventMessage则定义了事件消息的标准接口,包括获取聚合ID、事件头部等方法。EventDescriptor实现了EventMessage接口,并通过PublishEvent方法将事件发布到所有注册的事件处理器。

本文主要研究一下go.cqrs的EventHandler

EventHandler

type EventHandler interface {
	Handle(EventMessage)
}

EventHandler定义了Handle方法

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属性,它实现了EventMessage接口

PublishEvent

// PublishEvent publishes events to all registered event handlers
func (b *InternalEventBus) PublishEvent(event EventMessage) {
	if handlers, ok := b.eventHandlers[event.EventType()]; ok {
		for handler := range handlers {
			handler.Handle(event)
		}
	}
}

InternalEventBus的PublishEvent方法会遍历指定event.EventType()的handlers,挨个执行handler.Handle(event)方法

小结

go.cqrs的EventHandler定义了Handle方法;InternalEventBus的PublishEvent方法会遍历指定event.EventType()的handlers,挨个执行handler.Handle(event)方法。

doc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值