溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

go.cqrs中Dispatcher的用法

發(fā)布時間:2021-07-10 15:08:40 來源:億速云 閱讀:379 作者:chen 欄目:編程語言

這篇文章主要講解了“go.cqrs中Dispatcher的用法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“go.cqrs中Dispatcher的用法”吧!

本文主要研究一下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方法根據(jù)command.CommandType()獲取handler,然后執(zhí)行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方法

小結(jié)

go.cqrs的Dispatcher接口定義了Dispatch、RegisterHandler方法;InMemoryDispatcher定義了map[string]CommandHandler屬性,其Dispatch方法根據(jù)command.CommandType()獲取handler,然后執(zhí)行handler.Handle(command);其RegisterHandler方法遍歷commands,然后獲取command的type,挨個注冊到map[string]CommandHandler中。

感謝各位的閱讀,以上就是“go.cqrs中Dispatcher的用法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對go.cqrs中Dispatcher的用法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI