command

package
v0.0.0-...-fff96b5 Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package command contains types and interfaces for implementing Command Handlers and dispatching Domain Commands, necessary for producing side effects in your Aggregates and system, and implement your Domain's business logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command interface{}

Command is a marker interface that represents a Domain Command.

type Dispatcher

type Dispatcher interface {
	Dispatch(context.Context, eventually.Command) error
}

Dispatcher represents a component that routes Domain Commands into their appropriate Command Handlers.

Dispatchers might be synchronous, where the Command is directly sent to the Command Handler and the business logic is executed synchronously.

Dispatchers might also be asynchronous, where the Command is send to an external queue and return from the Dispatch call immediately, while the Command Handler picks up the Command from the external queue and executes the business logic.

Make sure to pick the right kind of Command Dispatcher that suits your application needs.

type Handler

type Handler interface {
	// CommandType returns an empty instance of the Command type the Handler
	// is supposed to handle.
	CommandType() Command

	// Handle receives a Domain Command and executes the necessary business logic.
	//
	// Since Commands' responsibility is only to trigger side effects, and not
	// answering Domain Queries, the Handle method only returns an error, whether
	// the Command has failed execution or not.
	Handle(context.Context, eventually.Command) error
}

Handler is the interface that defines a Command Handler, component that receives a specific kind of Command and executes the business logic related to that particular Command.

type HandlerFunc

type HandlerFunc func(context.Context, Command) error

HandlerFunc is a functional Command Handler.

func (HandlerFunc) Handle

func (fn HandlerFunc) Handle(ctx context.Context, cmd eventually.Command) error

Handle executes the functional Command Handler.

type SimpleBus

type SimpleBus struct {
	// contains filtered or unexported fields
}

SimpleBus is a simple Command Bus implementation that allows to register Command Handlers and dispatch Domain Commands.

Use NewSimpleBus to create a new SimpleBus instance.

func NewSimpleBus

func NewSimpleBus() *SimpleBus

NewSimpleBus returns a new instance of SimpleBus.

func (SimpleBus) Dispatch

func (b SimpleBus) Dispatch(ctx context.Context, cmd eventually.Command) error

Dispatch accepts incoming Domain Commands and route them to their appropriate Command Handlers, if registered.

An error is returned if no Command Handler has been registered for the Domain Command submitted.

Dispatching strategy for SimpleBus is synchronous, meaning that the Dispatch method returns when the Command Handler finished execution. If the Command Handler fails execution, returning an error, this error is also returned by the Dispatch invocation.

func (*SimpleBus) Register

func (b *SimpleBus) Register(handler Handler)

Register adds the specified Command Handler into the SimpleBus routing table, so as to be able to start dispatching Commands to the Handler when calling Dispatch.

Please note, when registering multiple Handlers accepting the same Command type, the last registration will overwrite any previous Handler registration, as this SimpleBus does not support voting.

Jump to

Keyboard shortcuts

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