为每个提供商实施电子邮件验证的正确设计模式是什么?

the*_*ist 2 c# design-patterns

我必须通过几个验证器验证电子邮件.我有class(EmailValidator),它有验证器列表(RegexValidator,MXValidator...),它通过验证器验证电子邮件. RegexValidator例如,每个提供商都有自己的验证器.如果它识别出这是gmail,那么检查它是否与特定模式匹配,如果它是mygmail,那么它检查它是否与mygmail的模式匹配,否则返回true. MXValidator将验证其他东西.

实现这个的正确设计模式是什么?

public interface IValidator
{
   bool Validate(string email);
}
public class RegexValidator : IValidator
    {
        private const string EMAIL_REGEX = @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b";
        public bool Validate(string email)
        {
            var regex = new Regex(EMAIL_REGEX);
            var isEmailFormat regex.IsMatch(email);
            if(isEmailFormat)
            {
                 //here it should recognize the provider and check if it match the provider's pattern
            }

            return true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ali*_*tad 5

责任链.

只要一个验证器找到无效模式,就返回false.您传递了一个有序的验证器列表.

bool ValidateEmail(string email, IEnumerable<IValidator> validators, ref errorMessage)
{
    return !validators.Any(v => !v.Validate(email, ref errorMessage);
}
Run Code Online (Sandbox Code Playgroud)

假设

interface IValidator
{
    bool Validate(object value, ref errorMessage);
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

我认为这是另一个验证器:

public class EmailDomainValidator : IValidator
{

   public EmailDomainValidator(string domain)
   {
      _domain = domain;
   }

   ...
}
Run Code Online (Sandbox Code Playgroud)