Fluent验证程序,以检查数据库中是否存在具有ID的实体

问题描述 投票:1回答:2

我正在尝试编写一个自定义验证器,该验证器将使用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);
        }
    }
servicestack fluentvalidation
2个回答
2
投票

您需要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));

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.