ardalis / Result

A result abstraction that can be mapped to HTTP response codes if needed.
MIT License
847 stars 100 forks source link

Consider New Ardalis.Result.FastEndpoints package with base classes, extension methods #175

Open ardalis opened 5 months ago

ardalis commented 5 months ago

For example this code from a devBetter.com member:

public abstract class EndpointBase<TRequest, TResponse, TMapper> : Endpoint<TRequest, IResult, TMapper> where TRequest : notnull where TResponse : notnull where TMapper : class, IResponseMapper
{
    public override async Task<IResult> ExecuteAsync(TRequest req, CancellationToken ct)
    {
        Result<TResponse> response = await ProcessAsync(req, ct)
            .ConfigureAwait(false);

        this.AddResultErrors(response);

        ThrowIfAnyErrors();

        return response
            .ToMinimalApiResult();
    }

    public abstract Task<Result<TResponse>> ProcessAsync(TRequest request, CancellationToken cancellationToken);
}

// or similar by nhwilly
public abstract class EndpointBase<TRequest, TResponse> : Endpoint<TRequest, Microsoft.AspNetCore.Http.IResult> where TRequest : notnull where TResponse : notnull
{
  public override async Task<Microsoft.AspNetCore.Http.IResult> ExecuteAsync(TRequest req, CancellationToken ct)
  {
    Result<TResponse> response = await ProcessAsync(req, ct)
        .ConfigureAwait(false);

    this.AddResultErrors(response);

    ThrowIfAnyErrors();

    return response.ToMinimalApiResult();
  }

  public abstract Task<Result<TResponse>> ProcessAsync(TRequest request, CancellationToken cancellationToken);
}
KyleMcMaster commented 5 months ago

See: #157

nhwilly commented 5 months ago

I tried that, but quickly ran into limitations for 201 and we'd have to subclass other varieties, for no dto returned, etc. I backed off until I have more time later.

See #174