-
Please excuse my bad English. private static readonly Error InternalError = Error.New(500, "Something went wrong");
public async Task<IResult> FindCompanyById(string id, CancellationToken cancellationToken)
{
var io =
from entity in DB.FinCompanyById(id)
| Prelude.@catch(IO<CompanyEntity>.Fail(InternalError))
from dto in entity.MapToDto()
select dto.ToHttpResult();
var fin = await io.RunSafeAsync(); // <- How to pass EnvIO with method's CancellationToken
return fin.IfFail(e => e.ToHttpResult());
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The public static ValueTask<Fin<A>> RunSafeAsync<A>(this K<IO, A> ma, EnvIO? envIO = null) =>
ma.As().Try().Run().RunAsync(envIO); So you can provide your own with your own using var envIO = EnvIO.New(cancellationToken);
var fin = await io.RunSafeAsync(envIO) By the way, not sure if this just example code, but I would separate out your public IO<CompanyDTO> FindCompanyById(string id) =>
from entity in DB.FinCompanyById(id)
| @catch(IO<CompanyEntity>.Fail(InternalError))
from dto in entity.MapToDto()
select dto; And create an extension to do the 'messy stuff': public static async Task<IResult> RunToHttp(this IO<A> io, CancellationToken token)
{
using var envIO = EnvIO.New(token);
var fin = await io.RunSafeAsync(envIO);
return fin.IfFail(e => e.ToHttpResult());
} Then you can use the FindCompanyById(id).RunToHttp(token); Which keeps the invocation behaviour separate from your lovely pure IO code. Which is good for separation of concerns, but it also means you can compose your Also, use: using static LanguageExt.Prelude; That means you don't need the |
Beta Was this translation helpful? Give feedback.
The
EnvIO
is optional:So you can provide your own with your own
CancellationToken
:By the way, not sure if this just example code, but I would separate out your
IO
functionality:And create an extension to do the 'messy stuff':