CQRS: rename Bus to Dispatcher
This commit is contained in:
parent
76dea96a46
commit
5d53e32316
|
@ -4,7 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bus struct {
|
type Dispatcher struct {
|
||||||
reg *Registry
|
reg *Registry
|
||||||
cmdFactory CommandFactory
|
cmdFactory CommandFactory
|
||||||
cmdResultFactory CommandResultFactory
|
cmdResultFactory CommandResultFactory
|
||||||
|
@ -12,13 +12,13 @@ type Bus struct {
|
||||||
qryResultFactory QueryResultFactory
|
qryResultFactory QueryResultFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bus) Exec(ctx context.Context, req CommandRequest) (CommandResult, error) {
|
func (d *Dispatcher) Exec(ctx context.Context, req CommandRequest) (CommandResult, error) {
|
||||||
cmd, err := b.cmdFactory(req)
|
cmd, err := d.cmdFactory(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
hdlr, mdlwrs, err := b.reg.MatchCommand(cmd)
|
hdlr, mdlwrs, err := d.reg.MatchCommand(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func (b *Bus) Exec(ctx context.Context, req CommandRequest) (CommandResult, erro
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := b.cmdResultFactory(cmd)
|
result, err := d.cmdResultFactory(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -41,13 +41,13 @@ func (b *Bus) Exec(ctx context.Context, req CommandRequest) (CommandResult, erro
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bus) Query(ctx context.Context, req QueryRequest) (QueryResult, error) {
|
func (d *Dispatcher) Query(ctx context.Context, req QueryRequest) (QueryResult, error) {
|
||||||
qry, err := b.qryFactory(req)
|
qry, err := d.qryFactory(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
hdlr, mdlwrs, err := b.reg.MatchQuery(qry)
|
hdlr, mdlwrs, err := d.reg.MatchQuery(qry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ func (b *Bus) Query(ctx context.Context, req QueryRequest) (QueryResult, error)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := b.qryResultFactory(qry, data)
|
result, err := d.qryResultFactory(qry, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -71,18 +71,18 @@ func (b *Bus) Query(ctx context.Context, req QueryRequest) (QueryResult, error)
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bus) RegisterCommand(match MatchFunc, hdlr CommandHandler, mdlwrs ...CommandMiddleware) {
|
func (d *Dispatcher) RegisterCommand(match MatchFunc, hdlr CommandHandler, mdlwrs ...CommandMiddleware) {
|
||||||
b.reg.RegisterCommand(match, hdlr, mdlwrs...)
|
d.reg.RegisterCommand(match, hdlr, mdlwrs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bus) RegisterQuery(match MatchFunc, hdlr QueryHandler, mdlwrs ...QueryMiddleware) {
|
func (d *Dispatcher) RegisterQuery(match MatchFunc, hdlr QueryHandler, mdlwrs ...QueryMiddleware) {
|
||||||
b.reg.RegisterQuery(match, hdlr, mdlwrs...)
|
d.reg.RegisterQuery(match, hdlr, mdlwrs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBus(funcs ...OptionFunc) *Bus {
|
func NewDispatcher(funcs ...OptionFunc) *Dispatcher {
|
||||||
opt := CreateOption(funcs...)
|
opt := CreateOption(funcs...)
|
||||||
|
|
||||||
return &Bus{
|
return &Dispatcher{
|
||||||
reg: NewRegistry(),
|
reg: NewRegistry(),
|
||||||
cmdFactory: opt.CommandFactory,
|
cmdFactory: opt.CommandFactory,
|
||||||
cmdResultFactory: opt.CommandResultFactory,
|
cmdResultFactory: opt.CommandResultFactory,
|
|
@ -3,7 +3,7 @@ package cqrs
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrInvalidRequestType = errors.New("invalid request type")
|
|
||||||
ErrHandlerNotFound = errors.New("handler not found")
|
ErrHandlerNotFound = errors.New("handler not found")
|
||||||
ErrUnexpectedRequest = errors.New("unexpected request")
|
ErrUnexpectedRequest = errors.New("unexpected request")
|
||||||
|
ErrUnexpectedData = errors.New("unexpected data")
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,7 +12,7 @@ func MatchCommandRequest(req interface{}) MatchFunc {
|
||||||
return func(raw interface{}) (bool, error) {
|
return func(raw interface{}) (bool, error) {
|
||||||
cmd, ok := raw.(Command)
|
cmd, ok := raw.(Command)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, ErrInvalidRequestType
|
return false, ErrUnexpectedRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
return reflect.TypeOf(cmd.Request()) == reqType, nil
|
return reflect.TypeOf(cmd.Request()) == reqType, nil
|
||||||
|
@ -25,7 +25,7 @@ func MatchQueryRequest(req interface{}) MatchFunc {
|
||||||
return func(raw interface{}) (bool, error) {
|
return func(raw interface{}) (bool, error) {
|
||||||
cmd, ok := raw.(Query)
|
cmd, ok := raw.(Query)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, ErrInvalidRequestType
|
return false, ErrUnexpectedRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
return reflect.TypeOf(cmd.Request()) == reqType, nil
|
return reflect.TypeOf(cmd.Request()) == reqType, nil
|
||||||
|
|
|
@ -5,9 +5,9 @@ import "gitlab.com/wpetit/goweb/service"
|
||||||
// ServiceProvider returns a service.Provider for the
|
// ServiceProvider returns a service.Provider for the
|
||||||
// the cqrs service
|
// the cqrs service
|
||||||
func ServiceProvider() service.Provider {
|
func ServiceProvider() service.Provider {
|
||||||
bus := NewBus()
|
dispatcher := NewDispatcher()
|
||||||
|
|
||||||
return func(container *service.Container) (interface{}, error) {
|
return func(container *service.Container) (interface{}, error) {
|
||||||
return bus, nil
|
return dispatcher, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,13 @@ import (
|
||||||
const ServiceName service.Name = "cqrs"
|
const ServiceName service.Name = "cqrs"
|
||||||
|
|
||||||
// From retrieves the cqrs service in the given container
|
// 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)
|
raw, err := container.Service(ServiceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||||
}
|
}
|
||||||
|
|
||||||
srv, ok := raw.(*Bus)
|
srv, ok := raw.(*Dispatcher)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
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
|
// 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)
|
service, err := From(container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
Loading…
Reference in New Issue