Afonsomtsm.Result 0.9.3.8

There is a newer version of this package available.
See the version list below for details.
dotnet add package Afonsomtsm.Result --version 0.9.3.8
                    
NuGet\Install-Package Afonsomtsm.Result -Version 0.9.3.8
                    
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="Afonsomtsm.Result" Version="0.9.3.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Afonsomtsm.Result" Version="0.9.3.8" />
                    
Directory.Packages.props
<PackageReference Include="Afonsomtsm.Result" />
                    
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 Afonsomtsm.Result --version 0.9.3.8
                    
#r "nuget: Afonsomtsm.Result, 0.9.3.8"
                    
#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 Afonsomtsm.Result@0.9.3.8
                    
#: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=Afonsomtsm.Result&version=0.9.3.8
                    
Install as a Cake Addin
#tool nuget:?package=Afonsomtsm.Result&version=0.9.3.8
                    
Install as a Cake Tool

A Result type for CSharp which allows for error passing without exceptions

This package is an attempt to allow consumers to pass custom errors upstream without having to try and catch exceptions that may change over time, thus decreasing the maintenance cost of changes and bugfixes downstream.
This also allows for more functionality within error with an IError interface which can be extended to provide better usability, and more flexibility. Ex: Having a method to return the appropriate ObjectResult for a failure of a rest api;

    public interface IError 
    {
        public string Message { get; }
        public ErrorType Type { get; }
        
        public void Log(IErrorLogger logger);
    }

    public class NotFoundInDatabase : IError 
    {
        private string _message;
        public string Message => _message;

        public NotFoundInDatabaseError(Guid id) =>
            _message = String.Format("Item with id: {0} not found in database", id);

        public void Log(IErrorLogger logger) => logger.LogError(Message);
    }

You can then either create an abstract class/interface to wrap all Errors into a type that is more favourable for your application. Ex:


public interface IHttpParsableError : IError
{
    public ObjectResult GetHttpResponse();
}

So NotFoundInDatabase would become:


public class NotFoundInDatabase : IHttpParsableError
{
        private string _message;
        public string Message => _message;

        public NotFoundInDatabaseError(Guid id) =>
            _message = String.Format("Item with id: {0} not found in database", id);

        public void Log(IErrorLogger logger) => logger.LogError(Message);

        public ObjectResult GetHttpResponse() 
        {
            ObjectResult result = new(_message);
            result.StatusCode = StatusCodes.Status404NotFound;
            return result;
        }
}

Which can then be returned by your service and consumed appropriately inside of your controller :

    public class ItemService
    {
        private IDatabase _db;
        public ItemService(IDatabase db) => _db = db;
        
        public Result<Item> GetItem(Guid id)
        {
            Item? item = db.Item.Find(id);
            return Result<Item>.Unknown(item, new NotFoundInDatabase(id));
        }
    }

    [ApiController]
    [Route("[controller]")]
    public class ItemController : ControllerBase
    {
        [HttpGet]
        [ApiRoute("get-item")]
        public ObjectResult GetItem(Guid id, [Service]ItemService itemService)
        {
            Result<Item> result = itemService.GetItem(id);
            if (result.Succeeded)
                return Ok(result.Data);

            IHttpParsableError? parsableError = result.Error as IHttpParsableError;
            return parsableError is null
                ? Bad(result.Error.Message)
                : parsableError.GetHttpResponse();
        }
    }

The extension methods can be used to parse null values to results in a simpler style and to use Piping with single variables such as in Fsharp:


    public class ItemService
    {
        private IDatabase _db;
        public ItemService(IDatabase db) => _db = db;
        
        public Result<Item> GetById(Guid id) => _db.Item
            .Find(id)
            .ToResult(new NotFoundInDatabase(id));
    }
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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.  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.
  • net7.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Afonsomtsm.Result:

Package Downloads
Afonsomtsm.Filtering

A package to provide the structure for reusable filtering

Afonsomtsm.Money

A package to more easily deal with money

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 161 1/16/2025
0.9.6.2 242 9/15/2024
0.9.6.1-alpha 123 6/17/2024
0.9.6 164 5/31/2024
0.9.5.6 172 4/3/2024
0.9.5.5 138 4/1/2024
0.9.5.4 147 4/1/2024
0.9.5.3 181 3/15/2024
0.9.5.2 157 3/15/2024
0.9.5 149 3/15/2024
0.9.4 184 2/29/2024
0.9.3.8 147 2/29/2024
0.9.3 246 7/28/2023
0.9.2 236 7/20/2023
0.9.1 221 7/18/2023