-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Liero/feature/v1.1
Feature/v1.1
- Loading branch information
Showing
6 changed files
with
184 additions
and
94 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
52 changes: 52 additions & 0 deletions
52
vNext.BlazorComponents.FluentValidation/AssemblyScannerValidatorFactory.cs
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,52 @@ | ||
#nullable enable | ||
|
||
using FluentValidation; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace vNext.BlazorComponents.FluentValidation | ||
{ | ||
public class AssemblyScannerValidatorFactory : IValidatorFactory | ||
{ | ||
static readonly List<string> ScannedAssembly = new(); | ||
static readonly List<AssemblyScanner.AssemblyScanResult> AssemblyScanResults = new(); | ||
|
||
public IValidator? CreateValidator(ValidatorFactoryContext context) | ||
{ | ||
|
||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(i => i.FullName is not null && !ScannedAssembly.Contains(i.FullName))) | ||
{ | ||
try | ||
{ | ||
AssemblyScanResults.AddRange(AssemblyScanner.FindValidatorsInAssembly(assembly, false)); | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
|
||
ScannedAssembly.Add(assembly.FullName!); | ||
} | ||
|
||
|
||
Type modelType = context.Model.GetType(); | ||
|
||
static int CommonPrefixLength(string? str1, string? str2) => | ||
(str1 ?? string.Empty).TakeWhile((c, i) => str2?.Length < i && c == str2[c]).Count(); | ||
|
||
|
||
Type? modelValidatorType = AssemblyScanResults.Where(i => context.ValidatorType.IsAssignableFrom(i.InterfaceType)) | ||
.OrderByDescending(e => e.ValidatorType.Assembly == modelType.Assembly) //prefer current assebly | ||
.ThenByDescending(e => CommonPrefixLength(e.ValidatorType.FullName, modelType.FullName)) //prefer similar namespace | ||
.ThenBy(e => e.ValidatorType.Namespace?.Length) | ||
.FirstOrDefault()?.ValidatorType; | ||
|
||
if (modelValidatorType != null) | ||
{ | ||
return (IValidator)ActivatorUtilities.CreateInstance(context.ServiceProvider, modelValidatorType); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
51 changes: 11 additions & 40 deletions
51
vNext.BlazorComponents.FluentValidation/DefaultValidatorFactory.cs
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,64 +1,35 @@ | ||
#nullable enable | ||
|
||
using FluentValidation; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace vNext.BlazorComponents.FluentValidation | ||
{ | ||
|
||
/// <summary> | ||
/// Returns validator from ServiceProvider or by scanning assemlies | ||
/// </summary> | ||
public class DefaultValidatorFactory : IValidatorFactory | ||
{ | ||
static readonly List<string> ScannedAssembly = new(); | ||
static readonly List<AssemblyScanner.AssemblyScanResult> AssemblyScanResults = new(); | ||
AssemblyScannerValidatorFactory? _assemblyScannerValidatorFactory; | ||
ServiceProviderValidatorFactory? _serviceProviderValidatorFactory; | ||
|
||
public bool DisableAssemblyScanning { get; set; } | ||
public bool DisableServiceProvider { get; set; } | ||
|
||
public IValidator? CreateValidator(ValidatorFactoryContext context) | ||
{ | ||
if (context.ServiceProvider.GetService(context.ValidatorType) is IValidator validator) | ||
IValidator? result = null; | ||
if (!DisableServiceProvider) | ||
{ | ||
return validator; | ||
_serviceProviderValidatorFactory ??= new(); | ||
result = _serviceProviderValidatorFactory.CreateValidator(context); | ||
} | ||
if (!DisableAssemblyScanning) | ||
{ | ||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(i => i.FullName is not null && !ScannedAssembly.Contains(i.FullName))) | ||
{ | ||
try | ||
{ | ||
AssemblyScanResults.AddRange(AssemblyScanner.FindValidatorsInAssembly(assembly, false)); | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
|
||
ScannedAssembly.Add(assembly.FullName!); | ||
} | ||
|
||
|
||
Type modelType = context.Model.GetType(); | ||
|
||
static int CommonPrefixLength(string? str1, string? str2) => | ||
(str1 ?? string.Empty).TakeWhile((c, i) => str2?.Length < i && c == str2[c]).Count(); | ||
|
||
|
||
Type? modelValidatorType = AssemblyScanResults.Where(i => context.ValidatorType.IsAssignableFrom(i.InterfaceType)) | ||
.OrderByDescending(e => e.ValidatorType.Assembly == modelType.Assembly) //prefer current assebly | ||
.ThenByDescending(e => CommonPrefixLength(e.ValidatorType.FullName, modelType.FullName)) //prefer similar namespace | ||
.ThenBy(e => e.ValidatorType.Namespace?.Length) | ||
.FirstOrDefault()?.ValidatorType; | ||
|
||
if (modelValidatorType != null) | ||
{ | ||
return (IValidator)ActivatorUtilities.CreateInstance(context.ServiceProvider, modelValidatorType); | ||
} | ||
_assemblyScannerValidatorFactory ??= new(); | ||
result ??= _assemblyScannerValidatorFactory.CreateValidator(context); | ||
} | ||
|
||
return null; | ||
return result; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.