Simple.Result
1.0.1
dotnet add package Simple.Result --version 1.0.1
NuGet\Install-Package Simple.Result -Version 1.0.1
<PackageReference Include="Simple.Result" Version="1.0.1" />
paket add Simple.Result --version 1.0.1
#r "nuget: Simple.Result, 1.0.1"
// Install Simple.Result as a Cake Addin
#addin nuget:?package=Simple.Result&version=1.0.1
// Install Simple.Result as a Cake Tool
#tool nuget:?package=Simple.Result&version=1.0.1
Simple.Result
This library is meant to provide a light-weight wrapper class that lets you quickly determine whether an action succeeded or failed.
The following console app was written using this nuget package (Simple.Result) and Newtonsoft.Json.
using Fcbc.Utility.Result;
using Newtonsoft.Json;
using System;
namespace ResultExample
{
class Program
{
public class UserData
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserData(string first, string last)
{
FirstName = first;
LastName = last;
}
}
public class Error
{
public string Message { get; set; }
public int Code { get; set; }
public Error(string message, int code)
{
Message = message;
Code = code;
}
}
public static Result<UserData, Error> TestIt(bool pass)
{
if (pass)
{
return new UserData("Chris", "Volpi");
}
else
{
return new Error("Well this is awkward...", 1002);
}
}
public static void MiniApp()
{
Console.WriteLine("Type pass for an example of a successful result object");
Console.WriteLine("Type fail for an example of a failure result object");
var command = Console.ReadLine();
if (command.Equals("pass", StringComparison.OrdinalIgnoreCase))
{
var result = TestIt(true);
Console.WriteLine(JsonConvert.SerializeObject(result, new JsonSerializerSettings { Formatting = Formatting.Indented }));
}
else if (command.Equals("fail", StringComparison.OrdinalIgnoreCase))
{
var result = TestIt(false);
Console.WriteLine(JsonConvert.SerializeObject(result, new JsonSerializerSettings { Formatting = Formatting.Indented }));
}
else
{
Console.WriteLine("Uh... WAT?");
}
}
static void Main(string[] args)
{
while (true)
{
MiniApp();
}
}
}
}
Console output for "pass"
{
"IsSuccess": true,
"Value": {
"FirstName": "Chris",
"LastName": "Volpi"
},
"Error": null
}
Console output for "fail"
{
"IsSuccess": false,
"Value": null,
"Error": {
"Message": "Well this is awkward...",
"Code": 1002
}
}
When a result is passed, IsSuccess
is true and Value
is not null.
When a result is failed, IsSuccess
is false and Error
is not null.
The above example makes use of the implicit operator
functionality in the Result class. You do not have to new up a result class if you have the success or failure object that you want to return.
The VoidResult class does not have an implicit operator for success responses; you must always return new VoidResult<ErrorClass>();
for success cases. This is because there is no way to implictly convert a void return into a type. Though, you can still return new ErrorClass();
instead of newing up the result wrapper.
Because this class uses generics, the success and failure types can be whatever you want them to be.
Let me know if you think anything could be improved here and do not be shy with PRs.
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 was computed. 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. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- 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.
Version | Downloads | Last updated |
---|---|---|
1.0.1 | 355 | 1/14/2021 |