-
Notifications
You must be signed in to change notification settings - Fork 150
/
RequestValidationBehavior.cs
99 lines (85 loc) · 3.23 KB
/
RequestValidationBehavior.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Text.Json;
using BuildingBlocks.Validation.Extensions;
using FluentValidation;
using MediatR;
using Microsoft.Extensions.Logging;
namespace BuildingBlocks.Validation;
public class RequestValidationBehavior<TRequest, TResponse>(
IServiceProvider serviceProvider,
ILogger<RequestValidationBehavior<TRequest, TResponse>> logger
) : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TResponse : class
{
public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken
)
{
var validator = serviceProvider.GetService<IValidator<TRequest>>()!;
if (validator is null)
return await next();
logger.LogInformation(
"[{Prefix}] Handle request={RequestData} and response={ResponseData}",
nameof(RequestValidationBehavior<TRequest, TResponse>),
typeof(TRequest).Name,
typeof(TResponse).Name
);
logger.LogDebug(
"Handling {FullName} with content {Request}",
typeof(TRequest).FullName,
JsonSerializer.Serialize(request)
);
await validator.HandleValidationAsync(request, cancellationToken);
var response = await next();
logger.LogInformation("Handled {FullName}", typeof(TRequest).FullName);
return response;
}
}
public class StreamRequestValidationBehavior<TRequest, TResponse>(
IServiceProvider serviceProvider,
ILogger<StreamRequestValidationBehavior<TRequest, TResponse>> logger,
IValidator<TRequest> validator
) : IStreamPipelineBehavior<TRequest, TResponse>
where TRequest : IStreamRequest<TResponse>
where TResponse : class
{
private readonly ILogger<StreamRequestValidationBehavior<TRequest, TResponse>> _logger =
logger ?? throw new ArgumentNullException(nameof(logger));
private readonly IServiceProvider _serviceProvider =
serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
public async IAsyncEnumerable<TResponse> Handle(
TRequest request,
StreamHandlerDelegate<TResponse> next,
CancellationToken cancellationToken
)
{
validator = _serviceProvider.GetService<IValidator<TRequest>>()!;
if (validator is null)
{
await foreach (var response in next().WithCancellation(cancellationToken))
{
yield return response;
}
yield break;
}
_logger.LogInformation(
"[{Prefix}] Handle request={RequestData} and response={ResponseData}",
nameof(StreamRequestValidationBehavior<TRequest, TResponse>),
typeof(TRequest).Name,
typeof(TResponse).Name
);
_logger.LogDebug(
"Handling {FullName} with content {Request}",
typeof(TRequest).FullName,
JsonSerializer.Serialize(request)
);
validator.HandleValidation(request);
await foreach (var response in next().WithCancellation(cancellationToken))
{
yield return response;
_logger.LogInformation("Handled {FullName}", typeof(TRequest).FullName);
}
}
}