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
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);
    }
}
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With