有没有更好的方法来实现“自定义”?业务验证器?
我有一个用户控件,我希望将用户控件放到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
甚至:
or even: