如何在流畅的验证中按实体类型注入或查找验证器?
How to inject or find validaters by entity type in fluent validation?
如何在流畅的验证中按实体类型注入或查找验证器?
我关注 类 并希望通过流畅的验证来验证实体
public class BaseEntity {}
public class Article :BaseEntity
{
public string Name {get;set;}
}
public class ArticleValidator : AbstractValidator<Article>
{
public ArticleValidator()
{
RuleFor(x => x.Name ).NotEmpty().Length(0,512);
}
}
并且有 BaseEntity 的扩展:
public static ValidationResult Validate(this BaseEntity entity)
{
//????and here how i can find entity validator by type of entity and validate it and return result.
}
public class ArticleService
{
public void AddArticle(Article aricle)
{
var result = article.Validate();
if(result.IsValid)
;.......
}
}
你需要这样的东西
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentValidation;
using FluentValidation.Results;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
var c = new MyClass();
var result = Validate(c);
}
public static ValidationResult Validate(object c)
{
if (c == null) throw new ArgumentNullException("c");
var vt = typeof (AbstractValidator<>);
var et = c.GetType();
var evt = vt.MakeGenericType(et);
var validatorType = FindValidatorType(Assembly.GetExecutingAssembly(), evt);
var validatorInstance = (IValidator)Activator.CreateInstance(validatorType);
return validatorInstance.Validate(c);
}
public static Type FindValidatorType(Assembly assembly, Type evt)
{
if (assembly == null) throw new ArgumentNullException("assembly");
if (evt == null) throw new ArgumentNullException("evt");
return assembly.GetTypes().FirstOrDefault(t => t.IsSubclassOf(evt));
}
}
public class MyClassValidator : AbstractValidator<MyClass>
{
}
public class MyClass
{
}
}
确保您的具体验证器至少有一个无参数构造函数。
祝你好运
public class ValidationAspect : MethodInterception
{
private Type _validatorType;
public ValidationAspect(Type validatorType)
{
if (!typeof(IValidator).IsAssignableFrom(validatorType))
{
throw new System.Exception("This is not a validation class.");
}
_validatorType = validatorType;
}
protected override void OnBefore(IInvocation invocation)
{
var validator = (IValidator)Activator.CreateInstance(_validatorType);
var entityType = _validatorType.BaseType.GetGenericArguments()[0];
var entities = invocation.Arguments.Where(t => t.GetType() == entityType);
foreach (var entity in entities)
{
ValidationTool.Validate(validator, entity);
}
}
}
public class BrandInsertDtoValidator : AbstractValidator<BrandInsertDto>
{
public BrandInsertDtoValidator()
{
RuleFor(p => p.Name).NotNull();
RuleFor(p => p.Name).NotEmpty();
RuleFor(p => p.Name).MaximumLength(50);
RuleFor(p => p.Name.Length).GreaterThanOrEqualTo(2);
}
}
public class BrandInsertDto : IDTO
{
public string Name { get; set; }
}
如何在流畅的验证中按实体类型注入或查找验证器? 我关注 类 并希望通过流畅的验证来验证实体
public class BaseEntity {}
public class Article :BaseEntity
{
public string Name {get;set;}
}
public class ArticleValidator : AbstractValidator<Article>
{
public ArticleValidator()
{
RuleFor(x => x.Name ).NotEmpty().Length(0,512);
}
}
并且有 BaseEntity 的扩展:
public static ValidationResult Validate(this BaseEntity entity)
{
//????and here how i can find entity validator by type of entity and validate it and return result.
}
public class ArticleService
{
public void AddArticle(Article aricle)
{
var result = article.Validate();
if(result.IsValid)
;.......
}
}
你需要这样的东西
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentValidation;
using FluentValidation.Results;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
var c = new MyClass();
var result = Validate(c);
}
public static ValidationResult Validate(object c)
{
if (c == null) throw new ArgumentNullException("c");
var vt = typeof (AbstractValidator<>);
var et = c.GetType();
var evt = vt.MakeGenericType(et);
var validatorType = FindValidatorType(Assembly.GetExecutingAssembly(), evt);
var validatorInstance = (IValidator)Activator.CreateInstance(validatorType);
return validatorInstance.Validate(c);
}
public static Type FindValidatorType(Assembly assembly, Type evt)
{
if (assembly == null) throw new ArgumentNullException("assembly");
if (evt == null) throw new ArgumentNullException("evt");
return assembly.GetTypes().FirstOrDefault(t => t.IsSubclassOf(evt));
}
}
public class MyClassValidator : AbstractValidator<MyClass>
{
}
public class MyClass
{
}
}
确保您的具体验证器至少有一个无参数构造函数。 祝你好运
public class ValidationAspect : MethodInterception
{
private Type _validatorType;
public ValidationAspect(Type validatorType)
{
if (!typeof(IValidator).IsAssignableFrom(validatorType))
{
throw new System.Exception("This is not a validation class.");
}
_validatorType = validatorType;
}
protected override void OnBefore(IInvocation invocation)
{
var validator = (IValidator)Activator.CreateInstance(_validatorType);
var entityType = _validatorType.BaseType.GetGenericArguments()[0];
var entities = invocation.Arguments.Where(t => t.GetType() == entityType);
foreach (var entity in entities)
{
ValidationTool.Validate(validator, entity);
}
}
}
public class BrandInsertDtoValidator : AbstractValidator<BrandInsertDto>
{
public BrandInsertDtoValidator()
{
RuleFor(p => p.Name).NotNull();
RuleFor(p => p.Name).NotEmpty();
RuleFor(p => p.Name).MaximumLength(50);
RuleFor(p => p.Name.Length).GreaterThanOrEqualTo(2);
}
}
public class BrandInsertDto : IDTO
{
public string Name { get; set; }
}