Conversation
… handlers to use new interfaces
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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.
|
|
||
| var result = await _inner.HandleAsync(command, cancellationToken); | ||
|
|
||
| _logger.LogInformation("Result: {result}", result); |
There was a problem hiding this comment.
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.
| _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); | |
| } |
| public static ErrorsList ToErrors(this IEnumerable<ValidationResult> validationResult) | ||
| => validationResult.SelectMany(e => e.Errors) |
There was a problem hiding this comment.
The method parameter name 'validationResult' should be plural 'validationResults' since it accepts an enumerable of ValidationResult objects.
| 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) |
Closes #20