Browse Source

CQRS: rename Bus to Dispatcher

extension
wpetit 2 years ago
parent
commit
5d53e32316
  1. 30
      cqrs/dispatcher.go
  2. 6
      cqrs/error.go
  3. 4
      cqrs/match.go
  4. 4
      cqrs/provider.go
  5. 6
      cqrs/service.go

30
cqrs/bus.go → 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,

6
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")
)

4
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

4
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
}
}

6
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)

Loading…
Cancel
Save