From 5d53e32316bd6e864fecf2946cb6e1cbe0e5d1dd Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 7 Jul 2020 08:19:15 +0200 Subject: [PATCH] CQRS: rename Bus to Dispatcher --- cqrs/{bus.go => dispatcher.go} | 30 +++++++++++++++--------------- cqrs/error.go | 6 +++--- cqrs/match.go | 4 ++-- cqrs/provider.go | 4 ++-- cqrs/service.go | 6 +++--- 5 files changed, 25 insertions(+), 25 deletions(-) rename cqrs/{bus.go => dispatcher.go} (56%) diff --git a/cqrs/bus.go b/cqrs/dispatcher.go similarity index 56% rename from cqrs/bus.go rename to cqrs/dispatcher.go index 7868c24..fe7d1c9 100644 --- a/cqrs/bus.go +++ b/cqrs/dispatcher.go @@ -4,7 +4,7 @@ import ( "context" ) -type Bus struct { +type Dispatcher struct { reg *Registry cmdFactory CommandFactory cmdResultFactory CommandResultFactory @@ -12,13 +12,13 @@ type Bus struct { qryResultFactory QueryResultFactory } -func (b *Bus) Exec(ctx context.Context, req CommandRequest) (CommandResult, error) { - cmd, err := b.cmdFactory(req) +func (d *Dispatcher) Exec(ctx context.Context, req CommandRequest) (CommandResult, error) { + cmd, err := d.cmdFactory(req) if err != nil { return nil, err } - hdlr, mdlwrs, err := b.reg.MatchCommand(cmd) + hdlr, mdlwrs, err := d.reg.MatchCommand(cmd) if err != nil { return nil, err } @@ -33,7 +33,7 @@ func (b *Bus) Exec(ctx context.Context, req CommandRequest) (CommandResult, erro return nil, err } - result, err := b.cmdResultFactory(cmd) + result, err := d.cmdResultFactory(cmd) if err != nil { return nil, err } @@ -41,13 +41,13 @@ func (b *Bus) Exec(ctx context.Context, req CommandRequest) (CommandResult, erro return result, nil } -func (b *Bus) Query(ctx context.Context, req QueryRequest) (QueryResult, error) { - qry, err := b.qryFactory(req) +func (d *Dispatcher) Query(ctx context.Context, req QueryRequest) (QueryResult, error) { + qry, err := d.qryFactory(req) if err != nil { return nil, err } - hdlr, mdlwrs, err := b.reg.MatchQuery(qry) + hdlr, mdlwrs, err := d.reg.MatchQuery(qry) if err != nil { return nil, err } @@ -63,7 +63,7 @@ func (b *Bus) Query(ctx context.Context, req QueryRequest) (QueryResult, error) return nil, err } - result, err := b.qryResultFactory(qry, data) + result, err := d.qryResultFactory(qry, data) if err != nil { return nil, err } @@ -71,18 +71,18 @@ func (b *Bus) Query(ctx context.Context, req QueryRequest) (QueryResult, error) return result, nil } -func (b *Bus) RegisterCommand(match MatchFunc, hdlr CommandHandler, mdlwrs ...CommandMiddleware) { - b.reg.RegisterCommand(match, hdlr, mdlwrs...) +func (d *Dispatcher) RegisterCommand(match MatchFunc, hdlr CommandHandler, mdlwrs ...CommandMiddleware) { + d.reg.RegisterCommand(match, hdlr, mdlwrs...) } -func (b *Bus) RegisterQuery(match MatchFunc, hdlr QueryHandler, mdlwrs ...QueryMiddleware) { - b.reg.RegisterQuery(match, hdlr, mdlwrs...) +func (d *Dispatcher) RegisterQuery(match MatchFunc, hdlr QueryHandler, mdlwrs ...QueryMiddleware) { + d.reg.RegisterQuery(match, hdlr, mdlwrs...) } -func NewBus(funcs ...OptionFunc) *Bus { +func NewDispatcher(funcs ...OptionFunc) *Dispatcher { opt := CreateOption(funcs...) - return &Bus{ + return &Dispatcher{ reg: NewRegistry(), cmdFactory: opt.CommandFactory, cmdResultFactory: opt.CommandResultFactory, diff --git a/cqrs/error.go b/cqrs/error.go index f32fa4e..791467e 100644 --- a/cqrs/error.go +++ b/cqrs/error.go @@ -3,7 +3,7 @@ package cqrs import "errors" var ( - ErrInvalidRequestType = errors.New("invalid request type") - ErrHandlerNotFound = errors.New("handler not found") - ErrUnexpectedRequest = errors.New("unexpected request") + ErrHandlerNotFound = errors.New("handler not found") + ErrUnexpectedRequest = errors.New("unexpected request") + ErrUnexpectedData = errors.New("unexpected data") ) diff --git a/cqrs/match.go b/cqrs/match.go index 2a863dd..ba8d462 100644 --- a/cqrs/match.go +++ b/cqrs/match.go @@ -12,7 +12,7 @@ func MatchCommandRequest(req interface{}) MatchFunc { return func(raw interface{}) (bool, error) { cmd, ok := raw.(Command) if !ok { - return false, ErrInvalidRequestType + return false, ErrUnexpectedRequest } return reflect.TypeOf(cmd.Request()) == reqType, nil @@ -25,7 +25,7 @@ func MatchQueryRequest(req interface{}) MatchFunc { return func(raw interface{}) (bool, error) { cmd, ok := raw.(Query) if !ok { - return false, ErrInvalidRequestType + return false, ErrUnexpectedRequest } return reflect.TypeOf(cmd.Request()) == reqType, nil diff --git a/cqrs/provider.go b/cqrs/provider.go index 42e961f..2d1e426 100644 --- a/cqrs/provider.go +++ b/cqrs/provider.go @@ -5,9 +5,9 @@ import "gitlab.com/wpetit/goweb/service" // ServiceProvider returns a service.Provider for the // the cqrs service func ServiceProvider() service.Provider { - bus := NewBus() + dispatcher := NewDispatcher() return func(container *service.Container) (interface{}, error) { - return bus, nil + return dispatcher, nil } } diff --git a/cqrs/service.go b/cqrs/service.go index fe6d4b7..74c5bf7 100644 --- a/cqrs/service.go +++ b/cqrs/service.go @@ -9,13 +9,13 @@ import ( const ServiceName service.Name = "cqrs" // From retrieves the cqrs service in the given container -func From(container *service.Container) (*Bus, error) { +func From(container *service.Container) (*Dispatcher, error) { raw, err := container.Service(ServiceName) if err != nil { return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName) } - srv, ok := raw.(*Bus) + srv, ok := raw.(*Dispatcher) if !ok { return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName) } @@ -24,7 +24,7 @@ func From(container *service.Container) (*Bus, error) { } // Must retrieves the cqrs service in the given container or panic otherwise -func Must(container *service.Container) *Bus { +func Must(container *service.Container) *Dispatcher { service, err := From(container) if err != nil { panic(err)