|
|
|
@ -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, |