Calling an async method OnSuccess
Is it possible to call async methods, or do I need to use Result or Wait()?
It should work with async operations too. Check out examples here: https://github.com/vkhorikov/CSharpFunctionalExtensions/blob/master/CSharpFunctionalExtensions.Examples/ResultExtensions/AsyncUsageExamples.cs
I've be playing around with it now, and I think I've found an issue, or I'm not using it right. My code looks like the following:
return Result.Combine(name, address)
.OnSuccess(async () =>
{
try
{
return Result.Ok(await eventstore.GetAsync<User>(message.UserId));
}
catch (AggregateNotFoundException)
{
return Result.Fail<User>("Aggregate not found");
}
})
.OnSuccess(user => user.UpdateAddress(address.Value))
.OnSuccess(async user =>
{
var saveResult = await eventStore.SaveAsync(user);
return Result.Ok(new CommandResult2(userId.ToString(), saveResult.StreamPosition, saveResult.AggregateVersion));
})
The first OnSuccess is using public static Result<T> OnSuccess<T>(this Result result, Func<T> func); instead of public static Task<Result<T>> OnSuccess<T>(this Result result, Func<Task<Result<T>>> func, bool continueOnCapturedContext = false);
I've found that if I change the first OnSuccess to OnSuccess<User> it works.