从 CQRS 中的 2 个不同聚合访问相同数据
我阅读了 CQRS 并喜欢这个概念,但发现它与“正常”开发有点不同。我确实发现了一个“问题”,但我还没有找到任何如何实现的好例子。这就是我如何从两个不同的聚合根访问“相同的数据”。我的理解方式是,我序列化聚合根及其对写入数据库的值,然后我永远无法从两个不同的聚合根获取“相同的数据”。
这是我的意思的一个例子。
因此,如果我有一个网上商店并且销售商品,我将减少该产品的 ItemsInStock,因此在我的网页上我可以显示有多少库存,或者是否必须在交付给客户之前订购。
因此该产品将是一个聚合根,因为我可以向它发送命令,例如 RefillStockCommand(int number)。我将拥有的另一个聚合根是 Order,它将有一个 SendCommand(),它将把订单发送给客户等等,好吧,你明白了。
当我执行 SendCommand() 时,我想减少订单中所有产品的 ItemsInStock 状态,并且如果其中一些产品已达到低于 5 的状态,我想从零售商处订购更多该产品。那么,我是否将我的产品aggregateroot 加载到我的Orderaggregateroot 中可以吗?如果我可以做到这一点,那么我会获取我的产品的数据,并在 SendCommand() 中更改我的产品的 ItemsInStock 状态,但是可以访问另一个聚合根中的聚合根吗,这听起来是错误的,但如果不是,我该如何更改1 个命令中的 2 个aggregatesroots 并使用另一个aggregaterroot 中的一个aggregateroots 数据进行业务验证?
此致 马格努斯·格拉德
am I reading up on CQRS and like the concept, but finds it a bit different against "normal" development. I did find an "problem" that I haven't found any good example of how to implemented. And that is how do I access the "same data" from 2 diffrent aggreates roots. The way I have understood it is that I serialize the aggregateroot and it's value to the write database, and then I can never get the "same data" from 2 diffrent aggregateroots.
Here is an example of what I mean.
So if I have a webshop and I sell an item I will decresse the ItemsInStock for that product, so on my webpage I can show how many is in stock or if it will has to be ordered in before delivery to the customer.
So the product will be an aggregateroot since I can send commands to it, like RefillStockCommand(int number). The other aggregateroot I will have is Order that will have an SendCommand() that will send the order to the customer etc etc, well you get the picture.
When I do the SendCommand() I want to decrease the ItemsInStock-state for all the products in my order and also if some of it has reached below say 5 I want to order more of this product from my retailer. So do I load up my products aggregateroots in my Orderaggregateroot is that okay? If I can do that then I get the data for my product and also change the ItemsInStock-state for my product in the SendCommand() but is it okay to access a aggregateroot inside another aggregateroot it sounds wrong, but if not how do I change 2 aggregatesroots in 1 command and use the one aggregateroots data in another aggregaterroot for business validation?
Best regards
Magnus Gladh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速流程如下:
在您的 Ordergin/Biling 边界上下文中:
您有一个 CreateOrder 命令来创建订单。结果将发布 OrderCreatedEvent。
在您的库存/产品管理有界上下文中:
您有事件处理程序订阅了 OrderCreatedEvent,它将直接告诉每个产品减少 ItemsInStock,或者为此向每个产品发送命令。
更改 CreateOrder 事务边界内产品的 ItemsInStock 的问题是,如果由于某种原因更改库存商品失败(例如变为负值),您的订单创建也将失败,这可能不是您想要的。
与 CQRS 相比,这更多地与领域建模和 DDD 有关。
A quick flow would be like this:
In your Ordergin/Biling bounded context:
You have a CreateOrder command which will create the order. As a result an OrderCreatedEvent will be published.
In your Inventory/Product Management bounded context:
You have event handlers subscribed the the OrderCreatedEvent which will either directly tell each product to decrease ItemsInStock or will send a command to each product for that.
The problem with changing the ItemsInStock for a product inside the CreateOrder transaction boundary is that if for some reason changing items in stock fails ( becomes negative for example ) your order creation will also fail, which is probably not what you want.
This has more to do with domain modeling and DDD than with CQRS.