From 99e07ca02f96bfd291b1e777f7c7fb23293dd253 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Tue, 11 Jun 2019 18:12:12 +0200 Subject: [PATCH 1/3] Add command subscriber function --- components/cqrs/command_processor.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/cqrs/command_processor.go b/components/cqrs/command_processor.go index 479e0c488..bcf97634d 100644 --- a/components/cqrs/command_processor.go +++ b/components/cqrs/command_processor.go @@ -33,6 +33,13 @@ type CommandHandler interface { // It allows you to create a separate customized Subscriber for every command handler. type CommandsSubscriberConstructor func(handlerName string) (message.Subscriber, error) +// CommandSubscriber provides a CommandsSubscriberConstructor for a single subscriber. +func CommandsSubscriber(subscriber message.Subscriber) CommandsSubscriberConstructor { + return func(handlerName string) (message.Subscriber, error) { + return subscriber, nil + } +} + // CommandProcessor determines which CommandHandler should handle the command received from the command bus. type CommandProcessor struct { handlers []CommandHandler From 23538abe0a26e949d98ed5e6f8c9dde3e2b12bcb Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Tue, 11 Jun 2019 18:13:39 +0200 Subject: [PATCH 2/3] Add topic generator shorthand function --- components/cqrs/topic.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 components/cqrs/topic.go diff --git a/components/cqrs/topic.go b/components/cqrs/topic.go new file mode 100644 index 000000000..a8e2dab59 --- /dev/null +++ b/components/cqrs/topic.go @@ -0,0 +1,13 @@ +package cqrs + +// Topic returns a generator function for a single topic name. +func Topic(topic string) func(string) string { + return func(string) string { + return topic + } +} + +// MessageTopic is a generator function returning the message name as a topic. +func MessageTopic(topic string) string { + return topic +} From b09127e6756def8e3fe9e9d52a29e355506fb672 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Tue, 11 Jun 2019 18:22:10 +0200 Subject: [PATCH 3/3] Update documentation --- _examples/cqrs-protobuf/main.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/_examples/cqrs-protobuf/main.go b/_examples/cqrs-protobuf/main.go index d102a44e8..aa23ce1e1 100644 --- a/_examples/cqrs-protobuf/main.go +++ b/_examples/cqrs-protobuf/main.go @@ -211,10 +211,8 @@ func main() { // cqrs.Facade is facade for Command and Event buses and processors. // You can use facade, or create buses and processors manually (you can inspire with cqrs.NewFacade) cqrsFacade, err := cqrs.NewFacade(cqrs.FacadeConfig{ - GenerateCommandsTopic: func(commandName string) string { - // we are using queue RabbitMQ config, so we need to have topic per command type - return commandName - }, + // we are using queue RabbitMQ config, so we need to have topic per command type + GenerateCommandsTopic: cqrs.MessageTopic, CommandHandlers: func(cb *cqrs.CommandBus, eb *cqrs.EventBus) []cqrs.CommandHandler { return []cqrs.CommandHandler{ BookRoomHandler{eb}, @@ -222,17 +220,12 @@ func main() { } }, CommandsPublisher: commandsPublisher, - CommandsSubscriberConstructor: func(handlerName string) (message.Subscriber, error) { - // we can reuse subscriber, because all commands have separated topics - return commandsSubscriber, nil - }, - GenerateEventsTopic: func(eventName string) string { - // because we are using PubSub RabbitMQ config, we can use one topic for all events - return "events" - // we can also use topic per event type - // return eventName - }, + // we can reuse subscriber, because all commands have separated topics + CommandsSubscriberConstructor: cqrs.CommandSubscriber(commandsSubscriber), + + // because we are using PubSub RabbitMQ config, we can use one topic for all events + GenerateEventsTopic: cqrs.Topic("events"), EventHandlers: func(cb *cqrs.CommandBus, eb *cqrs.EventBus) []cqrs.EventHandler { return []cqrs.EventHandler{ OrderBeerOnRoomBooked{cb},