ResultPattern 1.2.0

dotnet add package ResultPattern --version 1.2.0                
NuGet\Install-Package ResultPattern -Version 1.2.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="ResultPattern" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ResultPattern --version 1.2.0                
#r "nuget: ResultPattern, 1.2.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.
// Install ResultPattern as a Cake Addin
#addin nuget:?package=ResultPattern&version=1.2.0

// Install ResultPattern as a Cake Tool
#tool nuget:?package=ResultPattern&version=1.2.0                

Result Pattern

This library is designed to avoid throwing exceptions in the code. Instead, it returns a result from the method and describes the error. The library helps reduce the number of try-catch blocks in the code and is less taxing on the program compared to catching and rethrowing exceptions.

ResultPattern Code Updates

Overview of Changes

Added Comments

  • Enhanced code readability and maintainability by adding comments that describe the purpose and functionality of various classes, properties, and methods.

New Properties

  • HasValue Property: Added to the Result<TValue, TError> class. This property indicates whether the result has a value (is not null).

New Methods

  • Success Method: Added to the Result class. This method creates a successful result with a null value, providing a convenient way to signify success without an associated value.

Program Version 1.2.0

Overview

Version 1.2.0 of the program includes several improvements and new features. This update focuses on enhancing functionality by adding new methods and fixing existing issues. Below are the detailed changes.

New Features

Added New Methods

  1. Failure<TValue>(string message, TValue value)

    • Creates a failed result with the specified error message and value.
    • Parameters:
      • message: The error message.
      • value: The value associated with the failed result.
  2. Failure<TValue, TError>(TError error, TValue value)

    • Creates a failed result with the specified error and value.
    • Parameters:
      • error: The error associated with the failed result.
      • value: The value associated with the failed result.
  3. Failure<TValue>(Exception error, TValue value)

    • Creates a failed result with the specified exception and value.
    • Parameters:
      • error: The exception associated with the failed result.
      • value: The value associated with the failed result.
  4. Success<TValue>(TValue value)

    • Creates a successful result with the specified value.
    • Parameters:
      • value: The value associated with the successful result.
  5. Success<TValue, TError>(TValue value)

    • Creates a successful result with the specified value.
    • Parameters:
      • value: The value associated with the successful result.

Improved Documentation

  • Added comprehensive comments to methods and properties to improve code readability and maintainability.

Example Usage

Setting a Value


using ResultPattern;

PatternResultTValueAndTError();
PatternResultTValue();
PatternResult();

static void PatternResultTValueAndTError()
{
	Result<double, DivideByZeroException> r = Divide(4.5, 7.8);

	if (r)
	{
		Console.WriteLine($"Success: {r.IsSuccess}");
		Console.WriteLine($"Result: {r.Value}");
	}
	else
	{
		Console.WriteLine($"Error: {r.Message}");
	}
}

static void PatternResultTValue()
{
	Result<int> r = IsSix(6);

	if (r)
	{
		Console.WriteLine($"Success: {r.IsSuccess}");
		Console.WriteLine($"Result: {r.Value}");
	}
	else
	{
		Console.WriteLine($"Error: {r.Message}");
	}
}

static void PatternResult()
{
	var r = StringIsEmpty(string.Empty);

	if (r)
	{
		Console.WriteLine($"Success: {r.IsSuccess}");
	}
	else
	{
		Console.WriteLine($"Error: {r.Message}");
	}
}

static Result<double, DivideByZeroException> Divide(double a, double b)
{
	if (b == 0)
	{
		return Result<double, DivideByZeroException>.Failure(new DivideByZeroException());
	}
	return Result<double, DivideByZeroException>.Success(a / b);
}

static Result<int> IsSix(int value)
{
	if(value is 6)
	{
		return Result<int>.Success(value);
	}

	return Result<int>.Failure($"{value} is not 6");
}

static Result StringIsEmpty(string value)
{
	if(value == string.Empty)
	{
		return Result.Success();
	}

	return Result.Failure($"{value} is not empty");
}

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 (1)

Showing the top 1 NuGet packages that depend on ResultPattern:

Package Downloads
FirebaseExtensions

A library has been created to simplify data retrieval and uploading in Firestore.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 83 6/9/2024
1.1.0 80 6/5/2024
1.0.0 79 5/30/2024

Version 1.2.0 of the program includes several improvements and new features. This update focuses on enhancing functionality by adding new methods and fixing existing issues. Below are the detailed changes.