views:

22

answers:

0

I have the following code using FluentValidation but it is not possible to call SetValidator(FredAnotherClassValidator()) because it is not of type IAbstractAnotherClass

The compiler error is

The type 'IAbstractAnotherClass' cannot be used as type parameter 'TCollectionProperty' in the generic type or method 'FluentValidation.DefaultValidatorExtensions.SetValidator(FluentValidation.IRuleBuilder, FluentValidation.IValidator)'. There is no implicit reference conversion from 'IAbstractAnotherClass' to 'System.Collections.Generic.IEnumerable'.

public class MyClass
{
    public string SomeProperty { get; set; }
    public IAbstractAnotherClass AnotherClass { get; set; }
}

public interface IAbstractAnotherClass
{
    string SomeProperty { get; set; }
}

public class FredAnotherClass : IAbstractAnotherClass
{
    public string SomeProperty { get; set; }
    public string BlahProperty { get; set; }
}

public class MyClassValidator : AbstractValidator<MyClass>
{
    public MyClassValidator()
    {
        RuleFor(x => x.AnotherClass).SetValidator(new FredAnotherClassValidator());
    }
}

public class FredAnotherClassValidator : AbstractValidator<FredAnotherClass>
{
    public FredAnotherClassValidator()
    {
        RuleFor(x => x.SomeProperty).NotEmpty();
        RuleFor(x => x.BlahProperty).NotEmpty();
    }
}