BeesSnazzySnippets 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package BeesSnazzySnippets --version 1.1.0
                    
NuGet\Install-Package BeesSnazzySnippets -Version 1.1.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="BeesSnazzySnippets" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BeesSnazzySnippets" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="BeesSnazzySnippets" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BeesSnazzySnippets --version 1.1.0
                    
#r "nuget: BeesSnazzySnippets, 1.1.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.
#:package BeesSnazzySnippets@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BeesSnazzySnippets&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=BeesSnazzySnippets&version=1.1.0
                    
Install as a Cake Tool

A lightweight NuGet library for C# developers designed to simplify error handling while capturing actionable results for user interfaces. This library provides two main constructs: ApiResult and ServiceResult, allowing for clear representation of success and failure states in your applications.

Installation

To install the BeesSnazzySnippets.Results library, use the following command in your NuGet Package Manager Console:

Install-Package BeesSnazzySnippets

Alternatively, you can add it to your project file:

<PackageReference Include="BeesSnazzySnippets.Results" Version="1.0.0" />

Usage

ServiceResult

The ServiceResult class provides a mechanism to encapsulate potential error states, as well as surface the cause via the pre-defined ResultStatusCode enum.

This type is intended to be used internally; for a public-facing type, refer to the ApiResult

Below is a simple example of the ServiceResult where there are three possible return paths: the user could not be created because the name is taken, the user could not be created because of an unknown error, or the user is created successfully.

public async Task<ServiceResult<User>> CreateUserAsync(string username, string password)
{
    try
    {
        var isUsernameTaken = await _userService.IsUsernameTakenAsync(username);
        if(isUsernameTaken)
            return ServiceResult.Failure<User>("Username taken.", ResultStatusCode.ResourceAlreadyExists);

        var user = await _userService.CreateUserAsync(username, password);
        
        return user; // Implicit casting to a successful ServiceResult<User>
                     // Equivalent to: return ServiceResult.Success(user);
    }
    catch (Exception ex)
    {
        return ServiceResult.Failure<User>(ex, "Could not create user");
    }
}

The caller is then able to use this result to present a useful error to the user/client. For example, in an API controller it may look as follows:

    public async Task<IActionResult> CreateUser(string username, string password)
    {
        var createUserResult = await CreateUserAsync(username, password);
        if (createUserResult.TryGet(out var user))
            return Ok(user);

        if (createUserResult.StatusCode == ResultStatusCode.ResourceAlreadyExists)
            return Conflict();

        return Problem();
    }

ApiResult

This section has not been written yet.

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.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 132 9/10/2024
1.1.0 143 7/16/2024