Linger.Result 0.4.0-alpha

This is a prerelease version of Linger.Result.
dotnet add package Linger.Result --version 0.4.0-alpha
                    
NuGet\Install-Package Linger.Result -Version 0.4.0-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Linger.Result" Version="0.4.0-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Linger.Result" Version="0.4.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="Linger.Result" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Linger.Result --version 0.4.0-alpha
                    
#r "nuget: Linger.Result, 0.4.0-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Linger.Result&version=0.4.0-alpha&prerelease
                    
Install Linger.Result as a Cake Addin
#tool nuget:?package=Linger.Result&version=0.4.0-alpha&prerelease
                    
Install Linger.Result as a Cake Tool

Linger.Result

Linger.Result是一个轻量级库,为.NET应用程序提供了一种优雅的方式来处理操作结果,避免异常传播和空引用问题。

特性

  • 明确区分成功和失败结果
  • 支持带有强类型返回值的结果
  • 友好的错误处理机制
  • 支持多种结果状态:成功、错误、未找到等
  • 链式操作支持

安装

dotnet add package Linger.Result

基本用法

创建成功结果

// 不带值的成功结果
Result successResult = Result.Success();

// 带值的成功结果
Result<int> successWithValue = Result.Success(42);

创建失败结果

// 不带值的失败结果
Result failureResult = Result.Failure();
Result failureWithMessage = Result.Failure("操作失败");
Result failureWithError = Result.Failure(new Error("ERR001", "发生了错误"));

// 带值类型的失败结果
Result<int> typedFailure = Result<int>.Failure("数值计算失败");

创建"未找到"结果

Result notFoundResult = Result.NotFound();
Result<User> userNotFound = Result<User>.NotFound("未找到指定用户");

条件性创建结果

// 基于条件创建结果
Result conditionResult = Result.Create(age >= 18);

// 基于非空检查创建结果
Result<string> nullCheckResult = Result.Create(possiblyNullString);

处理结果

// 检查结果状态
if (result.IsSuccess)
{
    // 处理成功情况
}

// 对于带值结果,访问值
if (userResult.IsSuccess)
{
    User user = userResult.Value;
    // 使用user对象
}

// 使用模式匹配处理结果
int displayValue = userResult.Match(
    onSuccess: user => user.Score,
    onFailure: errors => -1
);

// 使用TryGetValue模式
if (userResult.TryGetValue(out User? user))
{
    // 使用user对象
}

// 使用ValueOrDefault获取值或默认值
User? userOrNull = userResult.ValueOrDefault;

// 指定自定义默认值
User defaultUser = new User("guest");
User userOrDefault = userResult.GetValueOrDefault(defaultUser);

进阶用法

错误处理

// 访问错误列表
if (result.IsFailure)
{
    foreach (var error in result.Errors)
    {
        Console.WriteLine($"错误码: {error.Code}, 消息: {error.Message}");
    }
}

常见错误代码

  • Error.None - 表示没有错误
  • Error.Default - 默认错误,不具体指定
  • Error.NullValue - 指定的结果值为空
  • Error.ConditionNotMet - 指定的条件未满足
  • Error.NotFound - 服务无法找到请求的资源

贡献

欢迎提交问题报告和Pull Request!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Linger.Result:

Package Downloads
Linger.Result.AspNetCore

C# Helper Library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.0-alpha 118 4/1/2025
0.3.3-alpha 119 3/19/2025
0.3.2-alpha 116 3/17/2025
0.3.1-alpha 105 3/16/2025
0.3.0-alpha 182 3/6/2025
0.1.2-alpha 70 12/17/2024
0.1.1-alpha 62 12/17/2024
0.1.0-alpha 60 12/6/2024
0.0.2-alpha 62 10/3/2024
0.0.1-alpha 67 8/8/2024