4q-dev.ResultSharp
1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package 4q-dev.ResultSharp --version 1.0.2
NuGet\Install-Package 4q-dev.ResultSharp -Version 1.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="4q-dev.ResultSharp" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add 4q-dev.ResultSharp --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: 4q-dev.ResultSharp, 1.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 4q-dev.ResultSharp as a Cake Addin #addin nuget:?package=4q-dev.ResultSharp&version=1.0.2 // Install 4q-dev.ResultSharp as a Cake Tool #tool nuget:?package=4q-dev.ResultSharp&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Repository:
https://github.com/4q-dev/ResultSharp/
Documentation
Full documentation is available here: ResultSharp Docs
Features
- Convenient representation of successful (
Success
) and failed (Failure
) results - Composition and transformation of results using functional methods (
Map
,Then
,Match
, etc.) - Eliminates the need to use
try-catch
in business logic - Support for asynchronous operations
- Support for logging: Microsoft.Extensions.Logging, Serilog, or any other custom adapter
Quick Start
Installation
dotnet add package 4q-dev.ResultSharp
Basic usage
using ResultSharp;
using ResultSharp.Errors;
using ResultSharp.Extensions.FunctionalExtensions.Sync;
Result<int> ParseNumber(string input)
{
return int.TryParse(input, out var number)
? number
: Error.Failure("Invalid number");
}
int result = ParseNumber("42")
.Map(n => n * 2)
.Match(
ok => Console.Write($"Success: {ok}"), // output: Success: 84
error => Console.Write($"Error: {error}")
)
.UnwrapOrDefault(@default: 0);
Console.WriteLine(result); // 84
Example Usage
Without using Result
:
var user = userRepository.Get();
if (user is null)
{
logger.LogMessage("User not found");
throw new Exception("User not found");
}
if (user.Email.IsConfirmed is false)
{
logger.LogMessage("Email address must be confirmed before sending notifications.");
throw new Exception("Email address must be confirmed before sending notifications.");
}
try
{
emailNotificationService.Notify(user.Email, "some notification message");
}
catch (Exception ex)
{
Logger.LogMessage("Error message: {ex}", ex.Message);
throw ex;
}
Using ResultSharp
:
return userRepository.Get()
.Ensure(user => user.Email.IsConfirmed, onFailure: Error.Unauthorized("Email address must be confirmed before sending notifications."))
.Then(user => emailNotificationService.Notify(user.Email, "some notification message"))
.LogIfFailure();
Contribution
We welcome contributions to the development of this library! To make changes:
- Fork the repository
- Create a new branch (
git checkout -b feature-branch
) - Make changes and commit them (
git commit -m 'Added new feature'
) - Push the changes (
git push origin feature-branch
) - Open a Pull Request
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 | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on 4q-dev.ResultSharp:
Package | Downloads |
---|---|
4q-dev.ResultSharp.Logging.Serilog
Библиотека Result паттерна с функциональными методами |
|
4q-dev.ResultSharp.Logging.MicrosoftLogger
Библиотека Result паттерна с функциональными методами |
GitHub repositories
This package is not used by any popular GitHub repositories.