最近我介绍了命令调度程序模式,它可以帮助命令与我们项目中的命令处理程序分离,该模式基于域驱动设计方法和 CQRS 模式。
不管怎样,我把它和 Mediator 设计模式搞混了。
Robert Harvey has already answered关于命令调度程序模式的问题如下:
A Command Dispatcher is an object that links the Action-Request with the appropriate Action-Handler. It's purpose is to decouple the command operation from the sending and receiving objects so that neither has knowledge of the other.
根据维基百科,The mediator pattern被描述为:
With the mediator pattern, communication between objects is encapsulated within a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects, thereby reducing coupling.
因此,据我所知,他们都将命令与指挥官分开,这使我们能够与调用者分离。
我在 Github 上看到了一些项目,它们使用命令调度程序模式来为请求的命令调用所需的处理程序,而其他项目则使用中介者模式来调度消息。 (例如,在大多数 DotNet 项目中,MediatR 库用于满足这一点)。
但是,我想知道在我们基于 DDD 方法和 CQRS 模式的项目中使用一种模式与另一种模式有什么区别和好处?
最佳答案
中介者模式在其纯粹的概念中更底层和更通用。中介者模式不定义您使用的通信类型或消息类型。在 Command Dispatcher 中,您处于上层(上下文和概念上),其中已经定义了通信和消息的类型。
您应该能够以 Mediator 模式(ergo with MediatR)为基础实现 Command Dispatcher 模式。
关于cqrs - Command Dispatcher 和 Mediator 设计模式有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57963474/