Skip to content

ET-NuGet-Packages/ETPackages.Mediator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Œ ETPackages.Mediator

Build & Publish NuGet Version NuGet Downloads Target Frameworks

ETPackages.Mediator is a simple and performance-oriented library for .NET. It supports IRequest, INotification, IPipelineBehavior, and works seamlessly with Dependency Injection.


πŸ“¦ Features

  • βœ… Request/Response (IRequest, IRequest<TResponse>)
  • βœ… Notifications (INotification)
  • βœ… Pipeline behaviors (IPipelineBehavior)
  • βœ… Dependency Injection ready
  • βœ… Fully async support

πŸ“‹ Requirements

  • .NET 8.0 or higher

πŸ“¦ Installation

Install via NuGet:

Install-Package ETPackages.Mediator

Or via the .NET Core command line interface:

dotnet add package ETPackages.Mediator

πŸš€ Getting Started

using ETPackages.Mediator;

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

🧩 IRequest / IRequestHandler

Define a request and its corresponding handler:

With Response

public class GetProductQuery : IRequest<ProductDto>
{
    public int Id { get; set; }
}

public class GetProductQueryHandler : IRequestHandler<GetProductQuery, ProductDto>
{
    public Task<ProductDto> Handle(GetProductQuery request, CancellationToken cancellationToken)
    {
        var product = new ProductDto { Id = request.Id, Title = "Domain-Driven Design: Tackling Complexity in the Heart of Software" };
        return Task.FromResult(product);
    }
}

No Response

public class CreateProductCommand : IRequest
{
    public string Title { get; set; }
}

public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand>
{
    public Task Handle(CreateProductCommand request, CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();

        // db record

        await Task.CompletedTask;
    }
}

πŸ“€ Sending with IMediator

Use IMediator to send the request:

With Response

var result = await mediator.Send(new GetProductQuery { Id = 1 });
Console.WriteLine(result.Title);

No Response

await mediator.Send(new CreateProductCommand { Title = "Domain-Driven Design: Tackling Complexity in the Heart of Software" });

πŸ” Pipeline Behavior

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

With Response

public interface ILoggableRequest
{
}

public class LoggingBehavior<TRequest, TResponse>(ILogger<LoggingBehavior<TRequest, TResponse>> logger) : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>, ILoggableRequest
{
    public async Task<TResponse> Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
    {
        var requestName = typeof(TRequest).Name;

        logger.LogInformation($"Request received: {requestName}");
        
        var response = await next();
        
        logger.LogInformation($"Response returned: {requestName}");
        
        return response;
    }
}

No Response

public class ValidationBehavior<TRequest>(ILogger<ValidationBehavior<TRequest>> logger) : IPipelineBehavior<TRequest>
    where TRequest : IRequest
{
    public async Task Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
    {
        var requestName = typeof(TRequest).Name;

        logger.LogInformation($"Request validation started: {requestName}");

        await next();

        logger.LogInformation($"Request validation finished: {requestName}");
    }
}

πŸ“£ Notification Handling

Define a notification and its handler:

public class ProductCreatedEvent : INotification
{
    public ProductCreatedEvent(int id)
    {
        Id = id;
    }

    public int Id { get; set; }
}

public class SendEmailHandler(ILogger<SendEmailHandler> logger) : INotificationHandler<ProductCreatedEvent>
{
    public Task Handle(ProductCreatedEvent notification, CancellationToken cancellationToken)
    {
        logger.LogInformation($"Id sent: {notification.Id}");

        // process send email

        return Task.CompletedTask;
    }
}

Publish a notification:

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

πŸ“ License

This project is licensed under the MIT License.

About

Simple and performance-oriented library for .NET with support for CQRS, pipeline behaviors, and notifications.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages