-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathValidationRequestHandlerDecorator.cs
More file actions
32 lines (28 loc) · 1.14 KB
/
ValidationRequestHandlerDecorator.cs
File metadata and controls
32 lines (28 loc) · 1.14 KB
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
namespace softaware.Cqs.Decorators.Validation;
/// <summary>
/// A decorator for validating the specified request. Uses the contructor injected <see cref="IValidator"/> for validating the query.
/// </summary>
/// <typeparam name="TRequest">The type of the request to execute.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
public class ValidationRequestHandlerDecorator<TRequest, TResult> : IRequestHandler<TRequest, TResult>
where TRequest : IRequest<TResult>
{
private readonly IValidator validator;
private readonly IRequestHandler<TRequest, TResult> decoratee;
public ValidationRequestHandlerDecorator(
IValidator validator,
IRequestHandler<TRequest, TResult> decoratee)
{
this.validator = validator;
this.decoratee = decoratee;
}
public Task<TResult> HandleAsync(TRequest request, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
this.validator.ValidateObject(request);
return this.decoratee.HandleAsync(request, cancellationToken);
}
}