Reditus 2.0.0
See the version list below for details.
dotnet add package Reditus --version 2.0.0
NuGet\Install-Package Reditus -Version 2.0.0
<PackageReference Include="Reditus" Version="2.0.0" />
paket add Reditus --version 2.0.0
#r "nuget: Reditus, 2.0.0"
// Install Reditus as a Cake Addin #addin nuget:?package=Reditus&version=2.0.0 // Install Reditus as a Cake Tool #tool nuget:?package=Reditus&version=2.0.0
Reditus
Reditus, is a Result pattern library for every .NET application.
Getting Started
You can install Reditus with NuGet:
Install-Package Reditus
Features
- Versatile — Can be used in any .NET project.
- Immutable — Once a Result is created, it cannot be changed.
- Detailed on Failure — A Result, when failed, contains a specific IError class.
- Thread safe — Results are immutable and by nature, safe to work with in multithreaded scenarios.
- Extensible — Extend the Error class by introducing your very own Error classes.
- Fully tested — The code has full coverage.
Usage
Result objects can hold values or simply be used as flow state control. The values can be anything. A class, a value-type, a struct, anything.
Creating a Result
Typically, the Result
class is being used by methods that don't return a value.
var result = Result.Success();
var error = new Error("An error occured.");
var result = Result.Fail(error);
// the error can also hold an exception
var error = new Error("An error occured.", new Exception());
var result = Result.Fail(error);
An example usage of the Result
class.
public async Task<Result> ExecuteJob()
{
try
{
var jobId = ExecuteCleanupJob();
if (jobId == 0)
{
// create an Error indicating the reason of failure
var error = new Error("Cleanup job was not executed.");
return Result.Fail(error);
}
return Result.Success();
}
catch (Exception ex)
{
// create an Error and attach the exception
var error = new Error("An unexpected error occured while trying execute Cleanup job.", ex);
return Result.Fail(error);
}
}
The Result<T>
class is being used by methods that return a value.
var result = Result<int>.Success(1);
var error = new Error("An error occured.");
var result = Result<int>.Fail(error);
// the error can also hold an exception
var error = new Error("An error occured.", new Exception());
var result = Result<int>.Fail(error);
An example usage of the Result<T>
class.
public async Task<Result<int>> ExecuteJob()
{
try
{
var jobId = ExecuteCleanupJob();
if (jobId == 0)
{
// create an Error indicating the reason of failure
var error = new Error("Cleanup job was not executed.");
return Result<int>.Fail(error);
}
return Result<int>.Success(jobId);
}
catch (Exception ex)
{
// create an Error and attach the exception
var error = new Error("An unexpected error occured while trying execute Cleanup job.", ex);
return Result<int>.Fail(error);
}
}
The anatomy of a Result
A Result
holds certain information about itself.
var sucessfulResult = Result.Successful();
sucessfulResult.IsSuccessful // true
sucessfulResult.IsFailed // false
sucessfulResult.Error // throws InvalidOperationException as the result is not in a failed state
var failedResult = Result.Failed(new Error("Operation failed.");
failedResult.IsSuccessful // false
failedResult.IsFailed // true
failedResult.Error // IError instance
When the Result<T>
holds a return value.
var sucessfulResult = Result<int>.Successful(1);
sucessfulResult.IsSuccessful // true
sucessfulResult.IsFailed // false
sucessfulResult.Value // 1
sucessfulResult.Error // throws InvalidOperationException as the result is not in a fail state
var failedResult = Result<int>.Failed(new Error("Operation failed.");
failedResult.IsSuccessful // false
failedResult.IsFailed // true
failedResult.Value // throws InvalidOperationException as the result is not in a success state
failedResult.Error // IError instance
Extending
You can introduce your very own Error classes by extending the existing one.
The below custom NotFoundError
class is being used when an application might need to return a NotFound 404 response.
public interface IHttpError : IError
{
public HttpStatusCode HttpStatusCode { get; }
}
public sealed class NotFoundError : Error, IHttpError
{
public HttpStatusCode HttpStatusCode => HttpStatusCode.NotFound;
public NotFoundError(string message = "The request resource was not found.")
: base(message)
{
}
}
An example of the above custom Error
class.
public async Task<Result<IEnumerable<Project>>> GetProjects()
{
try
{
var projects = await GetProjects();
if (!projects.Any())
{
var error = new NotFoundError(); // <-- the new NotFoundError Error class
return Result<IEnumerable<Project>>.Fail(error);
}
return Result<IEnumerable<Project>>.Success(jobId);
}
catch (Exception ex)
{
// create an Error and attach the exception
var error = new Error("An unexpected error occured while trying execute Cleanup job.", ex);
return Result<IEnumerable<Project>>.Fail(error);
}
}
You can also introduce simple Error
classes to better reflect your domain.
public sealed class ApplicationError : Error
{
public ApplicationError(string eventId, string message, Exception exceptio)
: base(message, exception)
{
}
}
The Error
class provides 2 constructors, so you are free to use whichever suits your needs
best. See definition
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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Reditus:
Package | Downloads |
---|---|
Reditus.Benchmarks
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.