Tolitech.Results 1.0.0-alpha11

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

// Install Tolitech.Results as a Cake Tool
#tool nuget:?package=Tolitech.Results&version=1.0.0-alpha11&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 type T.

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 type T.

No Content

  • NoContent(): Represents a result indicating no content.
  • NoContent<T>(): Represents a result indicating no content with the default value for type T.

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 type T.

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 type T.

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 type T.

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 type T.

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 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 (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 58 9/30/2024
1.0.0-alpha12 88 7/28/2024
1.0.0-alpha11 75 7/8/2024
1.0.0-alpha10 145 3/4/2024
1.0.0-alpha09 81 2/6/2024
1.0.0-alpha08 71 2/6/2024
1.0.0-alpha07 92 1/22/2024
1.0.0-alpha06 71 1/16/2024
1.0.0-alpha05 80 12/16/2023
1.0.0-alpha04 75 12/11/2023
1.0.0-alpha03 77 12/8/2023
1.0.0-alpha02 101 12/3/2023
1.0.0-alpha01 80 12/1/2023

The implementation of the AddMessages method.