流畅的验证器检查数据库中是否存在具有 ID 的实体

Fluent validator to check if entity with ID exists in database

我正在尝试使用 OrmLite 编写一个自定义验证器来检查数据库中是否存在实体。问题是 IRuleBuilder 的类型参数不能再从用法中推断出来。

我必须这样写方法调用:

RuleFor(r => r.Id).Exists<DtoName, int, EntityName>()

但是我想这样写:

Rulefor(r => r.Id).Exists<EntityName>()

发生这种情况是因为 IRuleBuilder 有两个类型参数并且该方法是扩展方法。有没有一种聪明、流畅的方式来设计这个并使函数调用更像第二个版本?

这是我的扩展方法和验证器的代码:

    public static class AbstractValidatorExtensions
    {
        public static IRuleBuilderOptions<T, TProperty> Exists<T, TProperty, U>(this IRuleBuilder<T, TProperty> ruleBuilder)
        {
            return ruleBuilder.SetValidator(new EntityExistsValidator<U>());
        }                
    }

    public class EntityExistsValidator<T> : PropertyValidator
    {
        public EntityExistsValidator() : base("Entity does not exist") {}

        protected override bool IsValid(PropertyValidatorContext context)
        {
            return HostContext.Resolve<Repository>()
                .Exists<T>((int)context.PropertyValue);
        }
    }

您需要 Custom Validator 进行自定义验证以访问依赖项,例如:

RuleFor(x => x.Id)
    .Must(id =>
    {
        using (var db = HostContext.AppHost.GetDbConnection(base.Request))
        {
            return !db.Exists<EntityName>(x => x.Id == id);
        }
    })
    .WithErrorCode("AlreadyExists")
    .WithMessage("...");

我也会考虑只进行在您的服务中使用依赖项的验证:

if (Db.Exists<EntityName>(x => x.Id == request.Id))
    throw new ArgumentException("Already Exists", nameof(request.Id));

我对 FluentValidation 的体验是,您试图将越来越多的逻辑推送到验证器中。我不会这样做,因为它增加了太多的复杂性。我的经验法则是仅验证离散 属性 值。示例:我将只使用 FluentValidation 来检查 属性 int Id 是否为 0 或大于 0。检查实体是否已经存在我将转移到另一个服务(通常称为“业务逻辑”)。