-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQueryHandlerValidationDecorator.cs
43 lines (39 loc) · 1.44 KB
/
QueryHandlerValidationDecorator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using FluentValidation;
using HumbleMediator;
namespace WebApiTemplate.Application.Validation;
/// <summary>
/// Decorator for validating query handlers.
/// </summary>
/// <typeparam name="TQuery"></typeparam>
/// <typeparam name="TQueryResult"></typeparam>
public sealed class QueryHandlerValidationDecorator<TQuery, TQueryResult>
: BaseValidationDecorator<TQuery>,
IQueryHandler<TQuery, TQueryResult>
where TQuery : IQuery<TQueryResult>
{
private readonly IQueryHandler<TQuery, TQueryResult> _decorated;
/// <summary>
/// Initializes a new instance of the <see cref="QueryHandlerValidationDecorator{TQuery, TQueryResult}"/> class.
/// </summary>
/// <param name="decorated">The query handler to decorate.</param>
/// <param name="validators">The validators to use.</param>
public QueryHandlerValidationDecorator(
IQueryHandler<TQuery, TQueryResult> decorated,
IEnumerable<IValidator<TQuery>> validators
)
: base(validators)
{
_decorated = decorated;
}
/// <summary>
/// Validates the query and handles it.
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<TQueryResult> Handle(TQuery request, CancellationToken cancellationToken)
{
await Validate(request, cancellationToken);
return await _decorated.Handle(request, cancellationToken);
}
}