handler

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: Apache-2.0 Imports: 6 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 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

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(*BaseHandler)

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