Factory to create common classes

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 specific implementations. One of them is AccountValidator:

public class AccountCreateValidator : Validator<IAccount>
{
    public override bool Validate(IAccount input)
    {
        //some validation
    }
}

Another will be LoginValidator:

public class LoginValidator : Validator<IAccount>
{
    public override bool Validate(IAccount input)
    {
        //some different validation
    }
}

Now I want to create a factory to return an instance of the validator implementation. Sort of:

public static class ValidatorFactory
{
    public static Validator GetValidator(ValidationType validationType)
    {
        switch (validationType)
        {
            case ValidationType.AccountCreate:
                return new AccountCreateValidator();
        }
    }
}

I would like to do it as

Validator myValidator = ValidatorFactory.GetValidator(ValidationType.AccountCreate); 

However, he doesn’t like to return the new line AccountCreateValidator (), or the fact that I declare myValidator as a Validator, not Validator<SomeType>. Any help would be appreciated.

+5
source share
3 answers

, factory . , , , , -, , . , GetValidator :

public static Validator<TypeToValidate> GetValidator<TypeToValidate>(ValidationType validationType) where TypeToValidate : IValidatable

: Validator<IAccount> validator = ValidatorFactory.GetValidator<IAccount>(ValidationType.AccountCreate)

+2

, , , , Validator. , - :

public interface IValidator 
{
    bool Validate(object input);
}

public abstract class Validator<T> : IValidator where T : IValidatable
{
    public abstract bool Validate(T input);

    public bool Validate (object input)
    {
        return Validate ((T) input);
    }
}

public static class ValidatorFactory
{
    public static IValidator GetValidator(ValidationType validationType)
    {
        switch (validationType)
        {
            case ValidationType.AccountCreate:
                return new AccountCreateValidator();
        }
    }
}

:

IValidator myValidator = ValidatorFactory.GetValidator(ValidationType.AccountCreate); 

.

+1

factory, Type , factory . , ( IAccount), , factory, , :

public static class ValidatorFactory
{
    public static Validator<T> GetValidator<T>(ValidationType validationType) 
        where T : IValidatable
    {
        switch (validationType)
        {
            case ValidationType.AccountCreate:
                return new AccountCreateValidator() as Validator<T>;
                    // etc...
        }
    }
}

:

var value = ValidatorFactory.GetValidator<IAccount>(ValidationType.AccountCreate)

AccountCreateValidator Validator<IAccount>.

, , , , , , - , , . ValidatorFactory.GetValidator<IUser>(ValidationType.AccountCreate) .

: - , , , , new AccountCreateValidator() as Validator<T>. , , , , ( , , ). LINQPad, .

I also believe that my previous comment about a possible compilation error, if you pass the wrong generic types, no longer means that casting using the keyword aswill simply return nullif it fails to do so.

0
source

All Articles