(Best Practice) FluentValidation and Checking Dupl

2020-08-17 06:28发布

问题:

I have a table (junction table) with 3 primary keys so when I want to check duplicate records , I must check 3 properties together

I wrote a method like this

    private bool IsDuplicate(long roleId, long componentGroupId, long operationId)
    {
        var business = new RoleGroupBusiness();
        var result = business.Where(x => x.RoleID == roleId && x.ComponentGroupID == componentGroupId && x.OperationID == operationId).Any();
        return result;
    }

and I have a FluentValidator class like this :

public class RoleGroupValidator : AbstractValidator<RoleGroup>
{

    private bool IsDuplicate(long roleId, long componentGroupId, long operationId)
    {
        var business = new RoleGroupBusiness();
        var result = business.Where(x => x.RoleID == roleId && x.ComponentGroupID == componentGroupId && x.OperationID == operationId).Any();
        return result;
    }
    public RoleGroupValidator()
    {
        RuleFor(x => x.RoleID).NotNull().WithMessage("A");
        RuleFor(x => x.ComponentGroupID).NotNull().WithMessage("A");
        RuleFor(x => x.OperationID).NotNull().WithMessage("A");    
    }

}

1) How can i use IsDuplicate methid in FluentValidator ?

or

2) What is best way for checking entity is Duplicate or not in fluentValidation Library ?

RuleFor only get me value of one of properties but I need value of all properties for passing to my method

回答1:

You should use Must method:

public class RoleGroupValidator : AbstractValidator<RoleGroup>
{
    public RoleGroupValidator()
    {
        RuleFor(x => x.RoleID).NotNull().WithMessage("A");
        RuleFor(x => x.ComponentGroupID).NotNull().WithMessage("A");
        RuleFor(x => x.OperationID).NotNull().WithMessage("A");
        RuleFor(x => x).Must(x => !IsDuplicate(x));
    }

    private bool IsDuplicate(RoleGroup r)
    {
        var business = new RoleGroupBusiness();
        return business.Any(x => x.RoleID == r.RoleID &&
                                 x.ComponentGroupID == r.ComponentGroupID &&
                                 x.OperationID == r.OperationID);
    }
}

Don't forget to dispose RoleGroupBusiness instance if it's disposable(it probably is):

private bool IsDuplicate(RoleGroup r)
{
    using (var business = new RoleGroupBusiness())
    {
        return business.Any(x => x.RoleID == r.RoleID &&
                             x.ComponentGroupID == r.ComponentGroupID &&
                             x.OperationID == r.OperationID);
    }
}


回答2:

This code has not been tested, but I've done something similar, Using a dummy value in the method that is never used for anything.

private bool NotBeDuplicate(RoleGroup r, long dummy)
{
  var business = new RoleGroupBusiness();
  var result = business.Where(x => x.RoleID == r.RoleID && x.ComponentGroupID == r.ComponentGroupID && x.OperationID == r.OperationID).Any();
  return !result;
}

public RoleGroupValidator()
{
  RuleFor(x => x.RoleID).Must(NotBeDuplicate);
}

Hopefully that points you in the right direction.