NetHandler 1.0.1

dotnet add package NetHandler --version 1.0.1
                    
NuGet\Install-Package NetHandler -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetHandler" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetHandler" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="NetHandler" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NetHandler --version 1.0.1
                    
#r "nuget: NetHandler, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NetHandler@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NetHandler&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=NetHandler&version=1.0.1
                    
Install as a Cake Tool

NetHandler

NetHandler é uma biblioteca .NET focada em simplificar a comunicação e o desacoplamento entre componentes da sua aplicação. Inspirada nos princípios do mediator, ela oferece uma abordagem intuitiva para implementar os padrões de Command e Query, além de suportar notificações, permitindo construir aplicações mais modulares, testáveis e fáceis de manter.

Se você busca uma alternativa leve e com uma curva de aprendizado suave ao MediatR, o NetHandler oferece uma solução eficiente para orquestrar o fluxo de trabalho da sua aplicação através de mensagens e handlers dedicados.

Exemplo de configuração IoC


public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Registra os handlers e o dispatchr, procurando no assembly atual.
        services.AddNetHandler(Assembly.GetExecutingAssembly());

        // Registra os notification handlers automaticamente
        services.AddNetNotificationHandlers(Assembly.GetExecutingAssembly());

        // Outros serviços...
        services.AddControllers();
    }
    
    // Resto da configuração do Startup...
}

Exemplo de utilização RequestHandler


    // Definição de uma request (comando) que espera uma resposta (neste caso, um bool).
    public class CreateOrderCommand : IRequest<bool>
    {
        public string OrderId { get; set; }
        public decimal Amount { get; set; }
    }

    // Implementação do handler para a request CreateOrderCommand.
    public class CreateOrderHandler : IRequestHandler<CreateOrderCommand, bool>
    {
        public async Task<bool> Handle(CreateOrderCommand command, CancellationToken cancellationToken)
        {
            // Lógica de criação do pedido.
            Console.WriteLine($"Criando pedido {command.OrderId} com valor {command.Amount}.");
            await Task.CompletedTask;
            return true;
        }
    }

     ...

    // Exemplo de uso no programa:

    // Resolve o Dispatchr configurado via DI.
    var dispatchr = serviceProvider.GetRequiredService<IDispatchr>();

    // Cria o comando (request) para criar um pedido.
    var command = new CreateOrderCommand
    {
        OrderId = "XYZ789",
        Amount = 123.45m
    };

    // Envia o comando e aguarda a resposta.
    // O Dispatchr utilizará reflection e cache para resolver o handler adequado (CreateOrderHandler).
    bool result = await dispatchr.SendAsync<bool>(command);

    Console.WriteLine($"Comando processado. Resultado: {result}");

Exemplo de utilização Notification


    public class OrderCreatedNotification : INotification
    {
        public string OrderId { get; set; }
        public DateTime CreatedAt { get; set; }
    }

    // Exemplo de handler que processa a notificação
    public class NotifyAdminHandler : INotificationHandler<OrderCreatedNotification>
    {
        public async Task Handle(OrderCreatedNotification notification, CancellationToken cancellationToken)
        {
            Console.WriteLine($"[Admin] Pedido {notification.OrderId} criado em {notification.CreatedAt}.");
            await Task.CompletedTask;
        }
    }
     ...

    // Exemplo de uso no programa:

    // Resolve o Dispatchr
    var dispatchr = serviceProvider.GetRequiredService<IDispatchr>();

    // Cria a notificação (por exemplo, um evento de pedido criado)
    var notification = new OrderCreatedNotification
    {
        OrderId = "ABC123",
        CreatedAt = DateTime.Now
    };

    // Publica a notificação: todos os handlers registrados para OrderCreatedNotification serão executados.
    await dispatchr.PublishAsync(notification);
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 196 4/10/2025
1.0.0 182 4/9/2025

Adicionado opcão de notificação em paralelo