MFA.Result 8.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package MFA.Result --version 8.0.2
NuGet\Install-Package MFA.Result -Version 8.0.2
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="MFA.Result" Version="8.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MFA.Result --version 8.0.2
#r "nuget: MFA.Result, 8.0.2"
#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.
// Install MFA.Result as a Cake Addin
#addin nuget:?package=MFA.Result&version=8.0.2

// Install MFA.Result as a Cake Tool
#tool nuget:?package=MFA.Result&version=8.0.2

MFA.Result NuGet Package

Overview

MFA.Result is a lightweight, versatile .NET library designed to simplify error handling and response management in your applications. It provides a robust way to encapsulate the results of operations, including success or failure information along with data and error messages. This library is ideal for APIs, web applications, and any .NET project that requires clear and concise result management.

Features

  • Generic Result<T> class to handle operation outcomes with success or failure states.
  • Support for HTTP status codes to align with RESTful API response patterns.
  • Implicit conversion operators for easy construction of Result objects.
  • Optional error messages list for detailed failure information.

Installation

To install MFA.Result, use the NuGet Package Manager console:

Installation of MFA.Result can be done using several methods as described below. Choose the one that best fits your development environment and workflow.

Using NuGet Package Manager Console

To install MFA.Result via the NuGet Package Manager Console in Visual Studio, use the following command:

Install-Package MFA.Result -Version 8.0.1

Using NuGet CLI

If you prefer using the command line, NuGet CLI can be used to install MFA.Result. First, ensure you have the NuGet CLI installed, then run the following command:

nuget install MFA.Result -Version 8.0.1

Using Visual Studio's NuGet Package Manager GUI

For those who prefer a graphical interface within Visual Studio:

  1. Open your solution in Visual Studio.
  2. Right-click on your project in the Solution Explorer and select Manage NuGet Packages.
  3. Go to the Browse tab, search for MFA.Result, select it, then press Install.
  4. Choose the version you wish to install if prompted.

These methods provide flexibility in how you can add MFA.Result to your projects, depending on your preference for tooling and the environment.

Usage

Below are some examples of how to use the MFA.Result library in your projects.

Successful Operation (Implicit Conversion)

public Result<string> GetUserName(int userId)
{
    // Your logic here
    return "John Doe"; // Implicitly converts to a successful Result
}

Successful Operation (Using Succeed Method)

public Result<string> GetUserNameWithSucceed(int userId)
{
    // Instead of relying on implicit conversion, directly use the Succeed method for clarity
    return Result<string>.Succeed("John Doe");
}

Operation With Error (Implicit Conversion)

public Result<string> GetUserName(int userId)
{
    // Your logic here
    return (404, "User not found"); // StatusCode is now an integer, converts implicitly to a failure Result
}

Operation With Error (Using Failure Method)

public Result<string> GetUserNameWithFailure(int userId)
{
    // Explicitly use the Failure method for creating a failure Result with status code and error message
    return Result<string>.Failure(404, new List<string> {"User not found"});
}

Checking Operation Result

var result = GetUserName(1);
if (result.IsSuccessful)
{
    Console.WriteLine(result.Data);
}
else
{
    Console.WriteLine(result.ErrorMessages.FirstOrDefault());
}

Contributing

Contributions are welcome! If you have suggestions or want to improve MFA.Result, please feel free to fork the repository, make changes, and submit a pull request.

Release Notes

Version 8.0.1

  • Changed: StatusCode property type has been updated from HttpStatusCode to int.

Release Notes for Version 8.0.2

  • Added Succeed and Failure methods for clearer result creation. Upgrade recommended for improved functionality and code clarity.

License

MFA.Result is available under the MIT license. See the LICENSE file for more info.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
8.0.4 118 3/8/2024
8.0.3 95 3/7/2024
8.0.2 101 3/6/2024
8.0.1 88 2/28/2024
8.0.0 88 2/27/2024

Breaking Changes:
   - StatusCode property type has been changed from HttpStatusCode to int. Update your code to use integer comparison.