-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathValidatorServiceProviderCache.cs
More file actions
40 lines (35 loc) · 1.26 KB
/
ValidatorServiceProviderCache.cs
File metadata and controls
40 lines (35 loc) · 1.26 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
using System.Diagnostics.CodeAnalysis;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
namespace GraphQL.FluentValidation;
/// <summary>
/// Uses the <see cref="IServiceProvider"/> to determine what validators are available and resolve validator instances.
/// </summary>
sealed class ValidatorServiceProviderCache : IValidatorCache
{
/// <inheritdoc />
public bool IsFrozen => true;
/// <inheritdoc />
public void Freeze()
{
// Intentionally empty.
}
/// <inheritdoc />
public bool TryGetValidators(Type argumentType, IServiceProvider? provider, [NotNullWhen(true)] out IEnumerable<IValidator>? validators)
{
var validatorType = typeof(IValidator<>).MakeGenericType(argumentType);
try
{
validators = provider!.GetServices(validatorType).Cast<IValidator>();
return true;
}
catch (InvalidOperationException)
{
validators = null;
return false;
}
}
/// <inheritdoc />
public void AddResult(AssemblyScanner.AssemblyScanResult result) =>
throw new InvalidOperationException("Method not supported. The service provider cache uses IServiceProvider to determine what validators are available.");
}