聊聊go.cqrs的dispatcher

codecraft      2022-02-12     686

关键词:

本文主要研究一下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

  • go.cqrs

iosgcd使用dispatch_after、dispatch_time、dispatch_walltime

...经常会有需要一个方法或者处理需要延迟一段时间执行,dispatch_after函数就是用来实现这种功能打印结果如下dispatch_after有三个参数,第一个参与用来指定时间,传入的是dispatch_time_t类型的值,通过dispatch_time和dispatch_walltime函数... 查看详情

dispatch_queue_create、dispatch_retain、dispatch_release函数的使用

参考技术A该函数的第一个参数指定SerialDispatchQueue的名称。推荐使用应用程序ID这种逆序全程域名。第二个参数,当生成SerialDispatchQueue时,第二个参数设置NULL。当生成ConcurrentDispatchQueue,指定为DISPATCH_QUEUE_CONCURRENT通过dispatch_queue_... 查看详情

GCD dispatch_sync 优先于先前排队的 dispatch_async

】GCDdispatch_sync优先于先前排队的dispatch_async【英文标题】:GCDdispatch_syncpriorityoverpreviouslyqueueddispatch_async【发布时间】:2014-11-0718:09:44【问题描述】:我有一个包装数据模型并由多个线程访问/修改的类。我需要确保对数据模型的... 查看详情

dispatch_once的秘密

原文:https://www.mikeash.com/pyblog/friday-qa-2014-06-06-secrets-of-dispatch_once.html我的总结:dispatch_once_t要线程安全,还要快dispatch_once_t读多,写少执行dispat 查看详情

等待 Dispatcher.InvokeAsync 与 Dispatcher.Invoke

】等待Dispatcher.InvokeAsync与Dispatcher.Invoke【英文标题】:awaitDispatcher.InvokeAsyncvsDispatcher.Invoke【发布时间】:2021-09-0404:56:12【问题描述】:我有一个带有按钮的WPF程序,它创建和显示一些数据绑定到网格的数据。创建数据的过程非... 查看详情

dispatch_walltime与dispatch_time_t的区别(代码片段)

dispatch_walltime的官方文档解释如下Functiondispatch_walltime//根据系统时钟,创建一个绝对时间Createsadispatch_time_tusinganabsolutetimeaccordingtothewallclock.Declarationdispatch_time_tdispatch_walltime(conststructtimespec*wh 查看详情

redux 的 dispatch() 中的 [[Scopes]] 是啥

】redux的dispatch()中的[[Scopes]]是啥【英文标题】:Whatis[[Scopes]]indispatch()ofreduxredux的dispatch()中的[[Scopes]]是什么【发布时间】:2018-01-1209:20:07【问题描述】:我正在使用带有react的redux。这使得dispatch可以作为组件中的props使用。所以... 查看详情

在 launch(Dispatchers.IO) 中调用的每个函数是不是也在 IO Dispatcher 中调用?

】在launch(Dispatchers.IO)中调用的每个函数是不是也在IODispatcher中调用?【英文标题】:Iseveryfunctionthatiscalledinsidelaunch(Dispatchers.IO)alsocalledintheIODispatcher?在launch(Dispatchers.IO)中调用的每个函数是否也在IODispatcher中调用?【发布时间】... 查看详情

dispatch是啥意思

参考技术Adispatch英[dɪˈspætʃ]美[dɪˈspætʃ]vt.派遣;调遣;派出;发出,发送(邮件、包裹、信息);迅速处理;迅速办妥;迅速完成;杀死n.派遣;调遣;发送;(军事人员或政府官员之间的)急件,快信;(驻外国记者发给报... 查看详情

event_base_dispatch() 是如何工作的?

】event_base_dispatch()是如何工作的?【英文标题】:Howdoesevent_base_dispatch()work?【发布时间】:2015-06-1210:26:43【问题描述】:当event_base_dispatch()函数被调用时,内部会发生什么?是否有创建的线程会一直运行,直到收到停止信号?【... 查看详情

主队列上的 dispatch_sync 与 dispatch_async

】主队列上的dispatch_sync与dispatch_async【英文标题】:dispatch_syncvs.dispatch_asynconmainqueue【发布时间】:2011-09-2604:40:02【问题描述】:请耐心等待,这需要一些解释。我有一个类似于下面的函数。上下文:“aProject”是一个名为LPProjec... 查看详情

dispatch_semaphore

...发数一个是可以一个一个的执行任务-(void)testMaxConcurrent{dispatch_group_tgroup=dispatch_group_create();dispatch_semaphore_tsemaphore=dispatch_semaphore_create(1 查看详情

『ios』dispatch_once死锁和滥用单例导致的问题

参考技术A在学习dispatch_once原理过程中,发现了之前因为信号量引起的卡住主线程的问题所在。所以,了解原理,绝对是提高自己的必备条件。我们带着两个问题去看1.单例为什么会造成死锁。2.滥用单例为什么会导致内存不断增... 查看详情

【umijs】在非组件中调用dispatch

参考技术A这需要在request.ts中调用umiJS的dispatch函数。脱离了hock文件使用dva的dispatch函数,怎么搞?可以用过umi暴露的getDvaApp方法获取dva实例,再获取dispatch方法,如下所示: 查看详情

关于filter配置中dispatcher的使用(代码片段)

dispatcher使用方式如下:<filter><filter-name>testFilter</filter-name><filter-class>filter.TestFilter</filter-class></filter><filter-mapping><filter-name>testFilter</filter-name><url-pattern>/test2.jsp</url-pattern><dispatche... 查看详情

Store.Dispatch() 重置 Redux 存储

】Store.Dispatch()重置Redux存储【英文标题】:Store.Dispatch()ResettingReduxStore【发布时间】:2021-12-1615:47:48【问题描述】:我dispatch(action())从我的反应组件外部触发action。它工作正常,因为它正在触发我的操作并更新我的store中的新项... 查看详情

单例的销毁(代码片段)

单例的代码实现staticPerson*ple;staticdispatch_once_tpredicate;dispatch_once(&predicate,^NSLog(@"2:%ld",predicate);ple=[[Personalloc]init];);单例的底层实现原理voiddispatch_once(dispatch_once_t*val,dispatch_block_tblock dispatch_once_f(val,block,_dispatch_Block_invo... 查看详情

单例的销毁(代码片段)

单例的代码实现staticPerson*ple;staticdispatch_once_tpredicate;dispatch_once(&predicate,^NSLog(@"2:%ld",predicate);ple=[[Personalloc]init];);单例的底层实现原理voiddispatch_once(dispatch_once_t*val,dispatch_block_tblock dispatch_once_f(val,block,_dispatch_Block_invo... 查看详情