SResult 1.4.0
dotnet add package SResult --version 1.4.0
NuGet\Install-Package SResult -Version 1.4.0
<PackageReference Include="SResult" Version="1.4.0" />
<PackageVersion Include="SResult" Version="1.4.0" />
<PackageReference Include="SResult" />
paket add SResult --version 1.4.0
#r "nuget: SResult, 1.4.0"
#:package SResult@1.4.0
#addin nuget:?package=SResult&version=1.4.0
#tool nuget:?package=SResult&version=1.4.0
SResult
This is an "as simple as it could" and a pure Result pattern library.
Why another result pattern?
- Just a boiler plate basic codes made as library for everyday use. No mvc or any bulk.
- It has to have a Result when success.
- It has to have a Failure when fail.
- It impossible to be both or neither.
Structure
Result<TValue>For simplest use. It returns built inReasoninstance when failed orTValuewhen succeeded.
How to return a Result from a function in most basic way.
public static Result<int> TryParseToInteger(string value)
{
if (int.TryParse(value, out var val))
{
return val;
}
else
{
return (Failure)"The value is not any integer.";
}
}
How to read a Result
In continuation of the example above
var result = TryParseToInteger("101");
if(result.IsSuccess(out var value, out var failure)) Console.WriteLine($"The integer value is {value}");
else Console.WriteLine(failure.Message);
The usual approach is to throw an exception when trying to access the Value when it Failed. But that brings additional concern on how the code is written and a proper exception handling. But here we will have to check for Success or Failure to gain access to the Value or the Failure. This is to reduce the concern discussed above.
Do something before returning
public bool Handler()
{
var result = TryParseToInteger("125")
.OnSuccess((value) => { /* Do something on success with value */ })
.OnFailure((failure) => { /* Do something on failure with failure */ });
return result.IsSuccess();
}
Built in Failure Type
Initially this was not part of the library. But I end up making basic Failure class for all projects which is same 95% of the time. it usually has a string message and a failure type sort of thing. So I thought to make a part of it. So those 95% cases are covered. And free to use custom type when required.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.