简体   繁体   English

常见事件处理的最佳实践

[英]Best Practice with Common Event Handling

In a WinForms solution, you have multiple controls of the same type. 在WinForms解决方案中,您有多个相同类型的控件。 You need to add an event handler to each of the control and at the current time the event handler will be doing the same thing. 您需要为每个控件添加一个事件处理程序,并且在当前时间事件处理程序将执行相同的操作。 You do not expect there to be difference between them down the road any reason. 你不希望他们在路上出现任何原因。

eg: 例如:

ScheduledPatientsGrid.ProcessGridKey += ScheduledPatientsGrid_ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += RecentPatientsGrid_ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += RecentPatientsGrid_ProcessGridKey;

... 

private void ScheduledPatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
   ...
}

private void RecentPatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
   ...
}

private void PatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
   ...
}

Now is it better to sharing an single Event Handler between the different events as shown below or use different ones like in the code sample shown above? 现在,如下所示在不同事件之间共享单个事件处理程序或使用上面显示的代码示例中的不同事件更好吗?

ScheduledPatientsGrid.ProcessGridKey += ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += ProcessGridKey;                


private void ProcessGridKey(object sender, KeyEventArgs e)
{
  ...
}

In the following page, Microsoft seems to suggest that sharing is better, however I notice that they have not updated it since .NET 2.0 (ie: Visual Studio 2008) 在下一页中,Microsoft似乎建议共享更好,但是我注意到自.NET 2.0以来它们没有更新它(即:Visual Studio 2008)

http://msdn.microsoft.com/en-us/library/4ac48519%28v=vs.90%29.aspx http://msdn.microsoft.com/en-us/library/4ac48519%28v=vs.90%29.aspx

Is there a Guide that makes a best practices recommendation in this case? 在这种情况下是否有指南提出最佳做法建议?

I would absolutely use the same method. 绝对会使用相同的方法。 What possible benefit is there to having multiple methods which do exactly the same, none of which is named to say what it does? 有多种方法完全相同,有什么可能的好处,没有一个方法被命名为它的作用?

Personally I abhor the source_EventName convention that Visual Studio spawns. 我个人厌恶Visual Studio产生的source_EventName约定。 I prefer to give my event handler methods meaningful names which say what they do . 我宁愿把我的事件处理方法有意义的名字,这不能不说他们什么。 Then when you look down the event handler list in the designer, you can see that when a button is clicked, X will happen rather than "the button's click event handler will be called" which is useless. 然后当你向下看设计器中的事件处理程序列表时,你会发现当单击一个按钮时,X将发生而不是“按钮的click事件处理程序将被调用”,这是无用的。

Alternatively, use lambda expressions to subscribe to the events and call meaningful methods with meaningful parameters. 或者,使用lambda表达式订阅事件并使用有意义的参数调用有意义的方法。 (The sender and args are often useless for event handlers.) senderargs通常对事件处理程序无用。)

In this case, I usually have them wrap a common method, but I keep their event handlers named per usage. 在这种情况下,我通常让它们包装一个常用方法,但我保留了按用法命名的事件处理程序。 This allows me to easily unit test the method and (usually) reduce the needed parameters, and any errors in the stack trace will be very readable as to which grid the process failed for: 这使我可以轻松地对方法进行单元测试,并且(通常)减少所需的参数,并且堆栈跟踪中的任何错误都将非常易读,因为该过程失败的网格:

ScheduledPatientsGrid.ProcessGridKey += ScheduledPatientsGrid_ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += RecentPatientsGrid_ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += RecentPatientsGrid_ProcessGridKey;

... 

private void ScheduledPatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
   ProcessGridKey(e.Key);
}

private void RecentPatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
   ProcessGridKey(e.Key);
}

private void PatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
   ProcessGridKey(e.Key);
}

private void ProcessGridKey(Key e)
{
    ...
}

Your mileage may vary depending on what the shared method does, or the parameters passed in. (For example, in my above sample, I duplicate the pulling of the Key from the KeyEventArgs . 您的里程可能会有所不同,具体取决于共享方法的作用或传入的参数。(例如,在上面的示例中,我复制了从KeyEventArgs提取Key的内容。

I prefer sharing, if the logic gets out of hand you can always just use the single event as a router to the correct method like... 我更喜欢共享,如果逻辑失控,你总是可以使用单个事件作为路由器来使用正确的方法,如...

private void ProcessGridKey(object sender, KeyEventArgs e)
{
     if (sender is x)
          xmethod();
     if (sender is y)
          ymethod();    //etc 
}

I'm aware this syntax doesn't quite make sense as the sender will always be the same object in OP example, but you get the idea. 我知道这个语法没有多大意义,因为发送者在OP示例中始终是同一个对象,但是你明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM