jomaxso / Segres

A mediator library for .Net using strongly-typed handler implementations. It provides a synchronise and asynchronise api, which is optimized for speed and memory.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool



Segres

SEGRES

The simple way to segerate your responsibilities.

Explore the docs »

View Demo · Report Bug · Request Feature

NuGet version NuGet downloads Test status

A mediator library for .Net using strongly-typed handler implementations. It provides a synchronise and asynchronise api, which is optimized for speed and memory.

Getting Started

Segres can be installed using the Nuget package manager or the dotnet CLI.

dotnet add package Segres 

Explore the documentation for instructions on how to use the package.

Example

Segres

Register all dependencies

// Program.cs

using Segres

...

builder.Services.AddSegres(); 

...

Segres.Abstractions

Create a handler

// CreateCustomerRequestHandler.cs

using Segres;

public record CreateCustomerRequest(string Firstname, string Lastname) : IRequest<Guid>;

public sealed class CreateCustomerRequestHandler : IRequestHandler<CreateCustomerRequest, Guid>
{
    public async ValueTask<Guid> HandleAsync(CreateCustomerRequest request, CancellationToken cancellationToken)
    {
        await ValueTask.CompletedTask;
        return Guid.NewGuid();
    }
} 

Send a request

// SomeService.cs

using Segres;

public class SomeService
{
    private readonly IMediator _mediator;

    public SomeService(IMediator mediator)
    {
        _mediator = mediator;
    }
    
    ...
    
    public async ValueTask<Guid> SomeMethodAsync(CancellationToken cancellationToken)
    {
        var request = new CreateCustomerRequest("Peter", "Parker");
        Guid id = await _mediator.SendAsync(request, cancellationToken);
        return id;
    }
}

Segres.AspNetCore

Register all endpoints

// Program.cs

using Segres

...

app.UseSegres(); 

...

Create a request

// CreateUserRequest.cs

public record CreateUserRequest() : IHttpRequest<int>
{
    public static string EndpointRoute => "/create";
    public static RequestType RequestType => RequestType.Post;
}

Create an endpoint for a request

// CreateUserEndpoint.cs

public sealed class CreateUserEndpoint : AbstractEndpoint<CreateUserRequest, int>
{
    public override async ValueTask<HttpResult<int>> ResolveAsync(CreateUserRequest request, CancellationToken cancellationToken)
    {
        int result = await ...
        return Ok(result)
    }
}

For more examples, please refer to the Documentation

License

Distributed under the MIT License. See LICENSE.md for more information.

About

A mediator library for .Net using strongly-typed handler implementations. It provides a synchronise and asynchronise api, which is optimized for speed and memory.

License:MIT License


Languages

Language:C# 100.0%