I am trying to integrate fast-endpoints with Results (nuget: LanguageExt.Core) for the purpose of moving away from throw-based exception validation. My issue is that while the lib allows me to do something like
public class MyEndpoint : Endpoint<MyRequest,Results<Ok<MyResponse>, NotFound,ProblemDetails>>
this forces me to if-else on every endpoint to output the different return values. I am more interested in doing something like
public class MyEndpoint : Endpoint<MyRequest, Result<MyResponse>>
and i would then have a global post-processor to handle all faulted responses (which would be exceptions carried over from the domain, etc) and leave the endpoint to handle the happy path.
My technical hurdle with this is that, inside the post-processor handler i don't know how i would get a hold of this Result object. from the signature of the post-processor handler
public Task PostProcessAsync(IPostProcessorContext context, CancellationToken ct)
the context has a Response property but its object?
so i dont know how to proceed.
i haven't used the results pattern from
LanguageExt.Core
. but when usingArdalis.Result
package, we use an extension method for sending the response as shown below. it then becomes the responsibility of theSendResponseAsync()
method to check the result to see if it's in a faulted state or not and send the appropriate happy or sad response. the endpoint handler code is free fromif-else
code this way.