From e9968cb87f04661871405b80daa30a00b874babe Mon Sep 17 00:00:00 2001 From: Mehdi Hadeli Date: Fri, 6 Oct 2023 00:31:44 +0330 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20add=20ctx=20to=20RequestHa?= =?UTF-8?q?ndlerFunc=20for=20keeping=20the=20flow=20o=E2=80=A6=20(#12)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commands/create_product_handler.go | 6 ++++++ .../shared/behaviours/request_logger_behaviour.go | 8 ++++++-- mediatr.go | 10 +++++----- mediatr_test.go | 4 ++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/examples/cqrs/internal/products/features/creating_product/commands/create_product_handler.go b/examples/cqrs/internal/products/features/creating_product/commands/create_product_handler.go index 5818bf0..261b285 100644 --- a/examples/cqrs/internal/products/features/creating_product/commands/create_product_handler.go +++ b/examples/cqrs/internal/products/features/creating_product/commands/create_product_handler.go @@ -2,6 +2,8 @@ package commands import ( "context" + "fmt" + "github.com/mehdihadeli/go-mediatr" creatingProductDtos "github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/creating_product/dtos" "github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/creating_product/events" @@ -18,6 +20,10 @@ func NewCreateProductCommandHandler(productRepository *repository.InMemoryProduc } func (c *CreateProductCommandHandler) Handle(ctx context.Context, command *CreateProductCommand) (*creatingProductDtos.CreateProductCommandResponse, error) { + isLoggerPipelineEnabled := ctx.Value("logger_pipeline").(bool) + if isLoggerPipelineEnabled { + fmt.Println("[CreateProductCommandHandler]: logging pipeline is enabled") + } product := &models.Product{ ProductID: command.ProductID, diff --git a/examples/cqrs/internal/shared/behaviours/request_logger_behaviour.go b/examples/cqrs/internal/shared/behaviours/request_logger_behaviour.go index 6920cda..5f13a6f 100644 --- a/examples/cqrs/internal/shared/behaviours/request_logger_behaviour.go +++ b/examples/cqrs/internal/shared/behaviours/request_logger_behaviour.go @@ -2,8 +2,9 @@ package behaviours import ( "context" - "github.com/mehdihadeli/go-mediatr" "log" + + "github.com/mehdihadeli/go-mediatr" ) type RequestLoggerBehaviour struct { @@ -12,7 +13,10 @@ type RequestLoggerBehaviour struct { func (r *RequestLoggerBehaviour) Handle(ctx context.Context, request interface{}, next mediatr.RequestHandlerFunc) (interface{}, error) { log.Printf("logging some stuff before handling the request") - response, err := next() + // https://golang.org/pkg/context/#Context + ctx = context.WithValue(ctx, "logger_pipeline", true) + + response, err := next(ctx) if err != nil { return nil, err } diff --git a/mediatr.go b/mediatr.go index 76b846d..339bcfd 100644 --- a/mediatr.go +++ b/mediatr.go @@ -9,7 +9,7 @@ import ( ) // RequestHandlerFunc is a continuation for the next task to execute in the pipeline -type RequestHandlerFunc func() (interface{}, error) +type RequestHandlerFunc func(ctx context.Context) (interface{}, error) // PipelineBehavior is a Pipeline behavior for wrapping the inner handler. type PipelineBehavior interface { @@ -116,7 +116,7 @@ func RegisterNotificationHandlers[TEvent any](handlers ...NotificationHandler[TE return nil } -// RegisterNotificationHandlers register the notification handlers factories to mediatr registry. +// RegisterNotificationHandlersFactories register the notification handlers factories to mediatr registry. func RegisterNotificationHandlersFactories[TEvent any](factories ...NotificationHandlerFactory[TEvent]) error { if len(factories) == 0 { return errors.New("no handlers provided") @@ -172,7 +172,7 @@ func Send[TRequest any, TResponse any](ctx context.Context, request TRequest) (T if len(pipelineBehaviours) > 0 { var reversPipes = reversOrder(pipelineBehaviours) - var lastHandler RequestHandlerFunc = func() (interface{}, error) { + var lastHandler RequestHandlerFunc = func(ctx context.Context) (interface{}, error) { return handlerValue.Handle(ctx, request) } @@ -180,7 +180,7 @@ func Send[TRequest any, TResponse any](ctx context.Context, request TRequest) (T pipeValue := pipe nexValue := next - var handlerFunc RequestHandlerFunc = func() (interface{}, error) { + var handlerFunc RequestHandlerFunc = func(ctx context.Context) (interface{}, error) { return pipeValue.Handle(ctx, request, nexValue) } @@ -188,7 +188,7 @@ func Send[TRequest any, TResponse any](ctx context.Context, request TRequest) (T }) v := aggregateResult.(RequestHandlerFunc) - response, err := v() + response, err := v(ctx) if err != nil { return *new(TResponse), errors.Wrap(err, "error handling request") diff --git a/mediatr_test.go b/mediatr_test.go index e9e8360..19d9b04 100644 --- a/mediatr_test.go +++ b/mediatr_test.go @@ -455,7 +455,7 @@ func (c *PipelineBehaviourTest) Handle(ctx context.Context, request interface{}, fmt.Println("PipelineBehaviourTest.Handled") testData = append(testData, "PipelineBehaviourTest") - res, err := next() + res, err := next(ctx) if err != nil { return nil, err } @@ -471,7 +471,7 @@ func (c *PipelineBehaviourTest2) Handle(ctx context.Context, request interface{} fmt.Println("PipelineBehaviourTest2.Handled") testData = append(testData, "PipelineBehaviourTest2") - res, err := next() + res, err := next(ctx) if err != nil { return nil, err }