-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
150 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentValidation; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace FairyBread | ||
{ | ||
public class DefaultValidatorRegistry : IValidatorRegistry | ||
{ | ||
private static readonly Type _hasOwnScopeInterfaceType = typeof(IRequiresOwnScopeValidator); | ||
|
||
public DefaultValidatorRegistry(IServiceCollection services, IFairyBreadOptions options) | ||
{ | ||
var validatorResults = new List<AssemblyScanner.AssemblyScanResult>(); | ||
var objectValidatorInterface = typeof(IValidator<object>); | ||
var underlyingValidatorType = objectValidatorInterface.GetGenericTypeDefinition().UnderlyingSystemType; | ||
|
||
foreach (var service in services) | ||
{ | ||
if (!service.ServiceType.IsGenericType || | ||
service.ServiceType.Name != objectValidatorInterface.Name || | ||
service.ServiceType.GetGenericTypeDefinition() != underlyingValidatorType) | ||
{ | ||
continue; | ||
} | ||
|
||
validatorResults.Add( | ||
new AssemblyScanner.AssemblyScanResult( | ||
service.ServiceType, | ||
service.ImplementationType)); | ||
} | ||
|
||
if (!validatorResults.Any() && options.ThrowIfNoValidatorsFound) | ||
{ | ||
throw new Exception($"No validators were found by FairyBread. " + | ||
$"Ensure you're registering your FluentValidation validators for DI."); | ||
} | ||
|
||
foreach (var validatorResult in validatorResults) | ||
{ | ||
var validatorType = validatorResult.ValidatorType; | ||
|
||
var validatedType = validatorResult.InterfaceType.GenericTypeArguments.Single(); | ||
if (!Cache.TryGetValue(validatedType, out var validatorsForType)) | ||
{ | ||
Cache[validatedType] = validatorsForType = new List<ValidatorDescriptor>(); | ||
} | ||
|
||
var requiresOwnScope = ShouldBeResolvedInOwnScope(validatorType); | ||
|
||
var validatorDescriptor = new ValidatorDescriptor(validatorType, requiresOwnScope); | ||
|
||
validatorsForType.Add(validatorDescriptor); | ||
} | ||
} | ||
|
||
public Dictionary<Type, List<ValidatorDescriptor>> Cache { get; } = new(); | ||
|
||
public bool ShouldBeResolvedInOwnScope(Type validatorType) | ||
=> _hasOwnScopeInterfaceType.IsAssignableFrom(validatorType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
using HotChocolate.Resolvers; | ||
using System.Collections.Generic; | ||
using HotChocolate.Resolvers; | ||
using HotChocolate.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace FairyBread | ||
{ | ||
public interface IValidatorProvider | ||
{ | ||
IEnumerable<ResolvedValidator> GetValidators(IMiddlewareContext context, IInputField argument); | ||
|
||
bool ShouldBeResolvedInOwnScope(Type validatorType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace FairyBread | ||
{ | ||
public interface IValidatorRegistry | ||
{ | ||
Dictionary<Type, List<ValidatorDescriptor>> Cache { get; } | ||
|
||
bool ShouldBeResolvedInOwnScope(Type validatorType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
|
||
namespace FairyBread | ||
{ | ||
/// <summary> | ||
/// Description of a validator configuration to be stored in the validator descriptor cache. | ||
/// </summary> | ||
public class ValidatorDescriptor | ||
{ | ||
/// <summary> | ||
/// Type of the validator. | ||
/// </summary> | ||
public Type ValidatorType { get; } | ||
|
||
/// <summary> | ||
/// Does the validator inherit <see cref="IRequiresOwnScopeValidator"/>? | ||
/// If so, this means it should be resolved from the service provider in it's own scope. | ||
/// </summary> | ||
public bool RequiresOwnScope { get; } | ||
|
||
/// <summary> | ||
/// Instantiates a new <see cref="ValidatorDescriptor"/>. | ||
/// </summary> | ||
/// <param name="validatorType">The validator.</param> | ||
public ValidatorDescriptor(Type validatorType, bool requiresOwnScope) | ||
{ | ||
ValidatorType = validatorType; | ||
RequiresOwnScope = requiresOwnScope; | ||
} | ||
} | ||
} |