Some collection of design ideas (2)

Design on the picture content check

First, define the interface

public interface IValidator {
    void Validate(object value);
}

And virtual classes

public abstract class AbstractValidator {

    private string errorMessage;
    public string ErrorMessage {
        get { return errorMessage; }
        set { errorMessage = value; }
    }

    protected bool IsEmptyString(object obj) {
        if (obj == null || obj.ToString() == string.Empty) {
            return true;
        } else {
            return false;
        }
    }

    public AbstractValidator() {
        return;
    }
}

Then define each particular class check, for example only half-size alphanumeric input of check

public class AlphabetAndNumberValiator : AbstractValidator, IValidator {

    public void Validate(Object value) {
        Regex regex = new Regex(@"[a-zA-Z0-9]");
        if (!regex.IsMatch(value.ToString())) {
            throw new ValidatorException(this.ErrorMessage);
        }
    }

    AlphabetAndNumberValiator public () {
        this.ErrorMessage = "Please enter a half size alphanumeric";
    }

Finally, the definition of container

public class ActiveValidator {

    private ErrorProvider errorProvider;

    private Dictionary<Control, List<IValidator>> dic = new Dictionary<Control, List<IValidator>>();

    private bool isAutoAddEventHandler = true;
    public bool IsAutoAddEventHandler {
        get { return isAutoAddEventHandler; }
        set { isAutoAddEventHandler = value; }
    }

    public void AddValidator(Control control, IValidator validator) {
        if (dic.ContainsKey(control)) {
            if (dic[control].Contains(validator) == false) {
                dic[control].Add(validator);
            }
        } else {
            List<IValidator> list = new List<IValidator>();
            list.Add(validator);
            dic.Add(control, list);
            control.Validating += new CancelEventHandler(this.DoValidation);
        }
    }

    public bool DoAllValidation() {
        this.errorProvider.Clear();
        bool ret = false;
        foreach (Control control in dic.Keys) {
            foreach (IValidator validator in dic[control]) {
                try {
                    validator.Validate(control.Text);
                } catch (ValidatorException ex) {
                    this.errorProvider.SetError(control, ex.Message);
                    ret = true;
                }
            }
        }
        return ret;
    }

    private void DoValidation(object sender, EventArgs e) {
        if (this.isAutoAddEventHandler) {
            this.errorProvider.Clear();
            Control control;
            if (sender is Control) {
                control = (Control)sender;
            } else {
                return;
            }
            List<IValidator> list;
            if (dic.ContainsKey(control)) {
                list = dic[control];
            } else {
                return;
            }
            foreach (IValidator validator in list) {
                try {
                    validator.Validate(control.Text);
                } catch (ValidatorException ex) {
                    this.errorProvider.SetError(control, ex.Message);
                    // TODO
                }
            }
        }
    }

    public ActiveValidator(ErrorProvider errorProvider) {
        this.errorProvider = errorProvider;
    }
}

 

Example screen additional class

            this.validator = new ActiveValidator(this.errorProvider);
            this.validator.AddValidator(this.TextBox1, new AlphabetAndNumberValiator());

Meanwhile, in the event click submit button is, or you can call this.validator.DoAllValidation (), do check full-screen registration control.

Reproduced in: https: //my.oschina.net/cjkall/blog/195903

Guess you like

Origin blog.csdn.net/weixin_33766805/article/details/91756123