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

聊聊go.cqrs的Dispatcher

作者头像
code4it
发布2021-04-16 14:35:51
3020
发布2021-04-16 14:35:51
举报
文章被收录于专栏:码匠的流水账码匠的流水账

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

Dispatcher

代码语言:javascript
复制
type Dispatcher interface {
    Dispatch(CommandMessage) error
    RegisterHandler(CommandHandler, ...interface{}) error
}

Dispatcher接口定义了Dispatch、RegisterHandler方法

InMemoryDispatcher

代码语言:javascript
复制
type InMemoryDispatcher struct {
    handlers map[string]CommandHandler
}

//NewInMemoryDispatcher constructs a new in memory dispatcher
func NewInMemoryDispatcher() *InMemoryDispatcher {
    b := &InMemoryDispatcher{
        handlers: make(map[string]CommandHandler),
    }
    return b
}

//Dispatch passes the CommandMessage on to all registered command handlers.
func (b *InMemoryDispatcher) Dispatch(command CommandMessage) error {
    if handler, ok := b.handlers[command.CommandType()]; ok {
        return handler.Handle(command)
    }
    return fmt.Errorf("The command bus does not have a handler for commands of type: %s", command.CommandType())
}

//RegisterHandler registers a command handler for the command types specified by the
//variadic commands parameter.
func (b *InMemoryDispatcher) RegisterHandler(handler CommandHandler, commands ...interface{}) error {
    for _, command := range commands {
        typeName := typeOf(command)
        if _, ok := b.handlers[typeName]; ok {
            return fmt.Errorf("Duplicate command handler registration with command bus for command of type: %s", typeName)
        }
        b.handlers[typeName] = handler
    }
    return nil
}

InMemoryDispatcher定义了map[string]CommandHandler属性,其Dispatch方法根据command.CommandType()获取handler,然后执行handler.Handle(command);其RegisterHandler方法遍历commands,然后获取command的type,挨个注册到map[string]CommandHandler中

CommandHandler

代码语言:javascript
复制
// CommandHandler is the interface that all command handlers should implement.
type CommandHandler interface {
    Handle(CommandMessage) error
}

// CommandMessage is the interface that a command message must implement.
type CommandMessage interface {

    // AggregateID returns the ID of the Aggregate that the command relates to
    AggregateID() string

    // Headers returns the key value collection of headers for the command.
    Headers() map[string]interface{}

    // SetHeader sets the value of the header specified by the key
    SetHeader(string, interface{})

    // Command returns the actual command which is the payload of the command message.
    Command() interface{}

    // CommandType returns a string descriptor of the command name
    CommandType() string
}

CommandHandler接口定义了Handle方法;CommandMessage接口定义了AggregateID、Headers、SetHeader、Command、CommandType方法

小结

go.cqrs的Dispatcher接口定义了Dispatch、RegisterHandler方法;InMemoryDispatcher定义了map[string]CommandHandler属性,其Dispatch方法根据command.CommandType()获取handler,然后执行handler.Handle(command);其RegisterHandler方法遍历commands,然后获取command的type,挨个注册到map[string]CommandHandler中。

doc

  • go.cqrs
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-04-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

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