Tolitech.Results
1.0.0-alpha06
See the version list below for details.
dotnet add package Tolitech.Results --version 1.0.0-alpha06
NuGet\Install-Package Tolitech.Results -Version 1.0.0-alpha06
<PackageReference Include="Tolitech.Results" Version="1.0.0-alpha06" />
paket add Tolitech.Results --version 1.0.0-alpha06
#r "nuget: Tolitech.Results, 1.0.0-alpha06"
// Install Tolitech.Results as a Cake Addin #addin nuget:?package=Tolitech.Results&version=1.0.0-alpha06&prerelease // Install Tolitech.Results as a Cake Tool #tool nuget:?package=Tolitech.Results&version=1.0.0-alpha06&prerelease
Results
The Results pattern is a design approach used for handling errors and outcomes in a structured manner. Instead of relying solely on exceptions, this pattern involves returning an object that encapsulates both the result and any potential errors that may have occurred during the operation.
Overview
The Result
object represents the result of an operation with optional metadata and messages. It is designed for handling errors and outcomes in a structured manner, providing information about the operation's status and associated details.
Properties
Title
: Gets the title metadata associated with the result.Detail
: Gets the detail metadata associated with the result.StatusCode
: Gets the status code associated with the result.IsSuccess
: Gets a value indicating whether the result indicates success.IsFailure
: Gets a value indicating whether the result indicates failure.Messages
: Gets a collection of message results associated with the result.
Factory Methods
Success
OK()
: Represents a successful result.OK<T>(T value)
: Represents a successful result with a typed value.OK<T>()
: Represents a successful result with the default value for typeT
.
Created
Created()
: Represents a result indicating successful creation.Created<T>(T value)
: Represents a result indicating successful creation with a typed value.Created<T>()
: Represents a result indicating successful creation with the default value for typeT
.
No Content
NoContent()
: Represents a result indicating no content.NoContent<T>()
: Represents a result indicating no content with the default value for typeT
.
Bad Request
BadRequest()
: Represents a result indicating a bad request.BadRequest<T>(T value)
: Represents a result indicating a bad request with a typed value.BadRequest<T>()
: Represents a result indicating a bad request with the default value for typeT
.
Forbidden
Forbidden()
: Represents a result indicating forbidden access.Forbidden<T>(T value)
: Represents a result indicating forbidden access with a typed value.Forbidden<T>()
: Represents a result indicating forbidden access with the default value for typeT
.
Not Found
NotFound()
: Represents a result indicating a resource not found.NotFound<T>(T value)
: Represents a result indicating a resource not found with a typed value.NotFound<T>()
: Represents a result indicating a resource not found with the default value for typeT
.
Internal Server Error
InternalServerError()
: Represents a result indicating an internal server error.InternalServerError<T>(T value)
: Represents a result indicating an internal server error with a typed value.InternalServerError<T>()
: Represents a result indicating an internal server error with the default value for typeT
.
Methods
Message Handling
AddInformation(string message)
: Adds an informational message to the result.AddInformation(string? key, string message)
: Adds an informational message to the result with an optional key.AddWarning(string message)
: Adds a warning message to the result.AddWarning(string? key, string message)
: Adds a warning message to the result with an optional key.AddError(string message, StatusCode statusCode = StatusCode.BadRequest, Exception? exception = null)
: Adds an error message to the result with optional status code and exception.AddError(string? key, string message, StatusCode statusCode = StatusCode.BadRequest, Exception? exception = null)
: Adds an error message to the result with optional key, status code, and exception.
Example
Consider a scenario where we have an operation to divide two numbers. We can use the Results pattern to handle the outcome of this operation:
// Example implementation of a class using the Results pattern
public class MathOperation
{
public Result<int> Divide(int dividend, int divisor)
{
try
{
if (divisor == 0)
{
// Handling division by zero
return Result.BadRequest<int>()
.WithTitle("Cannot divide by zero");
}
int result = dividend / divisor;
// Returning a successful result with the quotient
return Result.OK(result);
}
catch (Exception ex)
{
// Handling other exceptions
return Result.InternalServerError<int>()
.WithTitle(ex.Message)
.WithDetail(ex.ToString());
}
}
}
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. |
-
net8.0
- No dependencies.
NuGet packages (6)
Showing the top 5 NuGet packages that depend on Tolitech.Results:
Package | Downloads |
---|---|
Tolitech.CleanArchitecture.Presentation
The Presentation repository is dedicated to providing interfaces and implementations for endpoint definition and registration within an application following the principles of Clean Architecture. With these tools, the configuration of routes and endpoints is simplified, promoting organization and clarity in the presentation layer of the architecture. |
|
Tolitech.CleanArchitecture.Application
The Application repository provides the Unit of Work interface within the Clean Architecture context. |
|
Tolitech.Results.Guards
Results.Guards is a utility library that provides fluent and expressive guard clauses for result-oriented programming. |
|
Tolitech.Results.Http
This package provides extension methods for handling HTTP responses in .NET applications, facilitating the extraction and mapping of relevant data from HttpResponseMessage objects into Result objects. |
|
Tolitech.CleanArchitecture.Application.Validation
The ValidationBehavior class represents a behavior in the pipeline responsible for validating requests using the provided validators before passing them to the next handler. It is designed to be used within applications built on the MediatR library in .NET. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-alpha13 | 55 | 9/30/2024 |
1.0.0-alpha12 | 85 | 7/28/2024 |
1.0.0-alpha11 | 72 | 7/8/2024 |
1.0.0-alpha10 | 142 | 3/4/2024 |
1.0.0-alpha09 | 78 | 2/6/2024 |
1.0.0-alpha08 | 68 | 2/6/2024 |
1.0.0-alpha07 | 89 | 1/22/2024 |
1.0.0-alpha06 | 68 | 1/16/2024 |
1.0.0-alpha05 | 77 | 12/16/2023 |
1.0.0-alpha04 | 72 | 12/11/2023 |
1.0.0-alpha03 | 74 | 12/8/2023 |
1.0.0-alpha02 | 98 | 12/3/2023 |
1.0.0-alpha01 | 77 | 12/1/2023 |
Inclusion of the generic option WithType.