namespace MS.Microservice.Core.Functional
{
///
/// Exceptional 的辅助工厂与异步包装能力。
///
public static class ExceptionalExtensions
{
public static Exceptional Try(Func operation)
{
try
{
return F.Success(operation());
}
catch (Exception ex)
{
return F.ExceptionThrown(ex);
}
}
public static Exceptional Try(Action operation)
=> Try(() =>
{
operation();
return Unit.Default;
});
public static async Task> TryAsync(Func> operation)
{
try
{
return F.Success(await operation());
}
catch (Exception ex)
{
return F.ExceptionThrown(ex);
}
}
public static Task> TryAsync(Func operation)
=> TryAsync(async () =>
{
await operation();
return Unit.Default;
});
}
}