在CQRS的Write Side上从数据库读取数据 [英] Reading data from database on Write Side in CQRS
问题描述
背景:
解释CQRS的图通常将清楚地区分读写路径和单向数据流,例如在此示例中(来源:
Diagrams explaining CQRS usually will have clear separation of read and write paths and one-way data flow, like in this example (source :
问题:
我想澄清一下,
如果后端的Command执行需要数据库中的某些数据,则
If a Command execution in back-end requires some data from the database,
- 写端"是否应具有从写数据库中读取的功能?
- 还是应该完全依靠读取侧"进行读取?
- 或者命令应该包含调用者提供的所有必需数据才能实现?
推荐答案
写面"应该具有从写数据库中读取的功能吗?
should a "write side" have some read capabilities from write database?
可能-加载将要运行命令的实体的最直接的方法是从写入数据库"中读取其状态.例如,在事件源体系结构中,通常通过以下方式处理更新事件源实体的命令:从写入模型中加载该实体的历史记录,从该历史记录中为该实体重新补水,评估该命令,并将新的更改附加到该历史记录中.
Probably -- the most straight forward way to load the entity that is going to be running the command is by reading its state from the "write database". For instance, in event sourced architectures, a command to update an event sourced entity is typically handled by loading the history of that entity from the write model, rehydrating the entity from that history, evaluating the command, and appending new changes to the history.
读取有关不是 处理命令的实体的状态是另一回事- ddd 的问题的词汇在这里有所帮助;您正在修改的状态恰好属于一个聚合,居住在聚合之外的状态应尽可能传递给模型,而不是获取.
Reading state about entities that are not handling the command is a different matter -- the ddd vocabulary helps here; the state you are modifying belongs to exactly one aggregate, state that lives outside of the aggregate should be passed to the model, rather than fetched, whenever it is possible to do so.
这可能意味着远程客户端需要提供数据,或者可能意味着处理命令的应用程序将获取所需的读取模型并提供答案.有许多不同的问题可以促使您在此处做出选择
That could mean that the remote client needs to provide the data, or it could mean that the application handling the command fetches the required read model and provides the answer. There are a number of different concerns which can drive the choice you make here
- 如果客户端在生成命令时使用的读取模型与应用在处理命令时使用的视图显着不同,则存在一定风险.
- 保持稳定的API可让您自由地重新设计模型中的聚合边界,而无需更新客户端.
因此,没有一个最佳答案,只是在不同问题之间进行权衡.
So no one best answer, just trade offs between different concerns.
出现了后者的最新示例-如果您的业务模型要求某些命令需要特定用户的权限,那么我们不应该从客户那里获得命令;我们需要应用程序来验证命令发布者的身份,然后将该身份的表示形式传递给模型,以便它可以确定要采取的操作.
A recent example of the latter came up -- if your business model requires that some commands require the authority of a specific user, then we shouldn't be taking the commands from the client at face value; we need the application to verify the identity of the command issuer, and then passing a representation of that identity to the model so that it can determine what action to take.
这篇关于在CQRS的Write Side上从数据库读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!