事件对象应该命名为过去时动词还是名词?

发布于 2024-11-17 05:03:45 字数 487 浏览 5 评论 0原文

我已经阅读了几篇事件溯源文章和示例。我认为有些是动词,有些是名词,两者都可能在末尾加上事件。哪个更正确?一个例子是。 OrderShippedEvent(或有时只是 OrderShipped)与 ShipOrderEvent。对这些类型的对象进行操作的方法的时态也没有多大帮助。这是我找到的一些例子。

process(OrderShippedEvent);

或者

apply(ShipOrderEvent);

在其他情况下,会有类似的处理程序

HandleOrderShipped(OrderShippedEvent)

,或者

HandleShipOrder(ShipOrderEvent)

我认为应该有一种标准方法来命名和处理任何上下文中的任何类型的事件,尽管我对事件溯源场景最感兴趣。

I have been reading several event sourcing articles and examples. I have seem some as verbs, and others name as nouns with both of which might have event tacked on the end. Which is more correct? One example would be. OrderShippedEvent ( or sometimes just OrderShipped) vs ShipOrderEvent. The tense of the methods that operate on these types of objects have not helped much either. Here are some examples I've found.

process(OrderShippedEvent);

or

apply(ShipOrderEvent);

in other cases there will be handlers like

HandleOrderShipped(OrderShippedEvent)

or

HandleShipOrder(ShipOrderEvent)

I assume there should be a standard way to name and handle any type of events in any context, although I'm most interested in an Event Sourcing scenario.

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

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

发布评论

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

评论(2

︶ ̄淡然 2024-11-24 05:03:45

嗯,它们的含义不同:

  • ShipOrderEvent 表示订单即将发货,但我们不确定订单是否已经发货。
  • OrderShippedEvent 肯定意味着订单已发货。

它们类似于 .NET 中 System.Windows.Forms.Form 类的 FormClosing/FormClosed 事件。因此,与其建议使用什么名称,我宁愿建议您思考您想要通过事件代表什么,并相应地命名它。

Well, they mean different things:

  • ShipOrderEvent means that a an order is about to be shipped, but we have no certainty as whether it has or hasn't been shipped yet.
  • OrderShippedEvent means that the order has been shipped, for sure.

They are smilar to the FormClosing/FormClosed events of the System.Windows.Forms.Form class in .NET. So, rather than suggesting what name to use, I'd rather suggest that you think what you want to represent by your event, and name it accordingly.

望她远 2024-11-24 05:03:45

事件通常定义已发生已被触发并在触发后发送消息。我勇敢地站在中间保留过去式动词的选项之外。当然,有些语言对于命名约定有特定的最佳实践 - 有些有 onEventName,有些使用现在发生的操作(Button.Click)。至于在名称上附加“事件”一词,嗯,我不习惯 - 但据我所知,命名约定的存在是为了摆脱神奇变量 (通过查看它们,您根本无法掌握其数据是什么或它为何存在),因此同样适用于方法和事件 - 例如名为 DoSomething() 的方法和名为 Thing 的事件。人们可以理解它们,但这需要更具冒险精神的灵魂去解析代码,以将超级好命名变量/方法/函数正确地放置在一些人类可理解的流程中。

An event usually defines something has happened or has been triggered and sends a message after being triggered. I valiantly stand besides the option to keep it with a past-tense verb in the middle. Of course some languages have specific best practices for naming conventions - some have onEventName, some use the action happening now (Button.Click). As for appending the Event word on the name, well, I'm not used to it - but as far as I am aware, naming conventions exist to get rid of magical variables (where by looking at them you can't grasp the least of what its data is or why it even exists), so the same would apply for methods and events - like a method named DoSomething() and an Event named Thing. One can understand them but that requires a more adventurous soul to go and parse the code to properly place the super-well-named variable/method/function in some human-understandable flow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文