MiF.Result
1.1.0
dotnet add package MiF.Result --version 1.1.0
NuGet\Install-Package MiF.Result -Version 1.1.0
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="MiF.Result" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MiF.Result" Version="1.1.0" />
<PackageReference Include="MiF.Result" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MiF.Result --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MiF.Result, 1.1.0"
#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.
#addin nuget:?package=MiF.Result&version=1.1.0
#tool nuget:?package=MiF.Result&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MiF.Result
MiF.Result is a lightweight .NET/C# library implementing the Result pattern. This pattern is commonly used to represent the outcome of an operation, encapsulating either a success or an error state. It simplifies error handling and improves code readability by avoiding exceptions for control flow.
Features
- Represent success or failure of operations.
- Encapsulate error details with custom error types.
- Provide utility methods for error handling and type checking.
Installation
To use MiF.Result in your project, add it as a dependency via NuGet:
dotnet add package MiF.Result
Usage
Basic Success and Failure
using MiF.Result;
// Represent a successful operation var success
Result = Result.Success();
// Represent a failed operation with an error message
var failureResult = Result.Fail("An unexpected error occurred.");
// Represent a failed operation with an error code and message
var failureWithCodeResult = Result.Fail("NOTFOUN", "Item not found.");
Custom Error Handling
using MiF.Result;
using MiF.Result.Interfaces;
// Define a custom error type
public class ValidationError : IError
{
public string Code { get; }
public string Message { get; }
public ValidationError(string code, string message)
{
Code = code;
Message = message;
}
}
// Create a result with a custom error
var validationError = new ValidationError("INVALID_INPUT", "The input value is invalid.");
var result = Result.Fail(validationError);
// Check the error type
if (result.IsErrorType<ValidationError>())
{
var error = result.GetError<ValidationError>();
Console.WriteLine($"Error Code: {error.Code}, Message: {error.Message}");
}
// Or try to get the error if it is of the expected type
var errorResult = result.TryGetError<ValidationError>(out var validationError);
License
This project is licensed under the MIT License. See the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.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.