Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The question here is, what good is a collection of all validators? If you want to perform validation, you're starting with an item (to validate) and you know its type, so it shouldn't be a problem to get the correct IValidator<T> for it. If you really need to have a common base for all validators, explicit interface implementation is a better choice than hiding methods with new:

    interface IValidator
    {
      bool Validate(object item);
    }
    
    interface IValidator<T> : IValidator
    {
      bool Validate(T item);
    }
    
    abstract class Validator<T> 
      : IValidator<T> where T : class
    {
      public abstract bool Validate(T item);
      
      bool IValidator.Validate(object item)
      {
        var typedItem = item as T;
        if (typedItem == null)
          throw new InvalidOperationException();
        
        return Validate(typedItem);
      }
    }


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: