From f991a621e463cb1ea3cd86930062db496baafaa0 Mon Sep 17 00:00:00 2001 From: mehdihadeli Date: Wed, 29 Nov 2023 17:24:46 +0330 Subject: [PATCH] . --- mediatr.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mediatr.go b/mediatr.go index 339bcfd..61b6485 100644 --- a/mediatr.go +++ b/mediatr.go @@ -8,6 +8,11 @@ import ( "github.com/pkg/errors" ) +// HandlerRegisterer for registering `RequestHandler` to mediatr registry, if handler implements this interface it will be registered automatically +type HandlerRegisterer interface { + RegisterHandler() error +} + // RequestHandlerFunc is a continuation for the next task to execute in the pipeline type RequestHandlerFunc func(ctx context.Context) (interface{}, error) @@ -20,6 +25,12 @@ type RequestHandler[TRequest any, TResponse any] interface { Handle(ctx context.Context, request TRequest) (TResponse, error) } +// RequestHandlerWithRegisterer for registering `RequestHandler` to mediatr registry and handling `RequestHandler` +type RequestHandlerWithRegisterer[TRequest any, TResponse any] interface { + HandlerRegisterer + RequestHandler[TRequest, TResponse] +} + type RequestHandlerFactory[TRequest any, TResponse any] func() RequestHandler[TRequest, TResponse] type NotificationHandler[TNotification any] interface { @@ -49,6 +60,11 @@ func registerRequestHandler[TRequest any, TResponse any](handler any) error { return nil } +// RegisterMarkedHandlers register all handlers that have implemented `HandlerRegisterer` for registering handlers. +func RegisterMarkedHandlers() { + // TODO: find all types that implement `HandlerRegisterer using reflection +} + // RegisterRequestHandler register the request handler to mediatr registry. func RegisterRequestHandler[TRequest any, TResponse any](handler RequestHandler[TRequest, TResponse]) error { return registerRequestHandler[TRequest, TResponse](handler)