有没有更好的方法来实现“自定义”?业务验证器?

发布于 2024-11-08 20:12:54 字数 1418 浏览 0 评论 0原文

我有一个用户控件,我希望将用户控件放到 winform 上的开发人员可以在该用户控件执行某些操作之前测试一些值,

我想知道我的方法是否好以及是否有更好的方法来执行此操作?

实际上,在我的用户控件中,我有一个公开公开的 List 集合,开发人员可以添加已实现的 IValidator 。该接口仅实现一个 bool Validate() 方法。

用户控件有一个名为 Validate() 的方法,该方法迭代 List 集合并调用 Validate() 方法。

这是代码:

public partial class MyUserControl : UserControl
{
    private SqlConnection _connection;
    private List<IValidator> _validators;


    private void button1_Click(object sender, EventArgs e)
    {
        Validate();
    }

    private bool Validate()
    {
        foreach (IValidator validator in this.Validators)
        {
            validator.Validate();
        }
    }


    public List<IValidator> Validators
    {
        get { return _customValidators; }
        set { _customValidators = value; }
    }

}

以及托管用户控件的 winform 中的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Validator val1 = new Validator();
        myUserControl1.Validators.Add(val1);
    }
}


public class Validator : IValidator 
{
    public bool Validate()
    {
        return true;
    }
}

我想知道是否可以使用 C# 3 语法(lambda 等)“改进”或“简化”(无需实现接口) 。

I have a user control and I want the developer who drops the user control on the winform, can tests some values before this user control does something

I want to know if my method is good and if there is a better way to do this ?

Actually in my user control, I have a collection of List<IValidator> which is exposed publicly, to the developer could add IValidator which has implemented. This interface implements only a bool Validate() method.

The user control have a method named Validate() which iterate on the List<IValidator> collection and calls Validate() method.

Here is the code :

public partial class MyUserControl : UserControl
{
    private SqlConnection _connection;
    private List<IValidator> _validators;


    private void button1_Click(object sender, EventArgs e)
    {
        Validate();
    }

    private bool Validate()
    {
        foreach (IValidator validator in this.Validators)
        {
            validator.Validate();
        }
    }


    public List<IValidator> Validators
    {
        get { return _customValidators; }
        set { _customValidators = value; }
    }

}

And the code in the winform hosting the user control :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Validator val1 = new Validator();
        myUserControl1.Validators.Add(val1);
    }
}


public class Validator : IValidator 
{
    public bool Validate()
    {
        return true;
    }
}

I would know if there it could be "improved" or "simplified" (without implementing an interface) with the C# 3 syntax (lambda etc.) by the way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛泛之交 2024-11-15 20:12:54
private void button1_Click(object sender, EventArgs e)
{
    this.Validators.ForEach(v => v.Validate());
}

甚至:

button1.Click += (sender, e) => { this.Validators.ForEach(v => v.Validate()); };
private void button1_Click(object sender, EventArgs e)
{
    this.Validators.ForEach(v => v.Validate());
}

or even:

button1.Click += (sender, e) => { this.Validators.ForEach(v => v.Validate()); };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文