前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊go.cqrs的EventHandler

聊聊go.cqrs的EventHandler

原创
作者头像
code4it
修改2021-04-12 10:30:47
4430
修改2021-04-12 10:30:47
举报
文章被收录于专栏:码匠的流水账码匠的流水账

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

EventHandler

代码语言:javascript
复制
type EventHandler interface {
    Handle(EventMessage)
}

EventHandler定义了Handle方法

EventMessage

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • EventHandler
  • EventMessage
  • EventDescriptor
  • PublishEvent
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档