I'm prototyping language-ext
and functional code style for a web server library to share between my projects. I really like binding of async
steps using LINQ query syntax but I have now encountered this compiler issue: language-ext Task of Either with multiple from clauses.
To implement the suggested solution in that link, I would need to refactor this method:
private static async Task<Either<RouteError, object>> DispatchRequest<T>(
IRequestDispatcher requestDispatcher,
T request)
where T : class, IRequest
{
var result = await requestDispatcher.Send(request);
return result.Match<Either<RouteError, object>>(
Some: x => x,
None: () => new NotFoundError(),
Fail: ex => new InternalServerError(ex));
}
where requestDispatcher is an implementation of:
public interface IRequestDispatcher
{
Task<OptionalResult<object>> Send<T>(T request) where T : IRequest;
}
What I need is a method with this signature instead, so that it can be more easily used in LINQ expressions:
private static EitherAsync<RouteError, object> DispatchRequest<T>(
IRequestDispatcher requestDispatcher,
T request)
where T : class, IRequest
{
// What should the implementation look like?
}
I can't figure out how to construct the method so that I can await
and then return Left or Right based on the result. Everything I tried so far also looks ugly compared to the nice and clean original implementation with a Task
return value.