简体   繁体   English

FluentValidation通过ID验证对象

[英]FluentValidation Validate object by id

Using FluentValidation I typically pass Ids vs the objects in Commands. 我通常使用FluentValidation在Commands中传递Ids与对象。 Yet I want to validate that an object with the id exists, and then validate that object. 但是我想验证具有id的对象是否存在,然后验证该对象。

Is there a better way than the below method? 有没有比下面的方法更好的方法?

    public class ProjectIdValidator : AbstractValidator<int>
    {

        public ProjectIdValidator(MyDbContext aDbContext)
        {
            Custom(aProjectId =>
            {
                var project = aDbContext.Projects.Find(aProjectId);
                if (project == null)
                {
                    return new ValidationFailure("ProjectId", "Project does not exist");
                }
                var projectValidator = new ProjectValidator();
                var x = projectValidator.Validate(project);
                if (x.IsValid)
                {
                    return null;
                }

                return new ValidationFailure("ProjectId", x.Errors.ToString());

            });
        }
    }

What about something like this? 那这样的东西呢?

public class UpdateProjectCommandValidator : AbstractValidator<UpdateProjectCommand>
{
    public UpdateProjectCommandValidator(DbContext dbContext) {
        RuleFor(x => x.Id)
            .MustFindProjectById(dbContext);
    }
}

Then your MustFindProjectById extension method would look something like this: 然后,您的MustFindProjectById扩展方法将如下所示:

public static class MustFindProjectByIdExtensions {
    public static IRuleBuilderOptions<T, int> MustFindProjectById<T>
        (this IRuleBuilder<T, int> ruleBuilder, DbContext dbContext) {
        return ruleBuilder.SetValidator(new MustFindProjectById(dbContext));
    }
}

...and it is just a wrapper around a custom PropertyValidator : ...这只是一个自定义PropertyValidator的包装器:

internal class MustFindProjectById : PropertyValidator {
    private readonly DbContext _dbContext;
    internal MustFindProjectById(DbContext dbContext) {
        _dbContext = dbContext;
    }

    protected override IsValid(PropertyValidatorContext) {
        var projectId = (int)context.PropertyValue;
        var entity = dbContext.Projects.Find(projectId);
        return entity != null;
    }
}

After some research I think this is a better way. 经过研究,我认为这是一种更好的方法。

AddRule(new DelegateValidator<Command>((aCommand, aContext) =>
{
    // return an IEnumerable<ValidationFailure> from in here:
    var projectId = (int?) aCommand.ProjectId;
    if (projectId.HasValue)
    {

        using (var dbContextScope = aDbContextScopeFactory.CreateReadOnly())
        {
            MyDbContext dbContext = dbContextScope.DbContexts.Get<MyDbContext>();
            Project project = dbContext.Projects.Find(projectId);
            if (project == null)
            {
                return Enumerable.Repeat(new ValidationFailure("ProjectId", "ProjectId not found"),1);
            }

            var projectValidator = new ProjectValidator();
            var results = projectValidator.Validate(project);

            return results.Errors;
        }
    }
    return null;
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM