Result.Simplified
2.1.0
dotnet add package Result.Simplified --version 2.1.0
NuGet\Install-Package Result.Simplified -Version 2.1.0
<PackageReference Include="Result.Simplified" Version="2.1.0" />
<PackageVersion Include="Result.Simplified" Version="2.1.0" />
<PackageReference Include="Result.Simplified" />
paket add Result.Simplified --version 2.1.0
#r "nuget: Result.Simplified, 2.1.0"
#:package Result.Simplified@2.1.0
#addin nuget:?package=Result.Simplified&version=2.1.0
#tool nuget:?package=Result.Simplified&version=2.1.0
Result.Simplified
Result.Simplified enables dot net methods to return an indication of success or failure, for any method return type (including void). It shouldn't be used instead of exceptions, but rather to enable a method to return a failure indication in non-exceptional circumstances.
Use Result
to enable void methods to return an indication of success or failure,
and Result<T>
to enable non-void methods to do the same.
Both Result
and Result<T>
overload the &
and |
operators as well as the true
and false
operators,
meaning you can easily combine results in a short-circuit manner for easy validations.
Usage example:
Return a Result
from a method:
Result DoSomething()
{
// some code...
return condition
? Result.Success()
: Result.Fail("Something went wrong...");
}
or
Result DoSomething()
{
// some code...
return Result.SuccessIf(condition, "Something went wrong...");
}
or
Result DoSomething()
{
// some code...
return Result.FailIf(negativeCondition, "Something went wrong...");
}
Return a Result<T>
from a method:
Result<int> DoSomethingAndReturnAnInt()
{
// some code...
return condition
? Result<int>.Success(5)
: Result<int>.Fail("Something went wrong...");
}
or
Result<int> DoSomethingAndReturnAnInt()
{
// some code...
return Result<int>.SuccessIf(condition, 5, "Something went wrong...", false);
// The boolean at the end determines whether to include the value in the failed result.
}
or
Result<int> DoSomethingAndReturnAnInt()
{
// some code...
return Result<int>.FailIf(negativeCondition, 5, "Something went wrong...", true);
// The boolean at the end determines whether to include the value in the failed result.
}
Consume a method that returns a Result
void DoIfMethodSucceeded()
{
var result = DoSomething();
if(!result.IsSuccess)
{
// Something went wrong, do something with result.ErrorDescription
// log or show the user or whatever
return;
}
// Everything is fine, you can go on with your code
}
Consume a method that returns a Result<T>
bool DoIfMethodSucceeded()
{
var result = DoSomethingAndReturnAnInt();
if(!result.IsSuccess)
{
// Something went wrong, do something with result.ErrorDescription
// Log or show the user or whatever
return false;
}
var intValue = result.Value;
// Everything is fine, you can go on with your code
}
Note: Since Result
and Result<T>
overloads the true
and false
operators,
you don't technically have to use the IsSuccess
property to check if the result is a success or not,
but I do recommend it for readability.
void DoIfMethodSucceeded()
{
var result = DoSomething();
if(!result)
{
// Something went wrong, do something with result.ErrorDescription
// log or show the user or whatever
return;
}
// Everything is fine, you can go on with your code
}
Since Result
and Result<T>
overloads the &
and |
operators as well,
you can combine multiple results in a short-circuit manner.
Result FailFast()
{
var result = DoSomething() // returns a result instance
&& DoSomethingElse() // returns another result instance
&& DoAnotherThing() // returns another result instance;
// result is the first failed result, or the last one if all succeeded.
return result;
}
Result SuccessIfAny()
{
var result = DoSomething() // returns a result instance
|| DoSomethingElse() // returns another result instance
|| DoAnotherThing() // returns another result instance;
// result is the first successful result, or the last one if all failed.
return result;
}
You can chain result objects for validation:
Result<SomeObject> Validate(SomeObject someObject)
{
return Validate("someObject is null.", d => d is object)
&& Validate("someObject has no SomeProperty.", d => d.SomeProperty is object)
&& Validate("SomeProperty is invalid.", d => d.SomeProperty.IsValid)
&& Validate("SomeCollection is empty.", d => (d.SomeCollection?.Count ?? 0) > 0);
Result<SomeObject> Validate(string errorMessage, Predicate<SomeObject> predicate)
{
var isValid = predicate(someObject);
if (!isValid)
{
// Optionally log non-exceptional error here...
}
return isValid
? Result<SomeObject>.Success(someObject)
: Result<SomeObject>.Fail(errorMessage);
}
}
Product | Versions 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 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. |
.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 was computed. 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. |
-
.NETStandard 2.0
- No dependencies.
-
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.
Added input validations.
Improved XML documentation.