mirror of https://github.com/abpframework/abp.git
13 changed files with 151 additions and 119 deletions
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using FluentValidation; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public class AbpFluentValidationConventionalRegistrar : DefaultConventionalRegistrar |
|||
{ |
|||
public override void AddType(IServiceCollection services, Type type) |
|||
{ |
|||
if (typeof(IValidator).IsAssignableFrom(type)) |
|||
{ |
|||
var dtoType = GetFirstGenericArgumentOrNull(type, 1); |
|||
if (dtoType != null) |
|||
{ |
|||
var serverType = typeof(IValidator<>).MakeGenericType(dtoType); |
|||
var serviceDescriptor = ServiceDescriptor.Describe(serverType, type, ServiceLifetime.Transient); |
|||
|
|||
services.Add(serviceDescriptor); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private static Type GetFirstGenericArgumentOrNull(Type type, int depth) |
|||
{ |
|||
const int maxFindDepth = 8; |
|||
|
|||
if (depth >= maxFindDepth) |
|||
{ |
|||
return null; |
|||
} |
|||
if (type.IsGenericType && type.GetGenericArguments().Length >= 1) |
|||
{ |
|||
return type.GetGenericArguments()[0]; |
|||
} |
|||
|
|||
return GetFirstGenericArgumentOrNull(type.BaseType, depth + 1); |
|||
} |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public static class AbpFluentValidationCrossCuttingConcern |
|||
{ |
|||
public const string FluentValidation = "AbpFluentValidation"; |
|||
} |
|||
} |
|||
@ -1,13 +1,23 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
[DependsOn(typeof(AbpValidationModule))] |
|||
public class AbpFluentValidationModule : AbpModule |
|||
{ |
|||
public override void PreConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.OnRegistred(FluentValidationInterceptorRegistrar.RegisterIfNeeded); |
|||
context.Services.AddConventionalRegistrar(new AbpFluentValidationConventionalRegistrar()); |
|||
} |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
Configure<AbpValidationOptions>(options => |
|||
{ |
|||
options.ValidationContributor.Add<FluentMethodInvocationValidator>(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,21 +1,30 @@ |
|||
using System; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.Linq; |
|||
using FluentValidation; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public class FluentValidator : IFluentValidator, ITransientDependency |
|||
public class FluentMethodInvocationValidator : IMethodInvocationValidator, ITransientDependency |
|||
{ |
|||
private readonly IServiceProvider _serviceProvider; |
|||
|
|||
public FluentMethodInvocationValidator(IServiceProvider serviceProvider) |
|||
{ |
|||
_serviceProvider = serviceProvider; |
|||
} |
|||
|
|||
public void Validate(MethodInvocationValidationContext context) |
|||
{ |
|||
var validationResult = new AbpValidationResult(); |
|||
|
|||
foreach (var parameterValue in context.ParameterValues) |
|||
{ |
|||
if (parameterValue is IValidator validator) |
|||
var serverType = typeof(IValidator<>).MakeGenericType(parameterValue.GetType()); |
|||
|
|||
if (_serviceProvider.GetService(serverType) is IValidator validator) |
|||
{ |
|||
var result = validator.Validate(parameterValue); |
|||
if (!result.IsValid) |
|||
@ -1,53 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Aspects; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.DynamicProxy; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public class FluentValidationInterceptor : AbpInterceptor, ITransientDependency |
|||
{ |
|||
private readonly IFluentValidator _fluentValidator; |
|||
|
|||
public FluentValidationInterceptor(IFluentValidator fluentValidator) |
|||
{ |
|||
_fluentValidator = fluentValidator; |
|||
} |
|||
|
|||
public override void Intercept(IAbpMethodInvocation invocation) |
|||
{ |
|||
if (AbpCrossCuttingConcerns.IsApplied(invocation.TargetObject, AbpFluentValidationCrossCuttingConcern.FluentValidation)) |
|||
{ |
|||
invocation.Proceed(); |
|||
return; |
|||
} |
|||
|
|||
Validate(invocation); |
|||
|
|||
invocation.Proceed(); |
|||
} |
|||
|
|||
public override async Task InterceptAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
if (AbpCrossCuttingConcerns.IsApplied(invocation.TargetObject, AbpFluentValidationCrossCuttingConcern.FluentValidation)) |
|||
{ |
|||
await invocation.ProceedAsync(); |
|||
return; |
|||
} |
|||
|
|||
Validate(invocation); |
|||
|
|||
await invocation.ProceedAsync(); |
|||
} |
|||
|
|||
protected virtual void Validate(IAbpMethodInvocation invocation) |
|||
{ |
|||
_fluentValidator.Validate(new MethodInvocationValidationContext( |
|||
invocation.TargetObject, |
|||
invocation.Method, |
|||
invocation.Arguments |
|||
)); |
|||
} |
|||
} |
|||
} |
|||
@ -1,16 +0,0 @@ |
|||
using FluentValidation; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public static class FluentValidationInterceptorRegistrar |
|||
{ |
|||
public static void RegisterIfNeeded(IOnServiceRegistredContext context) |
|||
{ |
|||
if (typeof(IValidator).IsAssignableFrom(context.ImplementationType)) |
|||
{ |
|||
context.Interceptors.TryAdd<FluentValidationInterceptor>(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,12 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public interface IFluentValidator |
|||
{ |
|||
void Validate(MethodInvocationValidationContext context); |
|||
} |
|||
} |
|||
@ -1,7 +1,7 @@ |
|||
namespace Volo.Abp.Validation |
|||
namespace Volo.Abp.Validation |
|||
{ |
|||
public interface IMethodInvocationValidator |
|||
{ |
|||
void Validate(MethodInvocationValidationContext context); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue