Skip to content

Remove result from handlers#15

Merged
penyland merged 16 commits intomainfrom
remove-result-from-handlers
Oct 28, 2025
Merged

Remove result from handlers#15
penyland merged 16 commits intomainfrom
remove-result-from-handlers

Conversation

@penyland
Copy link
Owner

This pull request introduces significant improvements to both the Infinity.Toolkit.AspNetCore library and the sample application, focusing on enhanced support for result-based request handlers and more flexible endpoint mapping. The changes modernize the handler registration patterns, improve error handling, and add new extension methods for mapping endpoints that return Result<T> types, making it easier to build robust, composable APIs.

Key changes include:

Endpoint Mapping Enhancements (Infinity.Toolkit.AspNetCore)

  • Added multiple new extension methods to EndpointRouteBuilderExtensions for mapping GET endpoints that work with request handlers returning Result<T> types, including overloads that support mapping and transformation of results. These methods improve error handling and response formatting for both success and failure cases. [1] [2]
  • Introduced new overloads for mapping endpoints that apply mapping functions to handler results, allowing for more flexible response shaping.
  • Updated XML documentation for all new methods to clarify usage and expected behavior. [1] [2]

Sample Application Modernization

  • Refactored handler registration in Program.cs to use Result<T> for response types, updated decorator registrations, and improved logging decorators for better diagnostics and error reporting. [1] [2] [3] [4] [5]
  • Updated endpoint mapping in the sample to use the new MapGetRequestHandlerWithResult extension, demonstrating the new result-based pattern.
  • Improved exception handling in request handlers to ensure failures are captured and returned as Result.Failure. [1] [2] [3]

Miscellaneous

  • Bumped the package version to 1.2.0 to reflect the addition of new features and breaking changes.

These updates collectively make the toolkit and sample app more robust, extensible, and aligned with modern .NET API design practices.

Added a new method `ToProblemDetails<T>` in `ResultExtensions` to convert `ErrorResult<T>` to `ProblemDetails`, improving API response handling. Updated `Error` class to include `ErrorType` for better error categorization, and introduced `ValidationError` class. Extended `Result` class with new `Failure` methods to handle collections of errors. These changes enhance error handling robustness and clarity.
Added logging capabilities to `ConfigurationBuilderExtensions.cs` by introducing `Microsoft.Extensions.Logging`. Configured a `LoggerFactory` to read from the "Logging" section and added debug and console logging in debug mode. Replaced exception throwing with error logging for missing Azure App Configuration endpoint, improving application robustness.
Simplified the request handling system by removing the `Result<T>` wrapper from `HandleAsync` methods in `IRequestHandler` interfaces. Updated `HandleAsync` to return plain `TResponse` for both parameterless and parameterized handlers.

Added `MapGetQuery` and `MapGetQueryWithResult` methods in `EndpointRouteBuilderExtensions` to streamline GET endpoint mapping with support for different request and response patterns.

Introduced `RequestHandlerBase<TRequest, TResponse>` abstract class to provide a reusable base for implementing request handlers, improving code organization and maintainability.
Refactored service registrations and handler implementations:
- Registered `InMemoryDatabase` as a singleton.
- Updated `CreateProductHandler` to use a logging decorator.
- Registered and decorated `ProductCreatedQuery` with `Result<Product>`.
- Introduced `CreateProductHandlerLoggingDecorator` for detailed logging.
- Simplified generic logging handlers and renamed `LoggingRequestHandler<TIn, TResult>` to `LoggingRequestHandler2`.

Streamlined endpoints:
- Updated `POST /product` to always return `Results.Ok()`.
- Replaced `MapGetQuery` with `MapGetQueryWithResult` for `GET /product/{id}`.

Removed `Result` wrapper from handlers, improved logging consistency, and cleaned up unused or redundant code.
Updated `CreateProductHandler` and related classes to use the
`Result<T>` pattern, improving error handling and encapsulating
success/failure states. Enhanced logging in decorators to provide
detailed insights into both success and failure scenarios.

Modified `/product` endpoint to work with `Result<CreateProductResponse>`
and updated response handling logic. Streamlined logging logic in
`LoggingRequestHandler2` and removed commented-out code.

These changes improve robustness, consistency, and debugging
capabilities across the application.
Introduced new `MapGetRequestHandler` methods to simplify the mapping of GET endpoints in ASP.NET Core applications. These methods support request/response handling, mapping functions, and `Result` objects.

