Documentation
ΒΆ
Overview ΒΆ
Mediatr package implements the mediator pattern for Go, providing:
- Request/response message handling - Notification broadcasting - Pipeline behaviors for cross-cutting concerns
The package is thread-safe and designed for high-performance concurrent use.
Index ΒΆ
- func ClearNotificationRegistrations()
- func ClearPipelineBehaviors()
- func ClearRequestRegistrations()
- func Publish[TNotification any](ctx context.Context, notification TNotification) error
- func RegisterNotificationHandler[TEvent any](handler NotificationHandler[TEvent]) error
- func RegisterNotificationHandlerFactory[TEvent any](factory NotificationHandlerFactory[TEvent]) error
- func RegisterNotificationHandlers[TEvent any](handlers ...NotificationHandler[TEvent]) error
- func RegisterNotificationHandlersFactories[TEvent any](factories ...NotificationHandlerFactory[TEvent]) error
- func RegisterRequestHandler[TRequest any, TResponse any](handler RequestHandler[TRequest, TResponse]) error
- func RegisterRequestHandlerFactory[TRequest any, TResponse any](factory RequestHandlerFactory[TRequest, TResponse]) error
- func RegisterRequestPipelineBehaviors(behaviours ...PipelineBehavior) error
- func Send[TRequest any, TResponse any](ctx context.Context, request TRequest) (TResponse, error)
- type NotificationHandler
- type NotificationHandlerFactory
- type PipelineBehavior
- type RequestHandler
- type RequestHandlerFactory
- type RequestHandlerFunc
- type Unit
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func ClearNotificationRegistrations ΒΆ added in v1.1.9
func ClearNotificationRegistrations()
ClearNotificationRegistrations removes all registered notification handlers.
func ClearPipelineBehaviors ΒΆ added in v1.4.0
func ClearPipelineBehaviors()
ClearPipelineBehaviors removes all registered pipeline behaviors.
func ClearRequestRegistrations ΒΆ added in v1.1.9
func ClearRequestRegistrations()
ClearRequestRegistrations removes all registered request handlers. Useful for testing scenarios.
func Publish ΒΆ
Publish broadcasts a notification to all registered handlers. All handlers are executed, even if some return errors. Returns the first error encountered, if any.
Example:
type OrderShipped struct { OrderID string }
// Register handlers
mediatr.RegisterNotificationHandlers[OrderShipped](
&ShippingNotifier{},
&InventoryUpdater{},
)
// Publish
err := mediatr.Publish(ctx, OrderShipped{OrderID: "123"})
if err != nil { /* handle error */ }
func RegisterNotificationHandler ΒΆ
func RegisterNotificationHandler[TEvent any](handler NotificationHandler[TEvent]) error
RegisterNotificationHandler registers a handler for notifications of specific type. Multiple handlers can be registered for the same notification type.
func RegisterNotificationHandlerFactory ΒΆ added in v1.1.10
func RegisterNotificationHandlerFactory[TEvent any](factory NotificationHandlerFactory[TEvent]) error
RegisterNotificationHandlerFactory registers a factory that creates notification handlers.
func RegisterNotificationHandlers ΒΆ
func RegisterNotificationHandlers[TEvent any](handlers ...NotificationHandler[TEvent]) error
RegisterNotificationHandlers registers multiple handlers for a notification type. Returns error if no handlers are provided or registration fails.
func RegisterNotificationHandlersFactories ΒΆ added in v1.1.10
func RegisterNotificationHandlersFactories[TEvent any](factories ...NotificationHandlerFactory[TEvent]) error
RegisterNotificationHandlersFactories registers multiple handler factories.
func RegisterRequestHandler ΒΆ
func RegisterRequestHandler[TRequest any, TResponse any](handler RequestHandler[TRequest, TResponse]) error
RegisterRequestHandler registers a request handler for a specific request type. Returns an error if a handler is already registered for the request type.
Example:
err := mediatr.RegisterRequestHandler[*MyRequest, *MyResponse](&MyHandler{})
func RegisterRequestHandlerFactory ΒΆ added in v1.1.10
func RegisterRequestHandlerFactory[TRequest any, TResponse any](factory RequestHandlerFactory[TRequest, TResponse]) error
RegisterRequestHandlerFactory registers a factory that creates request handlers. Useful for stateful handlers that need fresh instances per request.
func RegisterRequestPipelineBehaviors ΒΆ
func RegisterRequestPipelineBehaviors(behaviours ...PipelineBehavior) error
RegisterRequestPipelineBehaviors registers middleware behaviors that wrap request handlers. Behaviors are executed in registration order (first registered runs first). Returns error if any behavior is already registered.
func Send ΒΆ
Send dispatches a request to its registered handler and returns the response. Executes all registered pipeline behaviors in order. Returns error if: - No handler is registered for the request - Handler returns an error - Any pipeline behavior returns an error
Example:
response, err := mediatr.Send[*MyRequest, *MyResponse](ctx, &MyRequest{})
Types ΒΆ
type NotificationHandler ΒΆ
type NotificationHandler[TNotification any] interface { Handle(ctx context.Context, notification TNotification) error }
NotificationHandler processes notifications of a specific type. Multiple handlers can process the same notification.
type NotificationHandlerFactory ΒΆ added in v1.1.10
type NotificationHandlerFactory[TNotification any] func() NotificationHandler[TNotification]
NotificationHandlerFactory creates new instances of notification handlers.
type PipelineBehavior ΒΆ
type PipelineBehavior interface {
Handle(ctx context.Context, request interface{}, next RequestHandlerFunc) (interface{}, error)
}
PipelineBehavior defines middleware-like components that can intercept requests. Implement this interface to add cross-cutting concerns like logging, validation, etc.
type RequestHandler ΒΆ
type RequestHandler[TRequest any, TResponse any] interface { Handle(ctx context.Context, request TRequest) (TResponse, error) }
RequestHandler handles a specific request type and returns a response. Implement this interface for your request handlers.
Example:
type MyHandler struct{}
func (h *MyHandler) Handle(ctx context.Context, req MyRequest) (MyResponse, error) {
// handle request
}
type RequestHandlerFactory ΒΆ added in v1.1.10
type RequestHandlerFactory[TRequest any, TResponse any] func() RequestHandler[TRequest, TResponse]
RequestHandlerFactory creates new instances of a request handler. Useful when handlers need fresh instances per request.
type RequestHandlerFunc ΒΆ
RequestHandlerFunc is a continuation function used in pipeline behaviors. It represents the next handler in the pipeline chain.