I have an abstract class called Validator:
public abstract class Validator<T> where T : IValidatable
{
public abstract bool Validate(T input);
}
And I have a few concrete implementations. One is AccountValidator:
public class AccountCreateValidator : Validator<IAccount>
{
public override bool Validate(IAccount input)
{
//some validation
}
}
Another would be LoginValidator:
public class LoginValidator : Validator<IAccount>
{
public override bool Validate(IAccount input)
{
//some different validation
}
}
I now want to create a factory to return the an instance of a validator implementation. Something like:
public static class ValidatorFactory
{
public static Validator GetValidator(ValidationType validationType)
{
switch (validationType)
{
case ValidationType.AccountCreate:
return new AccountCreateValidator();
}
}
}
I'd then like to do call it like
Validator myValidator = ValidatorFactory.GetValidator(ValidationType.AccountCreate);
However it doesn't like the return new AccountCreateValidator() line, or the fact I'm declaring myValidator as Validator and not Validator<SomeType>
.
Any help would be appreciated.
IValidatable
rather than based on an Enum? Then you could return aValidator<T>
and take aT
in theGetValidator
method andT
could be restricted to be ofIValidatable
– Mauriziaclass Validator<T> where T : IValidatable
, isn't that you just wantclass Validator : IValidatable
? – Rexer