TheNoobs.Results 1.0.0-rc.7

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

// Install TheNoobs.Results as a Cake Tool
#tool nuget:?package=TheNoobs.Results&version=1.0.0-rc.7&prerelease

Results

The Result pattern to avoid the usage of exceptions in making decisions.

Nuget License Maintainability Rating Bugs Reliability Rating Quality Gate Status

Objectives

This library doesn't have the intent to replace the exceptions usage, but sometimes we need to implement a method that can return more than one type of response, and based on this response we need to modify the application flow to reflect system specifications.

It's very common to see codes using exceptions as control flow to solve that, but as we know it's an anti pattern and should be avoided.

Example:

public class Repository
{
    private readonly ICache _cache;

    public Repository(ICache cache)
    {
        _cache = cache ?? throw new ArgumentNullException(nameof(cache));
    }

    public ISomething GetSomething(int id, bool loadFromCache = true)
    {
        ISomething item;
        if (loadFromCache)
        {
            item = _cache.Load();
            if (item.NeedToBeReloaded())
            {
                throw new EntityMustBeLoadFromDatabaseException(id);
            }

            return item;
        }

        item = GetSomethingFromDatabase(id);

        _cache.Set(id, item);
        return item;
    }

    private ISomething GetSomethingFromDatabase(int id)
    {
        return $"SELECT * FROM Something WHERE Id = {id}".Query<Something>();
    }
}
    
public class Service
{
    private readonly Repository _repository;
    private readonly ILogger<Service> _logger;

    public Service(Repository repository, ILogger<Service> logger)
    {
        _repository = repository ?? throw new ArgumentNullException(nameof(repository));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public ISomething GetSomething(int id)
    {
        try
        {
            return _repository.GetSomething(id);
        }
        catch (EntityMustBeLoadFromDatabaseException _)
        {
            return _repository.GetSomething(id, false);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex.Message, ex);
            throw;
        }
    }
}

How to use

    public class EntityMustBeLoadFromDatabase : Fail
    {
        public EntityMustBeLoadFromDatabase(int id)
            : base($"Entity must be reload from database. Id: {id}")
        {
            Id = id;
        }

        public int Id { get; }
    }

    public class Repository
    {
        private readonly ICache _cache;

        public Repository(ICache cache)
        {
            _cache = cache ?? throw new ArgumentNullException(nameof(cache));
        }

        public Result<ISomething> GetSomething(int id, bool loadFromCache = true)
        {
            Something item;
            if (loadFromCache)
            {
                item = _cache.Load();
                if (item.NeedToBeReloaded())
                {
                    return new EntityMustBeLoadFromDatabase(id);
                }

                return item;
            }

            item = GetSomethingFromDatabase(id);

            _cache.Set(id, item);
            return item;
        }

        private Something GetSomethingFromDatabase(int id)
        {
            return $"SELECT * FROM Something WHERE Id = {id}".Query<Something>();
        }
    }

    
    public class Service
    {
        private readonly Repository _repository;
        private readonly ILogger<Service> _logger;

        public Service(Repository repository, ILogger<Service> logger)
        {
            _repository = repository ?? throw new ArgumentNullException(nameof(repository));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        public ISomething GetSomething(int id)
        {
            try
            {
                var result = _repository.GetSomething(id);
                return result
                    .Switch<ISomething>()
                    .Case<Success<ISomething>>(success => success.GetResult())
                    .Case<EntityMustBeLoadFromDatabase>(reload =>
                    {
                        var something = _repository.GetSomething(reload.Id, false);
                        if (something.IsFail())
                        {
                            throw new Exception($"Cannot get the entity with id: {id}.");
                        }

                        return something.GetResult();
                    })
                    .Else(another => throw new Exception("You can do a type check and process as you wish."));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                throw;
            }
        }
    }

♥ Made with love!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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
2.4.0-rc.18 51 4/29/2024
2.3.0 118 1/8/2024
2.2.1 98 1/7/2024
2.2.0 85 1/5/2024
2.2.0-rc.3 56 1/5/2024
2.1.1 132 11/26/2023
2.1.0 102 11/25/2023
2.0.0 100 11/25/2023
1.0.1 383 10/14/2022
1.0.1-rc.2 105 10/14/2022
1.0.0 371 10/14/2022
1.0.0-rc.7 91 10/14/2022
1.0.0-rc.2 92 10/14/2022
1.0.0-rc.1 97 10/14/2022