TheNoobs.Results
1.0.0
See the version list below for details.
dotnet add package TheNoobs.Results --version 1.0.0
NuGet\Install-Package TheNoobs.Results -Version 1.0.0
<PackageReference Include="TheNoobs.Results" Version="1.0.0" />
paket add TheNoobs.Results --version 1.0.0
#r "nuget: TheNoobs.Results, 1.0.0"
// Install TheNoobs.Results as a Cake Addin #addin nuget:?package=TheNoobs.Results&version=1.0.0 // Install TheNoobs.Results as a Cake Tool #tool nuget:?package=TheNoobs.Results&version=1.0.0
Results
The Result pattern to avoid the usage of exceptions in making decisions.
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 | Versions 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. |
-
.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 |
---|---|---|
4.1.0-rc.3 | 75 | 8/13/2024 |
4.1.0-rc.2 | 47 | 8/2/2024 |
4.1.0-rc.1 | 53 | 8/2/2024 |
4.0.1 | 90 | 11/1/2024 |
4.0.0 | 123 | 7/1/2024 |
4.0.0-rc.24 | 53 | 6/30/2024 |
4.0.0-rc.23 | 56 | 6/28/2024 |
4.0.0-rc.19 | 52 | 6/28/2024 |
4.0.0-rc.17 | 60 | 6/26/2024 |
4.0.0-rc.16 | 51 | 6/26/2024 |
4.0.0-rc.15 | 54 | 6/26/2024 |
4.0.0-rc.14 | 52 | 6/25/2024 |
4.0.0-rc.13 | 50 | 6/25/2024 |
4.0.0-rc.11 | 48 | 6/25/2024 |
4.0.0-rc.10 | 46 | 6/25/2024 |
4.0.0-rc.9 | 47 | 6/25/2024 |
4.0.0-rc.8 | 54 | 6/25/2024 |
4.0.0-rc.2 | 54 | 6/28/2024 |
3.1.1 | 110 | 6/20/2024 |
3.1.0 | 110 | 6/19/2024 |
3.0.0 | 108 | 6/18/2024 |
2.4.0-rc.18 | 71 | 4/29/2024 |
2.4.0-rc.7 | 60 | 6/18/2024 |
2.3.0 | 148 | 1/8/2024 |
2.2.1 | 129 | 1/7/2024 |
2.2.0 | 117 | 1/5/2024 |
2.2.0-rc.3 | 72 | 1/5/2024 |
2.1.1 | 172 | 11/26/2023 |
2.1.0 | 128 | 11/25/2023 |
2.0.0 | 125 | 11/25/2023 |
1.0.1 | 410 | 10/14/2022 |
1.0.1-rc.2 | 120 | 10/14/2022 |
1.0.0 | 401 | 10/14/2022 |
1.0.0-rc.7 | 106 | 10/14/2022 |
1.0.0-rc.2 | 105 | 10/14/2022 |
1.0.0-rc.1 | 111 | 10/14/2022 |