消息处理程序和命令处理程序之间有区别吗?
消息处理程序和命令处理程序之间有区别吗? 或者命令和消息之间有区别吗?
编辑:有第三个候选者称为命令消息 ...呃。
Is there a difference between a message handler and a command handler?
Or is there a difference between a command and a message?
Edit: There is 3rd candiate called command message ... ugh.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,区别不在于结构或实现,而在于概念上的差异——这两种结构都代表不同的概念。
消息 - 传输某种信息的结构
的结构
命令 - 触发某种操作 ,命令是一种消息,因为它至少传输有关要调用的操作的信息(在大多数情况下还传输附加参数)。
在实现方面,MessageHandler 和 CommandHandler 看起来非常相似,并且取决于您想要对信息执行什么操作。
示例:
UserRegisteredMessage (userName) - 通知系统中使用的注册信息,处理程序可以在 UI 上显示此信息
RegisterUserCommand(userName) - 指示我们的系统注册用户的命令,这可能涉及某种额外的操作(例如检查唯一性)并且可能会失败。
Actually, the difference is not in structure nor implementation, it is rather a conceptual one - both of these structures are to represent different concepts.
Message - a structure that transports some kind of information
Command - a structure that triggers some kind of action
That said, a Command is a kind of Message, as it transports information about action to invoke at least (and in most cases additional parameters as well).
In terms of implementation, both MessageHandlers and CommandHandlers look very similar and depend on what do you want to do with the information.
Examples:
UserRegisteredMessage (userName) - a message that informs about registered used in a system, a handler can for display this information on UI
RegisterUserCommand(userName) - a command that instructs our system to register a user, this may involve some kind of additional action (like checks for uniqueness) and can can fail.
根据我的经验,主要是语义以及模式的放置位置。消息可能有副作用,也可能没有,而命令则暗示它有副作用。另外,我认为命令模式旨在作为客户端上的 ui 模式(通常,但并非总是),其中消息模式意味着网络跃点的可能性。
此外,在大多数实现中,消息的耦合更为松散。您在可能会或可能不会被收听的频道上发送消息。命令(通常但不总是)是执行时更直接的关系。
Mostly semantics, and where the pattern is placed, in my experience. A message may or may not have side-effects, where a command implies that it will. Also, I think command pattern is intended as a ui pattern on a client (generally, though not always), where the message pattern implies the potential for a network hop.
Also, in most implementations, a message is more loosely coupled. You send a message on a channel which may or may not be listened to. Command (usually, not always) is a more direct relationship at execution time.