Simple.Result 1.0.1

Provides two lite-weight classes that can be used to check the success or failure of operations returned from methods.

Install-Package Simple.Result -Version 1.0.1
dotnet add package Simple.Result --version 1.0.1
<PackageReference Include="Simple.Result" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Simple.Result --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Simple.Result, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

Nuget Package

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.

Nuget Package

  • .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 History

Version Downloads Last updated
1.0.1 93 1/14/2021