Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add shorthand functions for topic name and subscriber generators #96

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions _examples/cqrs-protobuf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,28 +211,21 @@ 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},
OrderBeerHandler{eb},
}
},
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},
Expand Down
7 changes: 7 additions & 0 deletions components/cqrs/command_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions components/cqrs/topic.go
Original file line number Diff line number Diff line change
@@ -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
}