Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Consider using FastEnpoints over Minimal API #26

Open
GFoley83 opened this issue Jan 2, 2025 · 0 comments
Open

[FEATURE] Consider using FastEnpoints over Minimal API #26

GFoley83 opened this issue Jan 2, 2025 · 0 comments

Comments

@GFoley83
Copy link

GFoley83 commented Jan 2, 2025

Would greatly simplify the endpoint registration boilerplate (e.g. https://github.com/mehdihadeli/vertical-slice-api-template/blob/main/src/App/Vertical.Slice.Template/Products/Features/CreatingProduct/v1/CreateProductEndpoint.cs).
It's also built on top of minimal APIs and has built in support for other tools e.g. FluentValidation.

public class MyEndpoint : Endpoint<MyRequest, MyResponse>
{
    private ISender _mediator;

    public MyEndpoint(ISender mediator)
    {
        _mediator = mediator;
    }

    public override void Configure()
    {
        Post("/api/user/create");
        AllowAnonymous();
    }

    public override async Task HandleAsync(MyRequest req, CancellationToken ct)
    {
        Response = await _mediator.Send(req, ct);
    }

    public class Handler(ISomeService someService) : IRequestHandler<MyRequest, MyResponse>
    {
        public async Task<MyResponse> Handle(MyRequest request, CancellationToken ct)
        {
            // Do stuff
            return new MyResponse();
        }
    }
}

This is a contrived example of course as you could have your validation in here, mappings etc.

Counter point, the MediatR author has mentioned that there's no need to use both libraries (as covered here) which I don't personally agree with given the MediatR pipeline gives you a middleware that works with any .NET project eg Razor, Console, Azure Funcs, Lambdas etc. and has always been the most useful part of the library IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant