Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Best Practice) FluentValidation and Checking Duplicate Entities

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

like image 732
Hamed F Avatar asked May 01 '14 16:05

Hamed F


2 Answers

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);
    }
}
like image 112
Ufuk Hacıoğulları Avatar answered Oct 08 '22 11:10

Ufuk Hacıoğulları


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.

like image 28
zimmer62 Avatar answered Oct 08 '22 12:10

zimmer62