- Added overloads for handling requests and responses with or without mapping functions.
- Ensured consistent HTTP response handling (`200 OK`, `400 Bad Request`, or `Problem`).
- Validated input parameters (e.g., `mapper` functions) for better error handling.
- Added XML documentation for improved clarity and usability.
- Removed legacy code to enhance maintainability and extensibility.
Replaced `MapGetQueryWithResult` with `MapGetRequestHandlerWithResult` for the `/product/{id}` endpoint to align with a new request-handling pattern. Updated endpoint metadata to include a new name (`GetProductCreated`), summary, and description for improved clarity and documentation.
Updated `Infinity.Toolkit.csproj` to target both .NET 10.0 and .NET 9.0, enabling multi-framework support. Bumped version prefix from `1.2.0` to `1.3.0`.

Enhanced the `Error` class by:
- Adding a default value (`ErrorType.Failure`) for the `type` parameter in the constructor.
- Introducing predefined error instances: `None` and `NullValue`.
- Adding a `Validation` method to create validation errors with specific codes and messages.
Updated Infinity.Toolkit.TestUtils.csproj to target both .NET 10.0 and .NET 9.0, enabling multi-framework compatibility.

Refactored assertions in ResultExtensionsTests to use Shouldly for improved readability and maintainability. Added validations for the `errors` collection in `problemDetails.Extensions`.
Copilot AI review requested due to automatic review settings October 28, 2025 16:58
Updated the `<VersionPrefix>` property in the `Infinity.Toolkit.Azure.csproj` file from `1.0.0` to `1.0.1`. This version increment reflects a minor update or patch release, signaling updates, bug fixes, or improvements in the project.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces significant refactoring to the Infinity.Toolkit library, focusing on enhancing error handling capabilities and modernizing the handler interfaces. The changes include breaking API modifications to support more flexible handler patterns and improved error type categorization.

Key Changes:

  • Refactored IRequestHandler interfaces to return concrete types instead of Result<T>, with new "WithResult" variants for backward compatibility
  • Introduced ErrorType enum with HTTP status code values to categorize errors (Validation, NotFound, Conflict, etc.)
  • Added new endpoint mapping methods (MapGetRequestHandler, MapGetRequestHandlerWithResult) with better type safety and mapping support

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
src/Infinity.Toolkit/Result/Error.cs Added ErrorType enum with HTTP status codes, introduced ValidationError class, and enhanced Error constructor with type parameter
src/Infinity.Toolkit/Result/Result.cs Added new Failure<T> overload accepting IReadOnlyCollection<Error>
src/Infinity.Toolkit/Handlers/IRequestHandler.cs Breaking change: modified interface methods to return TResponse instead of Result<TResponse>, added RequestHandlerBase abstract class
src/Infinity.Toolkit.AspNetCore/ResultExtensions.cs Updated ToProblemDetails to include errors collection in extensions, added overload for ErrorResult<T>
src/Infinity.Toolkit.AspNetCore/EndpointRouteBuilderExtensions.cs Added multiple new endpoint mapping methods for handlers with and without Result wrapping, including mapper support
src/Infinity.Toolkit.Azure/Configuration/ConfigurationBuilderExtensions.cs Removed AddAzureAppConfiguration service registration, changed error handling from exception to logging
src/Infinity.Toolkit/Infinity.Toolkit.csproj Bumped version to 1.3.0 and added .NET 10.0 target framework
src/Infinity.Toolkit.TestUtils/Infinity.Toolkit.TestUtils.csproj Added .NET 10.0 target framework
src/Infinity.Toolkit.AspNetCore/Infinity.Toolkit.AspNetCore.csproj Bumped version to 1.2.0
tests/Infinity.Toolkit.Tests/ResultExtensionsTests.cs Updated test assertions to use Shouldly assertions and verify error collection in ProblemDetails extensions
samples/HandlersSample/Program.cs Updated sample to demonstrate new handler patterns with Result wrapping and decorator patterns

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


IResult response = result switch
{
Success => TypedResults.Ok(mapper(result)),
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect usage of mapper function. The mapper should be applied to result.Value (the unwrapped TResponse), not to result (which is a Result). This line should be Success => TypedResults.Ok(mapper(result.Value)),.

Suggested change
Success => TypedResults.Ok(mapper(result)),
Success => TypedResults.Ok(mapper(result.Value)),

Copilot uses AI. Check for mistakes.
penyland and others added 6 commits October 28, 2025 18:12
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@penyland penyland merged commit fcf358e into main Oct 28, 2025
2 checks passed
@penyland penyland deleted the remove-result-from-handlers branch October 28, 2025 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants