Afonsomtsm.Result
0.9.4
See the version list below for details.
dotnet add package Afonsomtsm.Result --version 0.9.4
NuGet\Install-Package Afonsomtsm.Result -Version 0.9.4
<PackageReference Include="Afonsomtsm.Result" Version="0.9.4" />
paket add Afonsomtsm.Result --version 0.9.4
#r "nuget: Afonsomtsm.Result, 0.9.4"
// Install Afonsomtsm.Result as a Cake Addin #addin nuget:?package=Afonsomtsm.Result&version=0.9.4 // Install Afonsomtsm.Result as a Cake Tool #tool nuget:?package=Afonsomtsm.Result&version=0.9.4
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 | Versions 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. |
-
net7.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Afonsomtsm.Result:
Package | Downloads |
---|---|
Afonsomtsm.Money
A package to more easily deal with money |
|
Afonsomtsm.Filtering
A package to provide the structure for reusable filtering |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 107 | 1/16/2025 |
0.9.6.2 | 202 | 9/15/2024 |
0.9.6.1-alpha | 109 | 6/17/2024 |
0.9.6 | 132 | 5/31/2024 |
0.9.5.6 | 141 | 4/3/2024 |
0.9.5.5 | 103 | 4/1/2024 |
0.9.5.4 | 116 | 4/1/2024 |
0.9.5.3 | 150 | 3/15/2024 |
0.9.5.2 | 127 | 3/15/2024 |
0.9.5 | 120 | 3/15/2024 |
0.9.4 | 153 | 2/29/2024 |
0.9.3.8 | 116 | 2/29/2024 |
0.9.3 | 211 | 7/28/2023 |
0.9.2 | 197 | 7/20/2023 |
0.9.1 | 183 | 7/18/2023 |