8000 GitHub - TanerSaydam/TS.MediatR: Lightweight and flexible Mediator library for .NET with support for CQRS, pipeline behaviors, and notifications. Built for clean and scalable architecture.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Lightweight and flexible Mediator library for .NET with support for CQRS, pipeline behaviors, and notifications. Built for clean and scalable architecture.

License

Notifications You must be signed in to change notification settings

TanerSaydam/TS.MediatR

Repository files navigation

TS.MediatR

TS.MediatR is a lightweight and flexible CQRS/Mediator library for .NET. It supports IRequest, INotification, IPipelineBehavior, and works seamlessly with Dependency Injection.

🔧 Installation

Install via NuGet:

dotnet add package TS.MediatR

🚀 Getting Started

using TS.MediatR;

services.AddMediatR(options =>
{
    options.AddRegisterAssemblies(typeof(Program).Assembly);
    options.AddOpenBehavior(typeof(LoggingBehavior<,>)); //with response
    options.AddOpenBehavior(typeof(ValidationBehavior<>)); //no response
});

🧩 IRequest / IRequestHandler

Define a request and its corresponding handler:

With Response

public class GetUserQuery : IRequest<UserDto>
{
    public int Id { get; set; }
}

public class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto>
{
    public Task<UserDto> Handle(GetUserQuery request, CancellationToken cancellationToken)
    {
        var user = new UserDto { Id = request.Id, Name = "Jhon Doe" };
        return Task.FromResult(user);
    }
}

No Response

public class CreateUserCommand : IRequest
{
    public string Name { get; set; }
}

public class GetUserQueryHandler : IRequestHandler<CreateUserCommand>
{
    public Task Handle(GetUserQuery request, CancellationToken cancellationToken)
    {
        var user = new UserDto { Name = request.Name };
        return Task.FromResult(user);
    }
}

📤 Sending with ISender

Use ISender to send the request:

With Response

var result = await sender.Send(new GetUserQuery { Id = 1 });
Console.WriteLine(result.Name);

No Response

await sender.Send(new CreateUserCommand { Name = "Jhon Doe" });

🔁 Pipeline Behavior

Use IPipelineBehavior for cross-cutting concerns like logging, validation, etc.

With Response

public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    public async Task<TResponse> Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Request received: {typeof(TRequest).Name}");
        var response = await next();
        Console.WriteLine($"Response returned: {typeof(TResponse).Name}");
        return response;
    }
}

No Response

public class ValidationBehavior<TRequest> : IPipelineBehavior<TRequest>
    where TRequest : IRequest
{
    public async Task Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Request received: {typeof(TRequest).Name}");
        await next();
        Console.WriteLine($"Response returned: {typeof(TResponse).Name}");
        return response;
    }
}

📣 Notification Handling

Define a notification and its handler:

public class ProductCreatedEvent : INotification
{
    public Guid Id { get; set; }
    public ProductCreatedEvent(Guid id)
    {
        Id = id;
    }
}

public class SendEmailHandler : INotificationHandler<ProductCreatedEvent>
{
    public Task Handle(ProductCreatedEvent notification, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Id sent: {notification.Id}");
        return Task.CompletedTask;
    }
}

Publish a notification:

await sender.Publish(new ProductCreatedEvent(product.Id));

📦 Features

  • ✅ Request/Response (IRequest, IRequest<TResponse>)
  • ✅ Notifications (INotification)
  • ✅ Pipeline behaviors (IPipelineBehavior)
  • ✅ Dependency Injection ready
  • ✅ Scoped resolution and handler chaining
  • ✅ Fully async support

📁 License

Licensed under the MIT License.

About

Lightweight and flexible Mediator library for .NET with support for CQRS, pipeline behaviors, and notifications. Built for clean and scalable architecture.

Resources

License

Stars

Watchers

Forks

Languages

0