validation – Cast Exception for FluentValidation (Upgrading from FluentValidation ver 4 to V 10 issue)

Please I need your help.

I’m trying to upgrade my application to use FluentValidation V 10 from older V 4.

I wrote my validator class inside my customer class because I don’t want to reuse it anywhere else, I created a constructor and I put my customer rules there so the FluentValidation can call upon the customer validator class and execute the validation. I created a virtual method in the customer class named ( GetValidator() ) to return an instance of the validator to my base class to call the validate method and obtaining the results.
So what I did works fine on the old version but when I upgrade to FluentValidation Ver 10 I’m getting an exception on line “ValidationResult results = _Validator.Validate(this);” The message says “cannot convert from ‘ObjBase’ to ‘FluentValidation.IValidationContext’”
So I tried to solve it but I couldn’t do it. Please I need your advice and help.

My Base Class:
public abstract class ObjBase : IDataErrorInfo
{
public ObjBase()
{
_Validator = GetValidator();
Validate();
}

    protected bool _IsDirty = false;
    protected IValidator _Validator = null;

    protected IEnumerable<ValidationFailure> _ValidationErrors = null;

    protected virtual IValidator GetValidator()
    {
        return null;
    }

    public IEnumerable<ValidationFailure> ValidationErrors
    {
        get { return _ValidationErrors; }
        set { }
    }

    public void Validate()
    {
        if (_Validator != null)
        {
            var context = new ValidationContext<object>(_Validator); 
            ValidationResult results = _Validator.Validate(context); //<=== the exception here
            _ValidationErrors = results.Errors;
        }
    }

    public virtual bool IsValid
    {
        get
        {
            if (_ValidationErrors != null && _ValidationErrors.Count() > 0)
                return false;
            else
                return true;
        }
    }

    #region IDataErrorInfo members

    string IDataErrorInfo.Error
    {
        get { return string.Empty; }
    }

    string IDataErrorInfo.this(string columnName)
    {
        get
        {
            StringBuilder errors = new StringBuilder();

            if (_ValidationErrors != null && _ValidationErrors.Count() > 0)
            {
                foreach (ValidationFailure validationError in _ValidationErrors)
                {
                    if (validationError.PropertyName == columnName)
                        errors.AppendLine(validationError.ErrorMessage);
                }
            }

            return errors.ToString();
        }
    }

    #endregion
}

My Customer Class:

public class Customer : ObjBase
{
    string _CustomerName = string.Empty;
    public string CustomerName
    {
        get { return _CustomerName; }
        set
        {
            if (_CustomerName == value)
                return;

            _CustomerName = value;
        }
    }

    class CustomerValidator : AbstractValidator<Customer>
    {
        public CustomerValidator()
        {
            RuleFor(obj => obj.CustomerName).NotEmpty();
        }
    }

    protected override IValidator GetValidator()
    {
        return new CustomerValidator();
    }
}

My test Class:

(TestClass)
public class CustomerValidationTestClass
{
    (TestMethod)
    public void test_validation()
    {
        Customer objTest = new Customer();

        objTest.CustomerName = "test value";

        int changeCounter = 2;
        Assert.IsTrue(changeCounter == 2, "");
    }
}

Exception info:
System.InvalidOperationException
HResult=0x80131509
Message=Cannot validate instances of type ‘CustomerValidator’. This validator can only validate instances of type ‘Customer’.
Source=FluentValidation