在领域驱动设计中,领域事件(应该不了解基础设施)可以将视图更改推送到客户端吗?

发布于 2025-01-10 10:25:37 字数 587 浏览 0 评论 0原文

我正在构建一个书店销售点系统,并第一次尝试使用分层架构/DDD。书籍是我的聚合,DDD 确保我的书籍只能以满足创建相关业务规则的方式创建。但是,另一个业务规则是两个图书库存项目不能具有相同的 SKU。因此,我设置了域事件 BookCreated 或 BookSkuUpdated,这些事件最终由域服务订阅,该域服务检查重复项并在发现重复项时对其进行标记。最终的一致性,到目前为止一切顺利。

我的问题是如何将此域行为集成到我的网络应用程序中。创建书籍时,代码从基础设施(我的控制器)流向应用程序层(创建书籍用例)再到域(书籍聚合),然后返回应用程序层和基础设施,以便可以将其显示给客户端。没问题,最终请求->回复。但是,我的域事件设置了一个小回调队列,该队列创建一个新的代码流,该代码流在域中启动,创建域事件,最终导致订阅者启动新的应用程序层用例 (updateDuplicateSku)。因为我的持久性是单个存储库,所以我可以将新的重复Sku警告保存到存储库,但我无法弄清楚如何将该视图推送给客户端。用例不是返回到具有请求和响应对象的控制器,而是在域中启动,因此对客户端没有任何了解。

简而言之 - 域事件回调是否有一种好方法可以对域进行最终的一致性更改,并将这些更改通过服务器发送的事件或 Websocket 推送到客户端?

I'm building a bookstore point of sale system, and trying to use layer architecture / DDD for the first time. Books are my aggregates, and DDD ensures that my books can only be created in such a way that the business rules around creation are satisfied. However, another business rule is that two book inventory items can't have the same SKU. I've therefore set up domain events, BookCreated or BookSkuUpdated, which ultimately are subscribed to by a domain service that checks for duplicates and marks them if they're found. Eventual consistency, and so far so good.

My question is how to integrate this domain behavior into my web app. When the book is created, the code flows from infra (my controller) to application layer (the create book use case) to domain (book aggregate) then back to application layer and infra so it can be displayed to the client. No problem, ultimately request -> response. However, my domain events are set up with a little callback queue that creates a new code flow which STARTS in the domain, with creation of the domain event, and ultimately results in a subscriber kicking off a new application layer use case (updateDuplicateSku). Because my persistence is a single repo I can save the new duplicateSku warning to the repo, but can't for the life of me figure out how to push that view to the client. Instead of the use case returning to the controller, which has the request and response objects, the use case was kicked off in the domain so doesn't have any knowledge of the client.

In short - is there a good way for domain event callbacks to make eventual consistency changes to the domain and push those changes to a client with server sent events or websockets?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪荡不羁 2025-01-17 10:25:37

领域事件允许聚合对另一个聚合中的更改做出反应,而无需耦合它们。因此,使用域事件处理程序 (updateDuplicateSku) 来检测重复项听起来不错。

接下来,updateDuplicateSku 应发布集成事件(即 SKUMarkedAsDuplicate)。集成事件的目的是传播已提交的事务并更新其他子系统。在这种情况下,集成事件将由 websocket 服务器处理,该服务器又负责将更改推送回客户端。

所有通信本质上都是异步的,因此您不能使用客户端 API 调用中的 req 和 res 变量。

Domain Events allow aggregates to react to a change in another aggregate without coupling them. So using a domain event handler (updateDuplicateSku) to detect duplicates sounds fine.

Next, updateDuplicateSku should publish an integration event (i.e. SKUMarkedAsDuplicate). The purpose of integration events is to propagate committed transactions and to update additional subsystems. In this case, an integration event would be handled by a websocket server, which is in turn responsible for pushing changes back to the client.

All the communication is asynchronous in nature so you cannot use req and res variables from a client API call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文