vendredi 14 juin 2019

Invoke method from different classes that was implemented from an Interface

I have an interface that is being inherited by two classes and implements its method Validate (No issues here).

public interface IValidator
{
   IEnumerable<ValidationError> Validate(MyModel model);
}

public class ValidatorA : IValidator
{
   public IEnumerable<ValidationError> Validate(MyModel model)
   {
       var list = new List<ValidationError>();

       //code to add items to list

       return list;
   }
}

public interface ValidatorB : IValidator
{
   public IEnumerable<ValidationError> Validate(MyModel model)
   {
       var list = new List<ValidationError>();

       //code to add items to list

       return list;
   }
}

What I want to achieve is to call the method from the interface and invoke all method from the class (ValidatorA, ValidatorB) that inherits the interface. Below is how I do it but it doesn't hit the child classes. What am I doing wrong or what is the proper way to do it?

//assuming this is inside a method of another class
//and injected inside
var validationErrors = _myInterfaceValidator.SelectMany(x => x.Validate(myModel));  //why is this not hitting validate from the child classes
return validationErrors.ToList();

Aucun commentaire:

Enregistrer un commentaire