diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..2176504 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.{cs,vb}] + +# IDE0130: Namespace does not match folder structure +dotnet_diagnostic.IDE0130.severity = none diff --git a/Miscellaneous.sln b/Miscellaneous.sln index a1871d4..b84f97b 100644 --- a/Miscellaneous.sln +++ b/Miscellaneous.sln @@ -50,9 +50,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FluentValidation", "FluentV EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.FluentValidation.Extensions.UnitTests", "tests\FluentValidation\BitzArt.FluentValidation.Extensions.UnitTests\BitzArt.FluentValidation.Extensions.UnitTests.csproj", "{9FDF435B-8ACD-48D5-986A-1B526D7F6E38}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitzArt.LinqExtensions.Batching", "src\BitzArt.LinqExtensions.Batching\BitzArt.LinqExtensions.Batching.csproj", "{81AC0594-A0C5-4A02-8C4D-B8E139F16F5E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.LinqExtensions.Batching", "src\BitzArt.LinqExtensions.Batching\BitzArt.LinqExtensions.Batching.csproj", "{81AC0594-A0C5-4A02-8C4D-B8E139F16F5E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitzArt.LinqExtensions.Batching.Tests", "tests\BitzArt.LinqExtensions.Batching.Tests\BitzArt.LinqExtensions.Batching.Tests.csproj", "{6DBA7894-9852-4DF1-B9E0-09D60D311F6B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.LinqExtensions.Batching.Tests", "tests\BitzArt.LinqExtensions.Batching.Tests\BitzArt.LinqExtensions.Batching.Tests.csproj", "{6DBA7894-9852-4DF1-B9E0-09D60D311F6B}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{629938AD-8A1E-49C0-ABFB-724E564B0CCB}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/FluentValidation/BitzArt.FluentValidation.Extensions/ActionTypes.cs b/src/FluentValidation/BitzArt.FluentValidation.Extensions/ActionTypes.cs deleted file mode 100644 index ef0fea3..0000000 --- a/src/FluentValidation/BitzArt.FluentValidation.Extensions/ActionTypes.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace FluentValidation; - -public static class ActionTypes -{ - public const string Get = "Get"; - - public const string Create = "Create"; - - public const string Update = "Update"; - - public const string Patch = "Patch"; - - public const string Options = "Options"; - - public const string Delete = "Delete"; -} diff --git a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Enums/ActionType.cs b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Enums/ActionType.cs index daa7797..3aaa3b2 100644 --- a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Enums/ActionType.cs +++ b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Enums/ActionType.cs @@ -1,24 +1,16 @@ -using System.Runtime.Serialization; - -namespace FluentValidation; +namespace FluentValidation; public enum ActionType : byte { - [EnumMember(Value = ActionTypes.Get)] Get = 1, - [EnumMember(Value = ActionTypes.Create)] Create = 2, - [EnumMember(Value = ActionTypes.Update)] Update = 3, - [EnumMember(Value = ActionTypes.Patch)] Patch = 4, - [EnumMember(Value = ActionTypes.Options)] Options = 5, - [EnumMember(Value = ActionTypes.Delete)] Delete = 6 } diff --git a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Extensions/AddActionValidatorExtensions.cs b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Extensions/AddActionValidatorExtensions.cs new file mode 100644 index 0000000..8eaa9a3 --- /dev/null +++ b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Extensions/AddActionValidatorExtensions.cs @@ -0,0 +1,80 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using System.Reflection; + +namespace FluentValidation; + +public static class AddActionValidatorExtensions +{ + public static IServiceCollection AddActionValidatorsFromAssemblyContaining(this IServiceCollection services, Func? actionTypeResolver = null) + => services.AddActionValidatorsFromAssemblyContaining(typeof(TAssemblyPointer), actionTypeResolver); + + public static IServiceCollection AddActionValidatorsFromAssemblyContaining(this IServiceCollection services, Type type, Func? actionTypeResolver = null) + => services.AddActionValidatorsFromAssembly(type.Assembly, actionTypeResolver); + + public static IServiceCollection AddActionValidatorsFromAssembly(this IServiceCollection services, Assembly assembly, Func? actionTypeResolver = null) + { + var validators = assembly + .DefinedTypes + .Where(x => x.IsClass && !x.IsAbstract) + .Where(x => x.GetInterfaces() + .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IActionValidator<>))); + + foreach (var validator in validators) services.AddActionValidator(validator, actionTypeResolver); + + return services; + } + + public static IServiceCollection AddActionValidator(this IServiceCollection services, Func? actionTypeResolver = null) + => services.AddActionValidator(typeof(TValidator), actionTypeResolver); + + public static IServiceCollection AddActionValidator(this IServiceCollection services, Type validatorType, Func? actionTypeResolver = null) + { + if (validatorType is null) throw new ArgumentException($"{nameof(validatorType)} must not be null"); + if (validatorType.BaseType!.GetGenericTypeDefinition() != typeof(ActionValidator<>)) throw new ArgumentException($"{validatorType.Name} is not assignable to ActionValidator"); + + services.TryAddScoped(serviceProvider => new ActionValidatorFactory(serviceProvider)); + + var interfaceDefinitions = validatorType.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IActionValidator<>)).ToList(); + if (interfaceDefinitions.Count == 0) throw new ArgumentException($"{validatorType.Name} does not implement IActionValidator"); + + services.AddTransient(validatorType); + ActionValidatorFactory.ValidatorTypeMap[validatorType] = validatorType; + + Func finalActionTypeResolver = actionTypeResolver is not null ? + x => actionTypeResolver(x) : + x => null; + + foreach (var interfaceDefinition in interfaceDefinitions) + { + var validationObjectType = interfaceDefinition.GetGenericArguments().First(); + services.AddActionValidator(validatorType, validationObjectType, finalActionTypeResolver); + } + + return services; + } + + private static IServiceCollection AddActionValidator(this IServiceCollection services, Type validatorType, Type validationObjectType, Func getActionType) + { + List registrationInterfaces = + [ + typeof(IValidator<>).MakeGenericType(validationObjectType), + typeof(IActionValidator<>).MakeGenericType(validationObjectType) + ]; + + foreach (var registrationInterface in registrationInterfaces) + { + services.AddScoped(registrationInterface, x => + { + var factory = x.GetRequiredService(); + var validator = factory.GetValidatorInternal(validatorType, getActionType: getActionType); + + return validator; + }); + + ActionValidatorFactory.ValidatorTypeMap[registrationInterface] = validatorType; + } + + return services; + } +} diff --git a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Extensions/AddActionValidatorsExtension.cs b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Extensions/AddActionValidatorsExtension.cs deleted file mode 100644 index f9b43df..0000000 --- a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Extensions/AddActionValidatorsExtension.cs +++ /dev/null @@ -1,96 +0,0 @@ -using BitzArt.EnumToMemberValue; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Reflection; - -namespace FluentValidation; - -public static class AddActionValidatorsExtension -{ - public static IServiceCollection AddActionValidatorsFromAssemblyContaining(this IServiceCollection services, Func? getActionType = null) - => services.AddActionValidatorsFromAssemblyContaining(typeof(TAssemblyPointer), getActionType); - - public static IServiceCollection AddActionValidatorsFromAssemblyContaining(this IServiceCollection services, Type type, Func? getActionType = null) - => services.AddActionValidatorsFromAssembly(type.Assembly, getActionType); - - public static IServiceCollection AddActionValidatorsFromAssembly(this IServiceCollection services, Assembly assembly, Func? getActionType = null) - { - var validators = assembly - .DefinedTypes - .Where(x => x.BaseType is not null && x.BaseType.IsGenericType) - .Where(x => x.BaseType!.GetGenericTypeDefinition() == typeof(ActionValidator<>)); - - foreach (var validator in validators) services.AddActionValidator(validator, getActionType); - - return services; - } - - public static IServiceCollection AddActionValidator(this IServiceCollection services, Type validatorType, Func? getActionType = null) - { - if (validatorType is null) throw new ArgumentException($"{nameof(validatorType)} must not be null"); - if (validatorType.BaseType!.GetGenericTypeDefinition() != typeof(ActionValidator<>)) throw new ArgumentException($"{validatorType.Name} is not assignable to ActionValidator"); - - var baseClassDefinition = validatorType.BaseType!; - var validationObjectType = baseClassDefinition.GetGenericArguments().Single(); - - var registrationType = typeof(IValidator<>).MakeGenericType(validationObjectType); - - services.AddTransient(validatorType); - - if (getActionType is not null) services.AddScoped(registrationType, x => - { - var validator = x.GetRequiredService(validatorType); - (validator as IActionValidator)!.ActionType = getActionType.Invoke(x); - return validator; - }); - - services.AddKeyedForEnum( - registrationType, - x => (IActionValidator)x.GetRequiredService(validatorType), - (ActionType type) => type.ToMemberValue(), - (validator, key) => validator.ActionType = key, - ServiceLifetime.Scoped); - - return services; - } - - private static void AddKeyedForEnum( - this IServiceCollection services, - Type registrationType, - Func implementationFactory, - Func enumStringValueFactory, - Action applyKeyAction, - ServiceLifetime serviceLifetime) - where TService : class - where TEnum : struct, Enum - { - var enumValues = Enum.GetValues(); - - foreach (var enumValue in enumValues) - { - services.Add( - new ServiceDescriptor( - registrationType, - enumValue, - (x, key) => - { - var service = implementationFactory(x); - applyKeyAction(service, enumValue); - return service; - }, - serviceLifetime)); - - services.Add( - new ServiceDescriptor( - registrationType, - enumStringValueFactory(enumValue), - (x, key) => - { - var service = implementationFactory(x); - applyKeyAction(service, enumValue); - return service; - }, - serviceLifetime)); - } - } -} diff --git a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Interfaces/IActionValidator.cs b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Interfaces/IActionValidator.cs index b0d3a47..621f147 100644 --- a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Interfaces/IActionValidator.cs +++ b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Interfaces/IActionValidator.cs @@ -1,6 +1,11 @@ namespace FluentValidation; +public interface IActionValidator : IValidator, IActionValidator +{ + +} + public interface IActionValidator { - public ActionType ActionType { get; set; } + public ActionType? Action { get; internal set; } } diff --git a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Services/ActionValidatorFactory.cs b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Services/ActionValidatorFactory.cs new file mode 100644 index 0000000..a9fa2da --- /dev/null +++ b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Services/ActionValidatorFactory.cs @@ -0,0 +1,55 @@ +using Microsoft.Extensions.DependencyInjection; +using System.Collections.Concurrent; + +namespace FluentValidation; + +internal class ActionValidatorFactory(IServiceProvider serviceProvider) : IActionValidatorFactory +{ + internal static ConcurrentDictionary ValidatorTypeMap = []; + + internal IServiceProvider _serviceProvider = serviceProvider; + + private ActionType? _actionType = null; + + public IActionValidator GetValidator(ActionType actionType) + => (IActionValidator)GetValidatorInternal(typeof(IActionValidator), definedActionType: actionType); + + public IActionValidator GetValidator(Type objectType, ActionType actionType) + => GetValidatorInternal(typeof(IActionValidator<>).MakeGenericType(objectType), definedActionType: actionType); + + public IActionValidator GetValidatorInternal(Type validatorType, Func? actionTypeResolver = null, ActionType? definedActionType = null) + { + bool cleanup = false; + try + { + var implementationType = ValidatorTypeMap[validatorType] + ?? throw new ArgumentException($"{validatorType.Name} is not registered as ActionValidator"); + + if (definedActionType.HasValue) + { + _actionType = definedActionType; + cleanup = true; + } + + var validator = (IActionValidator)_serviceProvider.GetRequiredService(implementationType); + + if (_actionType.HasValue) + { + validator.Action = _actionType!.Value; + return validator; + } + + if (actionTypeResolver is not null) + { + validator.Action = actionTypeResolver(_serviceProvider); + return validator; + } + + return validator; + } + finally + { + if (cleanup) _actionType = null; + } + } +} diff --git a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Services/IActionValidatorFactory.cs b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Services/IActionValidatorFactory.cs new file mode 100644 index 0000000..ee11367 --- /dev/null +++ b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Services/IActionValidatorFactory.cs @@ -0,0 +1,11 @@ + +namespace FluentValidation; + +public interface IActionValidatorFactory +{ + public IActionValidator GetValidator(ActionType actionType); + + public IActionValidator GetValidator(Type objectType, ActionType actionType); + + internal IActionValidator GetValidatorInternal(Type validatorType, Func getActionType, ActionType? actionType = null); +} diff --git a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Validators/ActionValidator.cs b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Validators/ActionValidator.cs index 56a64a1..789a87f 100644 --- a/src/FluentValidation/BitzArt.FluentValidation.Extensions/Validators/ActionValidator.cs +++ b/src/FluentValidation/BitzArt.FluentValidation.Extensions/Validators/ActionValidator.cs @@ -1,20 +1,12 @@ namespace FluentValidation; -public abstract class ActionValidator : AbstractValidator, IActionValidator +public abstract class ActionValidator : AbstractValidator, IActionValidator { - private ActionType? _actionType; - public ActionType ActionType - { - get => _actionType is not null ? - _actionType!.Value : - throw new ArgumentException("ActionType is not configured for this ActionValidator."); - - set => _actionType = value; - } + public ActionType? Action { get; set; } public IConditionBuilder When(ActionType actionType, Action action) - => When(x => ActionType == actionType, action); + => When(x => Action == actionType, action); public IConditionBuilder Unless(ActionType actionType, Action action) - => Unless(x => ActionType == actionType, action); + => Unless(x => Action == actionType, action); } diff --git a/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/ActionValidatorFactoryHierarchicalTests.cs b/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/ActionValidatorFactoryHierarchicalTests.cs new file mode 100644 index 0000000..1144235 --- /dev/null +++ b/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/ActionValidatorFactoryHierarchicalTests.cs @@ -0,0 +1,59 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace FluentValidation; + +public class ActionValidatorFactoryHierarchicalTests +{ + private class TestHierarchyParent + { + public string? Name { get; set; } + + public string? Description { get; set; } + } + + private class TestHierarchyParentValidator : ActionValidator + { + public readonly IActionValidator DescriptionValidator; + + public TestHierarchyParentValidator(IActionValidator descriptionValidator) + { + DescriptionValidator = descriptionValidator; + RuleFor(x => x.Description!).SetValidator(DescriptionValidator); + } + } + + private class TestDescriptionValidator : ActionValidator + { + public TestDescriptionValidator() + { + When(ActionType.Create, () => + { + RuleFor(x => x).NotEmpty().MinimumLength(5); + }); + } + } + + [Fact] + public void GetValidator_OnValidatorHierarchy_ShouldSetActionTypeForAllValidators() + { + // Arrange + var services = new ServiceCollection(); + services.AddActionValidator(); + services.AddActionValidator(); + + var serviceProvider = services.BuildServiceProvider(); + var factory = serviceProvider.GetRequiredService(); + + // Act + var validator = factory.GetValidator(ActionType.Create); + + // Assert + Assert.NotNull(validator); + Assert.True(validator is TestHierarchyParentValidator); + Assert.Equal(ActionType.Create, ((TestHierarchyParentValidator)validator).Action); + var descriptionValidator = ((TestHierarchyParentValidator)validator).DescriptionValidator; + Assert.NotNull(descriptionValidator); + Assert.True(descriptionValidator is TestDescriptionValidator); + Assert.Equal(ActionType.Create, descriptionValidator.Action); + } +} diff --git a/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/ActionValidatorFactoryTests.cs b/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/ActionValidatorFactoryTests.cs new file mode 100644 index 0000000..793ecc9 --- /dev/null +++ b/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/ActionValidatorFactoryTests.cs @@ -0,0 +1,88 @@ +using FluentValidation.Models; +using Microsoft.Extensions.DependencyInjection; + +namespace FluentValidation; + +public class ActionValidatorFactoryTests +{ + [Theory] + [InlineData(ActionType.Get)] + [InlineData(ActionType.Create)] + [InlineData(ActionType.Update)] + [InlineData(ActionType.Patch)] + [InlineData(ActionType.Options)] + [InlineData(ActionType.Delete)] + public void GetValidatorInternal_OnDefinedActionValidator_ShouldReturnValidator(ActionType actionType) + { + // Arrange + IServiceCollection services = new ServiceCollection(); + var validatorType = typeof(TestEntityValidator); + + // Act + services.AddActionValidator(validatorType); + + // Assert + var serviceProvider = services.BuildServiceProvider(); + var factory = serviceProvider.GetRequiredService(); + + var validator = factory.GetValidatorInternal(validatorType, (_) => null, actionType: actionType); + + Assert.NotNull(validator); + Assert.True(validator is TestEntityValidator); + Assert.Equal(actionType, ((TestEntityValidator)validator).Action); + } + + [Theory] + [InlineData(ActionType.Get)] + [InlineData(ActionType.Create)] + [InlineData(ActionType.Update)] + [InlineData(ActionType.Patch)] + [InlineData(ActionType.Options)] + [InlineData(ActionType.Delete)] + public void GetValidatorReflexion_OnDefinedActionValidator_ShouldReturnValidator(ActionType actionType) + { + // Arrange + IServiceCollection services = new ServiceCollection(); + var validatorType = typeof(TestEntityValidator); + + // Act + services.AddActionValidator(validatorType); + + // Assert + var serviceProvider = services.BuildServiceProvider(); + var factory = serviceProvider.GetRequiredService(); + + var validator = factory.GetValidator(typeof(TestEntity), actionType); + + Assert.NotNull(validator); + Assert.True(validator is TestEntityValidator); + Assert.Equal(actionType, ((TestEntityValidator)validator).Action); + } + + [Theory] + [InlineData(ActionType.Get)] + [InlineData(ActionType.Create)] + [InlineData(ActionType.Update)] + [InlineData(ActionType.Patch)] + [InlineData(ActionType.Options)] + [InlineData(ActionType.Delete)] + public void GetValidatorGeneric_OnDefinedActionValidator_ShouldReturnValidator(ActionType actionType) + { + // Arrange + IServiceCollection services = new ServiceCollection(); + var validatorType = typeof(TestEntityValidator); + + // Act + services.AddActionValidator(validatorType); + + // Assert + var serviceProvider = services.BuildServiceProvider(); + var factory = serviceProvider.GetRequiredService(); + + var validator = factory.GetValidator(actionType); + + Assert.NotNull(validator); + Assert.True(validator is TestEntityValidator); + Assert.Equal(actionType, ((TestEntityValidator)validator).Action); + } +} diff --git a/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/AddActionValidatorsExtensionTests.cs b/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/AddActionValidatorExtensionsTests.cs similarity index 51% rename from tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/AddActionValidatorsExtensionTests.cs rename to tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/AddActionValidatorExtensionsTests.cs index 341efa4..6f7d7aa 100644 --- a/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/AddActionValidatorsExtensionTests.cs +++ b/tests/FluentValidation/BitzArt.FluentValidation.Extensions.UnitTests/Tests/AddActionValidatorExtensionsTests.cs @@ -1,10 +1,9 @@ -using BitzArt.EnumToMemberValue; using FluentValidation.Models; using Microsoft.Extensions.DependencyInjection; namespace FluentValidation; -public class AddActionValidatorsExtensionTests +public class AddActionValidatorExtensionsTests { [Theory] [InlineData(ActionType.Get)] @@ -25,7 +24,7 @@ public void AddActionValidator_WithFlatActionType_AddsValidator(ActionType actio Assert.NotNull(validator); Assert.True(validator is TestEntityValidator); var validatorCasted = (TestEntityValidator)validator; - Assert.Equal(actionType, validatorCasted.ActionType); + Assert.Equal(actionType, validatorCasted.Action); } [Theory] @@ -49,7 +48,7 @@ public void AddActionValidator_WithActionTypeFromDi_AddsValidator(ActionType act Assert.NotNull(validator); Assert.True(validator is TestEntityValidator); var validatorCasted = (TestEntityValidator)validator; - Assert.Equal(actionType, validatorCasted.ActionType); + Assert.Equal(actionType, validatorCasted.Action); } [Fact] @@ -59,7 +58,7 @@ public void AddActionValidatorsFromAssembly_WithFlatActionType_AddsTestEntityVal var validatorType = typeof(TestEntityValidator); var actionType = ActionType.Get; - services.AddActionValidatorsFromAssembly(typeof(AddActionValidatorsExtensionTests).Assembly, x => actionType); + services.AddActionValidatorsFromAssembly(typeof(AddActionValidatorExtensionsTests).Assembly, x => actionType); var serviceProvider = services.BuildServiceProvider(); @@ -68,7 +67,7 @@ public void AddActionValidatorsFromAssembly_WithFlatActionType_AddsTestEntityVal Assert.NotNull(validator); Assert.True(validator is TestEntityValidator); var validatorCasted = (TestEntityValidator)validator; - Assert.Equal(actionType, validatorCasted.ActionType); + Assert.Equal(actionType, validatorCasted.Action); } [Fact] @@ -78,7 +77,7 @@ public void AddActionValidatorsFromAssemblyContaining_WithFlatActionType_AddsTes var validatorType = typeof(TestEntityValidator); var actionType = ActionType.Get; - services.AddActionValidatorsFromAssemblyContaining(typeof(AddActionValidatorsExtensionTests), x => actionType); + services.AddActionValidatorsFromAssemblyContaining(typeof(AddActionValidatorExtensionsTests), x => actionType); var serviceProvider = services.BuildServiceProvider(); @@ -87,7 +86,7 @@ public void AddActionValidatorsFromAssemblyContaining_WithFlatActionType_AddsTes Assert.NotNull(validator); Assert.True(validator is TestEntityValidator); var validatorCasted = (TestEntityValidator)validator; - Assert.Equal(actionType, validatorCasted.ActionType); + Assert.Equal(actionType, validatorCasted.Action); } [Fact] @@ -97,7 +96,7 @@ public void AddActionValidatorsFromAssemblyContainingGeneric_WithFlatActionType_ var validatorType = typeof(TestEntityValidator); var actionType = ActionType.Get; - services.AddActionValidatorsFromAssemblyContaining(x => actionType); + services.AddActionValidatorsFromAssemblyContaining(x => actionType); var serviceProvider = services.BuildServiceProvider(); @@ -106,83 +105,6 @@ public void AddActionValidatorsFromAssemblyContainingGeneric_WithFlatActionType_ Assert.NotNull(validator); Assert.True(validator is TestEntityValidator); var validatorCasted = (TestEntityValidator)validator; - Assert.Equal(actionType, validatorCasted.ActionType); - } - - [Theory] - [InlineData(ActionType.Get)] - [InlineData(ActionType.Create)] - [InlineData(ActionType.Update)] - [InlineData(ActionType.Patch)] - [InlineData(ActionType.Options)] - [InlineData(ActionType.Delete)] - public void AddActionValidator_WithoutSpecifyingActionType_ValidatorAccessibleByActionType(ActionType resolveKey) - { - // Arrange - IServiceCollection services = new ServiceCollection(); - var validatorType = typeof(TestEntityValidator); - - // Act - services.AddActionValidator(validatorType); - - // Assert - var serviceProvider = services.BuildServiceProvider(); - var validator = serviceProvider.GetRequiredKeyedService>(resolveKey); - - Assert.NotNull(validator); - Assert.True(validator is TestEntityValidator); - Assert.Equal(resolveKey, ((TestEntityValidator)validator).ActionType); - } - - [Theory] - [InlineData(ActionType.Get)] - [InlineData(ActionType.Create)] - [InlineData(ActionType.Update)] - [InlineData(ActionType.Patch)] - [InlineData(ActionType.Options)] - [InlineData(ActionType.Delete)] - public void AddActionValidator_WithActionType_ValidatorStillAccessibleByActionType(ActionType resolveKey) - { - // Arrange - IServiceCollection services = new ServiceCollection(); - var validatorType = typeof(TestEntityValidator); - - // Act - services.AddActionValidator(validatorType, x => default); - - // Assert - var serviceProvider = services.BuildServiceProvider(); - var validator = serviceProvider.GetRequiredKeyedService>(resolveKey); - - Assert.NotNull(validator); - Assert.True(validator is TestEntityValidator); - Assert.Equal(resolveKey, ((TestEntityValidator)validator).ActionType); - } - - [Theory] - [InlineData(ActionTypes.Get)] - [InlineData(ActionTypes.Create)] - [InlineData(ActionTypes.Update)] - [InlineData(ActionTypes.Patch)] - [InlineData(ActionTypes.Options)] - [InlineData(ActionTypes.Delete)] - public void AddActionValidator_WithoutSpecifyingActionType_ValidatorAccessibleByActionTypeEnumMemberValue(string resolveKey) - { - // Arrange - IServiceCollection services = new ServiceCollection(); - var validatorType = typeof(TestEntityValidator); - - // Act - services.AddActionValidator(validatorType); - - // Assert - var serviceProvider = services.BuildServiceProvider(); - var validator = serviceProvider.GetRequiredKeyedService>(resolveKey); - - var expectedActionType = resolveKey.ToEnum(); - - Assert.NotNull(validator); - Assert.True(validator is TestEntityValidator); - Assert.Equal(expectedActionType, ((TestEntityValidator)validator).ActionType); + Assert.Equal(actionType, validatorCasted.Action); } } \ No newline at end of file