request/src/request.validation/DispatchingValidator.cs
Louis Seubert c674de31f7
Some checks failed
default / dotnet-default-workflow (pull_request) Successful in 2m5s
default / dotnet-default-workflow (push) Has been cancelled
chore: rename internal types
Rename internal types in the validation to be more consistent with other
projects
2026-05-30 19:36:28 +02:00

44 lines
1.1 KiB
C#

// Copyright (c) The Geekeey Authors
// SPDX-License-Identifier: EUPL-1.2
using System.Collections.Concurrent;
using System.Reflection.Metadata;
[assembly: MetadataUpdateHandler(typeof(Geekeey.Request.Validation.DispatchingValidator))]
namespace Geekeey.Request.Validation;
internal sealed partial class DispatchingValidator : IValidator
{
private static readonly ConcurrentDictionary<Type, ValidatorInvoker> ValidatorsHandlers = new();
private readonly IServiceProvider _serviceProvider;
public DispatchingValidator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public static void ClearCache(Type[]? _)
{
ValidatorsHandlers.Clear();
}
public Validation Validate(ValidationContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context.Instance is null)
{
return new Validation([]);
}
var handler = ValidatorsHandlers.GetOrAdd(context.Instance.GetType(), static key =>
{
var type = typeof(ValidatorInvoker<>).MakeGenericType(key);
return (ValidatorInvoker)Activator.CreateInstance(type)!;
});
return handler.Validate(context, _serviceProvider);
}
}