关于golang:聊聊gocqrs的EventHandler

5次阅读

共计 2421 个字符,预计需要花费 7 分钟才能阅读完成。

本文次要钻研一下 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

  • go.cqrs
正文完
 0