cqrs

package
v0.32.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrHandlerNotFound = errors.New("cqrs: handler not found")

ErrHandlerNotFound is returned when no handler is registered for a command/query type.

View Source
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.

View Source
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

func Register

func Register[TAction Action, TResult any](bus Bus, handler Handler[TAction, TResult])

Register registers a type-safe handler for command type C. Panics if a handler is already registered for the same command type.

func Send

func Send[TAction Action, TResult any](ctx context.Context, bus Bus, action TAction) (TResult, error)

Send dispatches a command through the behavior pipeline to its registered handler.

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.

func (BehaviorFunc) Handle

func (f BehaviorFunc) Handle(ctx context.Context, action Action, next func(ctx context.Context) (any, error)) (any, error)

type Bus

type Bus interface {
	// contains filtered or unexported methods
}

Bus is the command/query dispatch bus interface.

func NewBus

func NewBus(behaviors []Behavior) Bus

NewBus creates a new Bus with the given behavior middlewares. Behaviors implementing Ordered are wrapped outside-in by ascending Order; ties and behaviors without Order share the FX-supplied position.

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.

func (HandlerFunc[TAction, TResult]) Handle

func (f HandlerFunc[TAction, TResult]) Handle(ctx context.Context, action TAction) (TResult, error)

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

type TypedHandler[TAction Action, TResult any] struct {
	// contains filtered or unexported fields
}

TypedHandler wraps a generic Handler and provides a type-erased dispatch method.

type Unit

type Unit struct{}

Unit is a placeholder return type for commands that produce no result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL