Skip to content

Implement command validation and logging decorators, refactor command handlers to use new interfaces - #19

Merged
verf1x merged 1 commit into
mainfrom
mediatr
Sep 30, 2025
Merged

Implement command validation and logging decorators, refactor command handlers to use new interfaces#19
verf1x merged 1 commit into
mainfrom
mediatr

Conversation

@verf1x

@verf1x verf1x commented Sep 30, 2025

Copy link
Copy Markdown
Owner

Closes #20

@verf1x
verf1x requested a review from Copilot September 30, 2025 10:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the command handling architecture to use decorator patterns for validation and logging, while maintaining the existing command handler interfaces. The changes introduce new interfaces for commands that require validation or logging, and implement corresponding decorators to handle these cross-cutting concerns.

  • Removes validation logic from individual command handlers and centralizes it in a decorator
  • Adds logging capabilities through a decorator pattern
  • Updates command classes to implement new marker interfaces for validation and logging

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
DependencyInjection.cs (Tags) Removes registration for ICommandHandler<> interface
QuestionsController.cs Comments out GET endpoint and updates to use ISender
QuestionsSqlRepository.cs Reformats method signatures for better readability
QuestionsEfCoreRepository.cs Replaces NotImplementedException with stub implementations
CreateQuestionValidator.cs Updates validator to work with command instead of DTO
CreateQuestionHandler.cs Removes validation logic and validator dependency
CreateQuestionCommand.cs Adds IValidation and ILogging marker interfaces
AddAnswerValidator.cs Updates validator to work with command instead of DTO
AddAnswerHandler.cs Removes validation logic and validator dependency
AddAnswerCommand.cs Adds IValidation marker interface
DependencyInjection.cs (Application) Registers validation and logging decorators
ValidationDecorator.cs Implements validation and logging decorators
Shared.csproj Adds MediatR package reference
ValidationExtensions.cs Adds extension for multiple validation results
DependencyInjection.cs (Shared) Removes ICommandHandler<> registration
ICommandHandler.cs Adds IValidation and ILogging marker interfaces

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


namespace Questions.Application.Decorators;

public class LoggingDecorator<TCommand, TResponse> : ICommandHandler<TCommand, TResponse>

Copilot AI Sep 30, 2025

Copy link

Choose a reason for hiding this comment

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

The LoggingDecorator class is defined in the ValidationDecorator.cs file. This should be in its own file or the file should be renamed to reflect that it contains both decorators.

Copilot uses AI. Check for mistakes.

var result = await _inner.HandleAsync(command, cancellationToken);

_logger.LogInformation("Result: {result}", result);

Copilot AI Sep 30, 2025

Copy link

Choose a reason for hiding this comment

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

Logging the entire result object could be expensive for large responses. Consider logging only essential information like success/failure status or response type instead of the full result.

Suggested change
_logger.LogInformation("Result: {result}", result);
if (result.IsSuccess)
{
_logger.LogInformation("Result: Success for command {CommandName}", typeof(TCommand).Name);
}
else
{
_logger.LogWarning("Result: Failure for command {CommandName}. Errors: {Errors}", typeof(TCommand).Name, result.Error);
}

Copilot uses AI. Check for mistakes.
Comment on lines +12 to +13
public static ErrorsList ToErrors(this IEnumerable<ValidationResult> validationResult)
=> validationResult.SelectMany(e => e.Errors)

Copilot AI Sep 30, 2025

Copy link

Choose a reason for hiding this comment

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

The method parameter name 'validationResult' should be plural 'validationResults' since it accepts an enumerable of ValidationResult objects.

Suggested change
public static ErrorsList ToErrors(this IEnumerable<ValidationResult> validationResult)
=> validationResult.SelectMany(e => e.Errors)
public static ErrorsList ToErrors(this IEnumerable<ValidationResult> validationResults)
=> validationResults.SelectMany(e => e.Errors)

Copilot uses AI. Check for mistakes.
@verf1x
verf1x merged commit 9156cf5 into main Sep 30, 2025
1 check passed
@verf1x
verf1x deleted the mediatr branch September 30, 2025 10:25
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.

mediatr

2 participants