您好,登录后才能下订单哦!
本篇文章为大家展示了go.cqrs中EventHandler的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
type EventHandler interface {
	Handle(EventMessage)
}EventHandler定义了Handle方法
// 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 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 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)方法。
上述内容就是go.cqrs中EventHandler的作用是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。