开发者

Access same data from 2 diffrent aggregates in CQRS

开发者 https://www.devze.com 2023-03-18 22:32 出处:网络
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 t

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 de开发者_如何学Ccresse 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消