Documentation
¶
Index ¶
- Variables
- func Register[TAction Action, TResult any](bus Bus, handler Handler[TAction, TResult])
- func Send[TAction Action, TResult any](ctx context.Context, bus Bus, action TAction) (TResult, error)
- type Action
- type ActionKind
- type BaseCommand
- type BaseQuery
- type Behavior
- type BehaviorFunc
- type Bus
- type CommandQueryBus
- type Dispatcher
- type Handler
- type HandlerFunc
- type Ordered
- type TypedHandler
- type Unit
Constants ¶
This section is empty.
Variables ¶
var ErrHandlerNotFound = errors.New("cqrs: handler not found")
ErrHandlerNotFound is returned when no handler is registered for a command/query type.
var ErrResultTypeMismatch = errors.New("cqrs: result type mismatch")
ErrResultTypeMismatch is returned when the dispatched value cannot be asserted to the caller's expected result type. The normal handler path always yields the correct type; this guards a misbehaving Behavior that short-circuits with a non-nil value of the wrong concrete type, turning an opaque runtime panic into a typed error.
var Module = fx.Module( "vef:cqrs", fx.Provide( fx.Annotate( NewBus, fx.ParamTags(`group:"vef:cqrs:behaviors"`), ), ), )
Module provides the CQRS Bus to the DI container.
Functions ¶
Types ¶
type Action ¶
type Action interface {
// Kind returns whether this action is a Command or a Query.
Kind() ActionKind
}
Action is the base interface for all commands and queries.
type ActionKind ¶
type ActionKind int
ActionKind distinguishes commands from queries.
const ( Command ActionKind = iota Query )
type BaseCommand ¶
type BaseCommand struct{}
BaseCommand is embedded by command types to mark them as commands.
func (BaseCommand) Kind ¶
func (BaseCommand) Kind() ActionKind
type BaseQuery ¶
type BaseQuery struct{}
BaseQuery is embedded by query types to mark them as queries.
func (BaseQuery) Kind ¶
func (BaseQuery) Kind() ActionKind
type Behavior ¶
type Behavior interface {
// Handle intercepts command/query execution; call next to continue the pipeline.
Handle(ctx context.Context, action Action, next func(ctx context.Context) (any, error)) (any, error)
}
Behavior is a Bus middleware that can intercept all commands/queries.
type BehaviorFunc ¶
type BehaviorFunc func(ctx context.Context, action Action, next func(ctx context.Context) (any, error)) (any, error)
BehaviorFunc is a function adapter for Behavior.
type Bus ¶
type Bus interface {
// contains filtered or unexported methods
}
Bus is the command/query dispatch bus interface.
type CommandQueryBus ¶
type CommandQueryBus struct {
// contains filtered or unexported fields
}
CommandQueryBus is the concrete Bus implementation.
type Dispatcher ¶
type Dispatcher interface {
// contains filtered or unexported methods
}
Dispatcher is the type-erased interface used for map lookup.
type Handler ¶
type Handler[TAction Action, TResult any] interface { // Handle executes the given command or query and returns the result. Handle(ctx context.Context, action TAction) (TResult, error) }
Handler is a type-safe command/query handler.
type HandlerFunc ¶
type HandlerFunc[TAction Action, TResult any] func(ctx context.Context, action TAction) (TResult, error)
HandlerFunc is a function adapter for Handler.
type Ordered ¶ added in v0.24.0
type Ordered interface {
// Order returns the sort key for the behavior. Lower values wrap
// outer; behaviors share an Order at the cost of an unstable relative
// position between them.
Order() int
}
Ordered is an optional interface for Behavior implementations that need deterministic wrapping order. The Bus sorts behaviors by Order ascending at construction time, so a behavior with a lower Order wraps a behavior with a higher Order (outermost first → innermost last). Behaviors that do not implement Ordered default to Order 0, in the order Uber FX produced them — which is not stable for value groups, so any behavior whose position matters MUST implement Ordered.
Order 0 is the outermost transactional band: an unordered behavior shares that band and wraps everything, which is rarely what a custom host behavior wants. Custom host behaviors must implement Ordered with a value in the 1000+ band to run inside the framework's own behaviors.
Conventional bands:
- 0–99 : transactional / contextual setup (must wrap everything; the default Order 0 falls here)
- 100–199 : audit / collector lifecycle (writes after handler succeeds)
- 200–299 : event publish / outbox (last buffered side effect)
- 1000+ : custom host behaviors (must implement Ordered to reach this band)
type TypedHandler ¶
TypedHandler wraps a generic Handler and provides a type-erased dispatch method.