forked from mehdihadeli/food-delivery-microservices
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequestValidationBehavior.cs
More file actions
99 lines (84 loc) · 3.3 KB
/
RequestValidationBehavior.cs
File metadata and controls
99 lines (84 loc) · 3.3 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
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 Mediator;
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 ValueTask<TResponse> Handle(
TRequest message,
CancellationToken cancellationToken,
MessageHandlerDelegate<TRequest, TResponse> next
)
{
var validator = serviceProvider.GetService<IValidator<TRequest>>()!;
if (validator is null)
return await next(message, cancellationToken);
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(message)
);
await validator.HandleValidationAsync(message, cancellationToken);
var response = await next(message, cancellationToken);
logger.LogInformation("Handled {FullName}", typeof(TRequest).FullName);
return response;
}
}
public class StreamRequestValidationBehavior<TRequest, TResponse>(
IServiceProvider serviceProvider,
ILogger<StreamRequestValidationBehavior<TRequest, TResponse>> logger
) : 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 message,
CancellationToken cancellationToken,
StreamHandlerDelegate<TRequest, TResponse> next
)
{
var validator = _serviceProvider.GetService<IValidator<TRequest>>()!;
if (validator is null)
{
await foreach (var response in next(message, 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(message)
);
await validator.HandleValidationAsync(message, cancellationToken: cancellationToken);
await foreach (var response in next(message, cancellationToken))
{
yield return response;
_logger.LogInformation("Handled {FullName}", typeof(TRequest).FullName);
}
}
}