(Best Practice) FluentValidation and Checking Duplicate Entities

江枫思渺然 提交于 2020-08-04 03:38:50

问题


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.



来源:https://stackoverflow.com/questions/23411933/best-practice-fluentvalidation-and-checking-duplicate-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!