Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Aggregate ¶
type Aggregate interface { aggregate.Aggregate // CommandNames returns the commands that the aggregate handles. CommandNames() []string // HandleCommand executes the command handler of the given command. HandleCommand(command.Context) error }
Aggregate is an aggregate that handles commands by itself.
type BaseHandler ¶
type BaseHandler struct {
// contains filtered or unexported fields
}
BaseHandler can be embedded into an aggregate to implement the aggregate interface. Provided methods are
- RegisterCommandHandler(string, func(command.Context) error)
- CommandNames() []string
- HandleCommand(command.Context) error
BaseHandler allows to do the following:
type MyAggregate struct { *aggregate.Base *handler.BaseHandler } func New(id uuid.UUID) *MyAggregate { foo := &MyAggregate{ Base: aggregate.NewBase("foo", id), BaseHandler: handler.NewBase(), } // Handle "foo" and "bar" commands using the provided handler function. command.HandelWith(foo, func(ctx command.Ctx[string]) error { ... }, "foo", "bar") return foo }
BaseHandler is not named Base to avoid name collisions with the aggregate.Base type.
func NewBase ¶
func NewBase(opts ...Option) *BaseHandler
NewBase returns a new *BaseHandler that can be embedded into an aggregate to implement the aggregate interface.
func (*BaseHandler) CommandNames ¶
func (base *BaseHandler) CommandNames() []string
CommandNames returns the commands that this handler (usually an aggregate) handles.
func (*BaseHandler) HandleCommand ¶
func (base *BaseHandler) HandleCommand(ctx command.Context) error
HandleCommand executes the command handler on the given command.
func (*BaseHandler) RegisterCommandHandler ¶
func (base *BaseHandler) RegisterCommandHandler(commandName string, handler func(command.Context) error)
RegisterHandler registers a command handler for the given command name.
type Of ¶
type Of[A Aggregate] struct { // contains filtered or unexported fields }
Of is a command handler for a specific aggregate. It subscribes to the commands of the aggregate and calls its registered command handlers. It is important that the provided newFunc that instantiates the aggregates has no side effects other than the setup of the aggregate, because in order to know which commands are handled by the aggregate, Of.Handle() initially creates an instance of the aggregate using the provided newFunc with a random UUID and calls CommandNames() on it to extract the command names from the registered handlers.
func New ¶
New returns a new command handler for commands of the given aggregate type that are published over the provided bus. Commands are handled by the aggregate itself, using the HandleCommand() method of the aggregate. The provided newFunc is used to instantiate the aggregates and to initially extract from the aggregate which commands it handles.
Under the hood, a generic *command.Handler is used.
type Option ¶
type Option func(*BaseHandler)
Option is an option for the BaseHandler.
func AfterHandle ¶
AfterHandle returns an Option that registers the provided function to be called after a command was handled. If command names are provided, the function will only be called for these commands; otherwise it will be called for every command.
func BeforeHandle ¶
BeforeHandle returns an Option that registers the provided function to be called before a command is handled. If the provided function returns a non-nil error, the command will not be handled. If command names are provided, the function will only be called for these commands; otherwise it will be called for every command.