FastEndpoints.MediatR 2.0.1

dotnet add package FastEndpoints.MediatR --version 2.0.1
NuGet\Install-Package FastEndpoints.MediatR -Version 2.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="FastEndpoints.MediatR" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FastEndpoints.MediatR --version 2.0.1
#r "nuget: FastEndpoints.MediatR, 2.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.
// Install FastEndpoints.MediatR as a Cake Addin
#addin nuget:?package=FastEndpoints.MediatR&version=2.0.1

// Install FastEndpoints.MediatR as a Cake Tool
#tool nuget:?package=FastEndpoints.MediatR&version=2.0.1

FastEndpoints.MediatR

Combines MediatR and FastEndpoint for quickly generating API endpoints from MediatR Requests

Request

Mark class or record as IMediatrEndpoint or IMediatrEndpoint<T> accordingly. This is an EndpointRequest Type in FastEndpoints and IRequest in MediatR

public record CreateEmployee(string Code, string Name) : IMediatrEndpoint;

Request handler

Impletement MediatrEndpointHandler<TRequest> or MediatrEndpointHandler<TRequest, TResponse> accordingly. This implements Endpoint<T> or Endpoint<T, TResponse> accordingly in FastEndpoints and IRequestHandler<TRequest, TResponse> in MediatR

internal sealed class CreateEmployeeHandler : MediatrEndpointHandler<CreateEmployee>
{
    private readonly IAppDbContext _dbContext;
    public CreateEmployeeHandler(IAppDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public override async Task<Result> Handle(CreateEmployee req, CancellationToken ct)
    {
        var employee = new Employee
        {
            Code = req.Code,
            Name = req.Name
        };

        await _dbContext.Employees.AddAsync(employee, ct);
        await _dbContext.SaveChangesAsync(ct);
        return Result.Succeed();
    }

    public override void Configure()
    {
        Post("create");
        Group<EmployeesGroup>();
        Summary("Create a new employee");
        RequestExample(new CreateEmployee("TEST0254", "Jone doe"));
        AllowAnonymous();
    }
}

Validator

Optionally you can use FluentValidation to generate validation for a request

internal sealed class CreateEmployeeValidator : AbstractValidator<CreateEmployee>
{
    private readonly IAppDbContext _dbContext;
    public CreateEmployeeValidator(IAppDbContext dbContext)
    {
        _dbContext = dbContext;
        RuleFor(x => x.Code).NotNull().MustAsync(beUniqueAsync).WithMessage("Code number already exists!");
        RuleFor(x => x.Name).NotNull();
    }

    private async Task<bool> beUniqueAsync(string code, CancellationToken ct)
    {
        return !await _dbContext.Employees.AnyAsync(x => x.Code == code, ct);
    }
}

Program

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
builder.Services.AddLogging();
builder.Services.AddFastEndpoints();
builder.Services.SwaggerDocument();

var app = builder.Build();

// Not require if you don't want to use FastEndpoints
app.UseFastEndpoints(config =>
{
    config.Endpoints.RoutePrefix = "api";
    config.Versioning.Prefix = "v";
    config.Versioning.PrependToRoute = true;
    config.Versioning.DefaultVersion = 1;
    config.Serializer.Options.Converters.Add(new JsonStringEnumConverter());
    config.Serializer.Options.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
    config.Serializer.Options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    config.Endpoints.IgnoreIsNotEndpoints();
});

//Use as minimal API if you wish
app.MapPost("/createEmployee", async ([FromServices]ISender sender, [FromBody]CreateEmployee command) =>
{
    var result = await sender.Send(command);
    return Results.Ok(result);
}).WithSummary("Using MediatR command");


app.UseSwaggerGen(default, config =>
{
    config.ValidateSpecification = true;
    config.DocExpansion = "list";
    config.DefaultModelExpandDepth = -1;
});


app.Run();


Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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
2.0.1 72 4/19/2024
2.0.0 55 4/19/2024
1.0.0 60 4/19/2024