query

package
v0.1.0-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package query contains types and components for implementing Domain Queries and Query Handlers for producing Domain Read Models, which should compose the Read API of your application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Answer

type Answer interface{}

Answer is a marker interface for answers to Domain Queries, returned by a Query Handler.

type Dispatcher

type Dispatcher interface {
	Dispatch(context.Context, Query) (Answer, error)
}

Dispatcher represents a component that routes Domain Queries into their appropriate Query Handlers.

Differently from Command Handlers, Query Handlers return an Answer to the Domain Query dispatched, which usually is the Domain Read Model for a specific query type.

Implementations can be synchronous or asynchronous, although it is generally used as a synchronous component, i.e. the call to Dispatch returns when the query has been resolved.

type Handler

type Handler interface {
	QueryType() Query
	Handle(context.Context, Query) (Answer, error)
}

Handler is a component capable of handling Domain Queries, resolving them into an Answer, typically a Domain Read Model.

An Handler implementation specifies their supported Query type using the QueryType method. Please, make sure the type returned by this method reflects the same type of the Query submitted in your application.

type Query

type Query interface{}

Query is a marker interface for Domain Query types.

type SimpleBus

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

SimpleBus is a simple Query Bus implementation, synchronous and that works in-memory by registering Query Handlers and dispatching Domain Queries, the Bus will take care of routing the Query to the appropriate Query Handler.

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, query Query) (Answer, error)

Dispatch routes the provided Domain Query to the appropriate Query Handler registered in the Bus, if any, and returns the produced Answer.

If the submitted Query has not been registered by any Handler, an error will be returned instead.

func (*SimpleBus) Register

func (b *SimpleBus) Register(handler Handler)

Register adds the specified Query Handler to the SimpleBus routing table, associated with the Query Handler's Domain Query type, so that calls to Dispatch using the specified Domain Query type will be routed to this Query Handler.

Please note, when registering multiple Handlers accepting the same Query type, the last registration call will overwrite any previous Handler registration, as SimpleBus, intentionally, does not support any kind of voting mechanism.

Jump to

Keyboard shortcuts

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