handler

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 7 Imported by: 2

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 for a command.
	HandleCommand(command.Context) error
}

Aggregate is an aggregate that handles commands.

type Base added in v0.2.3

type Base struct {
	command.Handlers
	// contains filtered or unexported fields
}

BaseHandler can be embedded into an aggregate to implement command.Registerer.

  • 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.

type BaseHandler deprecated

type BaseHandler = Base

BaseHandler is an alias for Base.

Deprecated: Use Base instead.

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(name string, handler func(command.Context) error)

RegisterCommandHandler implements command.Registerer.

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

func New[A Aggregate](newFunc func(uuid.UUID) A, repo aggregate.Repository, bus command.Bus) *Of[A]

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.

func (*Of[A]) Handle

func (h *Of[A]) Handle(ctx context.Context) (<-chan error, error)

Handle subscribes to and handles the commands for which a handler has been registered. Command errors are sent into the returned error channel.

func (*Of[A]) MustHandle

func (h *Of[A]) MustHandle(ctx context.Context) <-chan error

MustHandle is like Handle but panics if there is an error.

type Option

type Option func(*Base)

Option is an option for the BaseHandler.

func AfterHandle

func AfterHandle[Payload any](fn func(command.Ctx[Payload]), commands ...string) Option

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

func BeforeHandle[Payload any](fn func(command.Ctx[Payload]) error, commands ...string) Option

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.

Jump to

Keyboard shortcuts

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