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}, 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 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 +}