Norbert Maziarz
Norbert Maziarz

Reputation: 21

Automatic registration of API enpoints in .Net Core Minimal Api

I have the base class that I’m using for internal communication in my app.

    public abstract class EndpointBase<TRequest, TResponse>
        where TRequest : class, IRequest
        where TResponse : class

    {
        public abstract bool IsPublic { get; }
        public abstract string Route { get; }
        public abstract Task<TResponse> PerformAction(TRequest request);
    }

E.g.:

    internal sealed class UserEnpoint : EndpointBase<UserRequest, UserDto>
    {
        public override string Route => "/users";
        public override bool IsPublic => true;

        public override async Task<UserDto> PerformAction(UserRequest request)
        {
           .... await someContext.Users
                    .Where(user => user.Id == request.Id)
                    .FirstOrDefault();

        }
    }

Now using Minimal Api methods I'd like to automatically register all IsPublic implementations of this base class as Api Endpoints.

    public static void UseEndpoints(this WebApplication app)
    {
         app.MapGet(...Route..., ...PerformAction...);
    }

Do you think it's easily possible? What would be the best way to do that?

Upvotes: 1

Views: 700

Answers (1)

davidfowl
davidfowl

Reputation: 38864

For this style, you should look at something like https://fast-endpoints.com/

Upvotes: 1

Related Questions