程序员

聊聊go.cqrs的Dispatcher

作者:admin 2021-04-14 我要评论

序 本文主要研究一下go.cqrs的Dispatcher Dispatcher type Dispatcher interface { Dispatch(CommandMessage) error RegisterHandler(CommandHandler, ...interf...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

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

Dispatcher

type Dispatcher interface {
    Dispatch(CommandMessage) error
    RegisterHandler(CommandHandler, ...interface{}) error
}
Dispatcher接口定义了Dispatch、RegisterHandler方法

InMemoryDispatcher

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

// 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


本文转自网络,版权归原作者所有,原文链接:https://segmentfault.com/a/1190000039801677

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 四两拨千斤——你不知道的VScode编码Ty

    四两拨千斤——你不知道的VScode编码Ty

  • 我是如何在 Vue 项目中做代码分割的

    我是如何在 Vue 项目中做代码分割的

  • position:sticky 粘性定位的几种巧妙应

    position:sticky 粘性定位的几种巧妙应

  • 从零到一搭建React组件库

    从零到一搭建React组件库

腾讯云代理商
海外云服务器