Skip to content

A lightweight result pattern library for handling success/failure states for either in-process or networked scenarios.

License

Notifications You must be signed in to change notification settings

modabas/ModResults

Repository files navigation

ModResults

Nuget downloads Nuget License: MIT

ModResults provides a structured way to represent success or failure with optional details, enhancing readability and maintainability in codebases. It is designed for both in-process and networked scenarios with serialization/deserialization support. The library leverages nullability annotations, immutability, and factory methods for clarity.

✨ Features

  • Result Pattern: Encapsulates success or failure states with associated error messages and additional information.
  • Ready-to-Use Implementations:
    • Result and Result<TValue> with a default Failure state.
    • Result<TValue, TFailure> for custom failure states.
  • 📦 Extension Packages for Various Scenarios:
    • ModResults.FluentValidation bridges FluentValidation with ModResults for unified validation error handling.
    • ModResults.MinimalApis provides extensions to convert Result and Result<TValue> instances to ASP.NET Core Minimal APIs' IResult responses with proper HTTP status codes and response formatting.
    • ModResults.Orleans provides surrogate and converter implementations required by the Orleans serializer.
  • Serialization: JSON serialization/deserialization without special configuration.

🛠️ How To Use

Creating a Result or Result Instance

  • Success: Use Result.Ok() or Result<TValue>.Ok(TValue value).
  • Failure: Use static methods like Result.NotFound() or Result<TValue>.NotFound().
public async Task<Result<GetBookByIdResponse>> GetBookById(GetBookByIdRequest req, CancellationToken ct)
{
    var entity = await db.Books.FirstOrDefaultAsync(b => b.Id == req.Id, ct);

    return entity is null ?
      Result<GetBookByIdResponse>.NotFound() :
      Result.Ok(new GetBookByIdResponse(
        Id: entity.Id,
        Title: entity.Title,
        Author: entity.Author,
        Price: entity.Price));
}

Checking the State of a Result

A result can either be in an Ok or Failed state.

  • Ok State: The Result<TValue> instance contains a non-null Value of type TValue.
  • Failed State: Both Result and Result<TValue> instances contain a non-null Failure of type Failure.
public async Task<Result> PerformGetBookById(GetBookByIdRequest req, CancellationToken ct)
{
    var result = await GetBookById(req, ct);
    if (result.IsOk)
    {
        Console.WriteLine($"GetBookById is successful. Book title is {result.Value.Title}");
    }
    else
    {
        Console.WriteLine($"GetBookById has failed.");
    }
    return result
}

Advanced Topics

For more advanced examples, refer to the following:


About

A lightweight result pattern library for handling success/failure states for either in-process or networked scenarios.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages