Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsandavari committed Mar 18, 2023
1 parent 5dec060 commit 854fbc0
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 131 deletions.
File renamed without changes
34 changes: 17 additions & 17 deletions examples/cqrs/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package main

import (
"context"
"github.com/ehsandavari/go-mediator"
"log"
"os"
"os/signal"
"syscall"

"github.com/mehdihadeli/go-mediatr"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/docs"
productApi "github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/api"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/creating_product/commands"
creatingProductsDtos "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"
gettingProductByIdDtos "github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/getting_product_by_id/dtos"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/getting_product_by_id/queries"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/repository"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/shared/behaviours"
"github.com/ehsandavari/go-mediator/examples/cqrs/docs"
productApi "github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/api"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/creating_product/commands"
creatingProductsDtos "github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/creating_product/dtos"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/creating_product/events"
gettingProductByIdDtos "github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/getting_product_by_id/dtos"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/getting_product_by_id/queries"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/repository"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/shared/behaviours"

"github.com/labstack/echo/v4"
echoSwagger "github.com/swaggo/echo-swagger"
Expand All @@ -32,33 +32,33 @@ func main() {
productRepository := repository.NewInMemoryProductRepository()

//////////////////////////////////////////////////////////////////////////////////////////////
// Register request handlers to the mediatr
// Register request handlers to the mediator

createProductCommandHandler := commands.NewCreateProductCommandHandler(productRepository)
getByIdQueryHandler := queries.NewGetProductByIdHandler(productRepository)

err := mediatr.RegisterRequestHandler[*commands.CreateProductCommand, *creatingProductsDtos.CreateProductCommandResponse](createProductCommandHandler)
err := mediator.RegisterRequestHandler[*commands.CreateProductCommand, *creatingProductsDtos.CreateProductCommandResponse](createProductCommandHandler)
if err != nil {
log.Fatal(err)
}

err = mediatr.RegisterRequestHandler[*queries.GetProductByIdQuery, *gettingProductByIdDtos.GetProductByIdQueryResponse](getByIdQueryHandler)
err = mediator.RegisterRequestHandler[*queries.GetProductByIdQuery, *gettingProductByIdDtos.GetProductByIdQueryResponse](getByIdQueryHandler)
if err != nil {
log.Fatal(err)
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Register notification handlers to the mediatr
// Register notification handlers to the mediator
notificationHandler := events.NewProductCreatedEventHandler()
err = mediatr.RegisterNotificationHandler[*events.ProductCreatedEvent](notificationHandler)
err = mediator.RegisterNotificationHandler[*events.ProductCreatedEvent](notificationHandler)
if err != nil {
log.Fatal(err)
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Register request handlers pipeline to the mediatr
// Register request handlers pipeline to the mediator
loggerPipeline := &behaviours.RequestLoggerBehaviour{}
err = mediatr.RegisterRequestPipelineBehaviors(loggerPipeline)
err = mediator.RegisterRequestPipelineBehaviors(loggerPipeline)
if err != nil {
log.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions examples/cqrs/internal/products/api/products_controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package api

import (
"github.com/mehdihadeli/go-mediatr"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/creating_product/commands"
creatingProductsDtos "github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/creating_product/dtos"
gettingProductByIdDtos "github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/getting_product_by_id/dtos"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/getting_product_by_id/queries"
"github.com/ehsandavari/go-mediator"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/creating_product/commands"
creatingProductsDtos "github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/creating_product/dtos"
gettingProductByIdDtos "github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/getting_product_by_id/dtos"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/getting_product_by_id/queries"
"net/http"

"github.com/go-playground/validator"
Expand Down Expand Up @@ -43,7 +43,7 @@ func (pc *ProductsController) createProduct() echo.HandlerFunc {
}

command := commands.NewCreateProductCommand(request.Name, request.Description, request.Price)
result, err := mediatr.Send[*commands.CreateProductCommand, *creatingProductsDtos.CreateProductCommandResponse](ctx.Request().Context(), command)
result, err := mediator.Send[*commands.CreateProductCommand, *creatingProductsDtos.CreateProductCommandResponse](ctx.Request().Context(), command)

if err != nil {
return err
Expand Down Expand Up @@ -76,7 +76,7 @@ func (pc *ProductsController) getProductByID() echo.HandlerFunc {
return err
}

queryResult, err := mediatr.Send[*queries.GetProductByIdQuery, *gettingProductByIdDtos.GetProductByIdQueryResponse](ctx.Request().Context(), query)
queryResult, err := mediator.Send[*queries.GetProductByIdQuery, *gettingProductByIdDtos.GetProductByIdQueryResponse](ctx.Request().Context(), query)

if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package commands

import (
"context"
"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"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/models"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/repository"
"github.com/ehsandavari/go-mediator"
creatingProductDtos "github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/creating_product/dtos"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/creating_product/events"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/models"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/repository"
)

type CreateProductCommandHandler struct {
Expand Down Expand Up @@ -34,10 +34,10 @@ func (c *CreateProductCommandHandler) Handle(ctx context.Context, command *Creat

response := &creatingProductDtos.CreateProductCommandResponse{ProductID: createdProduct.ProductID}

// Publish notification event to the mediatr for dispatching to the notification handlers
// Publish notification event to the mediator for dispatching to the notification handlers

productCreatedEvent := events.NewProductCreatedEvent(createdProduct.ProductID, createdProduct.Name, createdProduct.Description, createdProduct.Price, createdProduct.CreatedAt)
err = mediatr.Publish[*events.ProductCreatedEvent](ctx, productCreatedEvent)
err = mediator.Publish[*events.ProductCreatedEvent](ctx, productCreatedEvent)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dtos

import (
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/dtos"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/dtos"
)

type GetProductByIdQueryResponse struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package queries
import (
"context"
"fmt"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products"
gettingProductByIdDtos "github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/getting_product_by_id/dtos"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/repository"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products"
gettingProductByIdDtos "github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/features/getting_product_by_id/dtos"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/repository"

"github.com/pkg/errors"
)
Expand Down
4 changes: 2 additions & 2 deletions examples/cqrs/internal/products/mapper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package products

import (
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/dtos"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/models"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/dtos"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/models"
)

func MapProductToProductDto(product *models.Product) *dtos.ProductDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package repository

import (
"context"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/models"
"github.com/ehsandavari/go-mediator/examples/cqrs/internal/products/models"

uuid "github.com/satori/go.uuid"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package behaviours

import (
"context"
"github.com/mehdihadeli/go-mediatr"
"github.com/ehsandavari/go-mediator"
"log"
)

type RequestLoggerBehaviour struct {
}

func (r *RequestLoggerBehaviour) Handle(ctx context.Context, request interface{}, next mediatr.RequestHandlerFunc) (interface{}, error) {
func (r *RequestLoggerBehaviour) Handle(ctx context.Context, request interface{}, next mediator.RequestHandlerFunc) (interface{}, error) {
log.Printf("logging some stuff before handling the request")

response, err := next()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mehdihadeli/go-mediatr
module github.com/ehsandavari/go-mediator

go 1.18
go 1.20

require (
github.com/ahmetb/go-linq/v3 v3.2.0
Expand Down
34 changes: 17 additions & 17 deletions mediatr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mediatr
package mediator

import (
"context"
Expand All @@ -9,11 +9,11 @@ import (
)

// RequestHandlerFunc is a continuation for the next task to execute in the pipeline
type RequestHandlerFunc func() (interface{}, error)
type RequestHandlerFunc func() (any, error)

// PipelineBehavior is a Pipeline behavior for wrapping the inner handler.
type PipelineBehavior interface {
Handle(ctx context.Context, request interface{}, next RequestHandlerFunc) (interface{}, error)
Handle(ctx context.Context, request any, next RequestHandlerFunc) (any, error)
}

type RequestHandler[TRequest any, TResponse any] interface {
Expand All @@ -24,13 +24,13 @@ type NotificationHandler[TNotification any] interface {
Handle(ctx context.Context, notification TNotification) error
}

var requestHandlersRegistrations = map[reflect.Type]interface{}{}
var notificationHandlersRegistrations = map[reflect.Type][]interface{}{}
var pipelineBehaviours []interface{} = []interface{}{}
var requestHandlersRegistrations = map[reflect.Type]any{}
var notificationHandlersRegistrations = map[reflect.Type][]any{}
var pipelineBehaviours []any

type Unit struct{}

// RegisterRequestHandler register the request handler to mediatr registry.
// RegisterRequestHandler register the request handler to mediator registry.
func RegisterRequestHandler[TRequest any, TResponse any](handler RequestHandler[TRequest, TResponse]) error {
var request TRequest
requestType := reflect.TypeOf(request)
Expand All @@ -46,7 +46,7 @@ func RegisterRequestHandler[TRequest any, TResponse any](handler RequestHandler[
return nil
}

// RegisterRequestPipelineBehaviors register the request behaviors to mediatr registry.
// RegisterRequestPipelineBehaviors register the request behaviors to mediator registry.
func RegisterRequestPipelineBehaviors(behaviours ...PipelineBehavior) error {
for _, behavior := range behaviours {
behaviorType := reflect.TypeOf(behavior)
Expand All @@ -62,14 +62,14 @@ func RegisterRequestPipelineBehaviors(behaviours ...PipelineBehavior) error {
return nil
}

// RegisterNotificationHandler register the notification handler to mediatr registry.
// RegisterNotificationHandler register the notification handler to mediator registry.
func RegisterNotificationHandler[TEvent any](handler NotificationHandler[TEvent]) error {
var event TEvent
eventType := reflect.TypeOf(event)

handlers, exist := notificationHandlersRegistrations[eventType]
if !exist {
notificationHandlersRegistrations[eventType] = []interface{}{handler}
notificationHandlersRegistrations[eventType] = []any{handler}
return nil
}

Expand All @@ -78,7 +78,7 @@ func RegisterNotificationHandler[TEvent any](handler NotificationHandler[TEvent]
return nil
}

// RegisterNotificationHandlers register the notification handlers to mediatr registry.
// RegisterNotificationHandlers register the notification handlers to mediator registry.
func RegisterNotificationHandlers[TEvent any](handlers ...NotificationHandler[TEvent]) error {
if len(handlers) == 0 {
return errors.New("no handlers provided")
Expand All @@ -95,11 +95,11 @@ func RegisterNotificationHandlers[TEvent any](handlers ...NotificationHandler[TE
}

func ClearRequestRegistrations() {
requestHandlersRegistrations = map[reflect.Type]interface{}{}
requestHandlersRegistrations = map[reflect.Type]any{}
}

func ClearNotificationRegistrations() {
notificationHandlersRegistrations = map[reflect.Type][]interface{}{}
notificationHandlersRegistrations = map[reflect.Type][]any{}
}

// Send the request to its corresponding request handler.
Expand All @@ -120,15 +120,15 @@ 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() (any, error) {
return handlerValue.Handle(ctx, request)
}

aggregateResult := linq.From(reversPipes).AggregateWithSeedT(lastHandler, func(next RequestHandlerFunc, pipe PipelineBehavior) RequestHandlerFunc {
pipeValue := pipe
nexValue := next

var handlerFunc RequestHandlerFunc = func() (interface{}, error) {
var handlerFunc RequestHandlerFunc = func() (any, error) {
return pipeValue.Handle(ctx, request, nexValue)
}

Expand Down Expand Up @@ -180,8 +180,8 @@ func Publish[TNotification any](ctx context.Context, notification TNotification)
return nil
}

func reversOrder(values []interface{}) []interface{} {
var reverseValues []interface{}
func reversOrder(values []any) []any {
var reverseValues []any

for i := len(values) - 1; i >= 0; i-- {
reverseValues = append(reverseValues, values[i])
Expand Down
6 changes: 3 additions & 3 deletions mediatr_benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mediatr
package mediator

import (
"context"
Expand All @@ -8,7 +8,7 @@ import (

func Benchmark_Send(b *testing.B) {
// because benchmark method will run multiple times, we need to reset the request handler registry before each run.
requestHandlersRegistrations = make(map[reflect.Type]interface{})
requestHandlersRegistrations = make(map[reflect.Type]any)

handler := &RequestTestHandler{}
errRegister := RegisterRequestHandler[*RequestTest, *ResponseTest](handler)
Expand All @@ -28,7 +28,7 @@ func Benchmark_Send(b *testing.B) {

func Benchmark_Publish(b *testing.B) {
// because benchmark method will run multiple times, we need to reset the notification handlers registry before each run.
notificationHandlersRegistrations = make(map[reflect.Type][]interface{})
notificationHandlersRegistrations = make(map[reflect.Type][]any)

handler := &NotificationTestHandler{}
handler2 := &NotificationTestHandler4{}
Expand Down
Loading

0 comments on commit 854fbc0

Please sign in to comment.