The simple way to segerate your responsibilities.
Explore the docs »
A mediator library for .Net using strongly-typed handler implementations. It provides a synchronise and asynchronise api, which is optimized for speed and memory.
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.
// Program.cs
using Segres
...
builder.Services.AddSegres();
...
// 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();
}
}
// 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;
}
}
// Program.cs
using Segres
...
app.UseSegres();
...
// CreateUserRequest.cs
public record CreateUserRequest() : IHttpRequest<int>
{
public static string EndpointRoute => "/create";
public static RequestType RequestType => RequestType.Post;
}
// 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
Distributed under the MIT License. See LICENSE.md
for more information.