事件对象应该命名为过去时动词还是名词?
我已经阅读了几篇事件溯源文章和示例。我认为有些是动词,有些是名词,两者都可能在末尾加上事件。哪个更正确?一个例子是。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,它们的含义不同:
它们类似于 .NET 中 System.Windows.Forms.Form 类的 FormClosing/FormClosed 事件。因此,与其建议使用什么名称,我宁愿建议您思考您想要通过事件代表什么,并相应地命名它。
Well, they mean different things:
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.
事件通常定义已发生或已被触发并在触发后发送消息。我勇敢地站在中间保留过去式动词的选项之外。当然,有些语言对于命名约定有特定的最佳实践 - 有些有 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.