注册到事件命名方法事件、方法

2023-09-03 16:59:37 作者:loveon凝结

假设我有一个公开以下事件类:

Assume that I have a class that exposes the following event:

public event EventHandler Closing

应该如何注册到该事件的方法来命名?你preFER遵循惯例,当它指定名称它所产生的方法的Visual Studio使用(又名+ =,标签,标签)?例如:

How should methods that are registered to this event be named? Do you prefer to follow the convention that Visual Studio uses when it assigns names to the methods it generates (aka. +=, Tab, Tab)? For example:

private void TheClass_Closing( object sender, EventArgs e )

或者你用自己的风格来命名这些方法?

Or do you use your own style to name these methods?

我已经尝试了不同的方式来命名这些方法(如 TheClassClosing HandleClosing 等)。但是我还没有找到一个好的作风,表示方法的目的是处理注册的事件。我个人不喜欢的样式(下划线)的Visual Studio使用生成方法名。

I've tried different ways to name these methods (like TheClassClosing, HandleClosing, etc.). But I haven't found a good style to indicate that the intent of a method is to handle a registered event. I personally don't like the style (underscore) that Visual Studio uses to generate method names.

我知道注册的事件处理方式始终是私人和有像一个对于的提高的事件(方法,例如, OnClosing )。

I know that registered event-handling methods are always private and that there is no naming convention like the one for methods that raise events (e.g., OnClosing).

推荐答案

对于命名的两个常见的选择是要么以后有什么方法做:

The two common options for naming is either after what the method does:

theObject.Closing += SaveResults;

或者可以选择后什么方法处理:

Or alternatively after what the method handles:

theObject.Closing += ClosingHandler;

这是preferable真的要看了一下环境。

Which is preferable really depends a bit on context.

在第一种情况下它是不清楚什么样的处理程序是要干什么,这使得code注册的处理程序更可读......但看着处理程序 SaveResults 孤立它不会是当它要被调用,一定是明显的,除非事件参数有明显的名称( ClosingEventArgs 或类似)。

In the first case it is immediately clear what the handler is going to do, which makes the code registering the handler more readable... but looking at the handler SaveResults in isolation it is not going to be necessarily obvious when it is going to be called, unless the event arguments have an obvious name (ClosingEventArgs or some such).

在第二种情况下,注册是更加不透明(好吧,这是怎么回事时,关闭情况发生吗?),但在另一方面,看着处理器实现很明显是怎么回事。

In the second case, the registration is more opaque (okay, so what is going to happen when Closing happens?), but on the other hand, looking at the handler implementation it will be obvious what is going on.

我想一个选择取决于哪两个要更加明显;注册的网站,或者处理程序的实现。

I guess the one to choose depends on which of the two you want to be more obvious; the site of the registration, or the implementation of the handler.

或者,你可以去这两种方法的邪恶组合:

Or alternatively, you can go for the unholy combination of both methods:

theObject.Closing += ClosingHandlerSaveResults;

现在无论是注册网站,并实施同样明显,无论看起来特别优雅(加,它可能违反了DRY原则)。

Now both the registration site and the implementation are equally obvious, and neither looks particularly elegant (plus, it probably violates the DRY principle).

有关记录我preFER当 theObject 包含在从执行不同范围的第一个命名方案 SaveResults ,当我连接最多处理程序,即都包含在同一类事件的第二个方案。

For the record I prefer the first naming scheme when theObject is contained in a different scope from the implementation of SaveResults, and the second scheme when I am wiring up handlers to events that are all contained within the same class.