mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 02:24:30 +08:00
feat(GFramework.Core.Functional.Result): 增强Result类的安全性和健壮性
- 在构造函数中添加强制不变式检查,确保失败状态必须携带非空异常 - 为Try方法添加空值验证,防止传入空委托导致异常 - 为Map方法添加空值验证,增强方法调用的安全性 - 为Bind方法添加空值验证,提升代码健壮性 - 重构Map和Bind方法结构,使其更清晰易读
This commit is contained in:
parent
4750910675
commit
c6024bf94a
@ -32,6 +32,10 @@ public readonly struct Result : IEquatable<Result>
|
||||
/// <param name="exception">失败时的异常信息</param>
|
||||
private Result(bool isSuccess, Exception? exception)
|
||||
{
|
||||
// 强制不变式:失败状态必须携带非空异常
|
||||
if (!isSuccess && exception is null)
|
||||
throw new ArgumentException("Failure Result must have a non-null exception.", nameof(exception));
|
||||
|
||||
_isSuccess = isSuccess;
|
||||
_exception = exception;
|
||||
}
|
||||
@ -177,6 +181,7 @@ public readonly struct Result : IEquatable<Result>
|
||||
[Pure]
|
||||
public static Result Try(Action action)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(action);
|
||||
try
|
||||
{
|
||||
action();
|
||||
@ -194,8 +199,11 @@ public readonly struct Result : IEquatable<Result>
|
||||
/// <typeparam name="B">映射后的目标类型</typeparam>
|
||||
/// <param name="func">用于转换值的函数</param>
|
||||
/// <returns>若当前为成功状态,返回包含转换后值的成功 Result;若为失败状态,返回保持原有错误的失败 Result</returns>
|
||||
public Result<B> Map<B>(Func<B> func) =>
|
||||
IsSuccess ? Result<B>.Success(func()) : Result<B>.Failure(_exception!);
|
||||
public Result<B> Map<B>(Func<B> func)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(func);
|
||||
return IsSuccess ? Result<B>.Success(func()) : Result<B>.Failure(_exception!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将当前 Result 绑定到一个返回 Result 的函数上
|
||||
@ -203,6 +211,9 @@ public readonly struct Result : IEquatable<Result>
|
||||
/// <typeparam name="B">Result 中值的类型</typeparam>
|
||||
/// <param name="func">返回 Result 的函数</param>
|
||||
/// <returns>若当前为成功状态,返回函数执行的结果;若为失败状态,返回保持原有错误的失败 Result</returns>
|
||||
public Result<B> Bind<B>(Func<Result<B>> func) =>
|
||||
IsSuccess ? func() : Result<B>.Failure(_exception!);
|
||||
public Result<B> Bind<B>(Func<Result<B>> func)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(func);
|
||||
return IsSuccess ? func() : Result<B>.Failure(_exception!);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user