简体   繁体   English

使用Spring Data Common发布域事件时如何处理没有存储库的聚合根

[英]How to deal with the aggregate root without repository, when using Spring Data Common to publish domain events

The logic is: publish an event to notify closing a netty channel.逻辑是:发布一个事件通知关闭一个netty频道。 Before, through DomainEventPublish , I publish the event at domain service, application service, or catch .之前,通过DomainEventPublish ,我在域服务、应用程序服务或catch上发布事件。 Now, I found that it was wrong because only aggregate root can publish domain event.现在,我发现这是错误的,因为只有聚合根才能发布域事件。

I plan to refactor the way of publishing domain events to using Spring Data Common.我计划将发布领域事件的方式重构为使用 Spring Data Common。 I can design an aggregate root named ChannelToClose , and with a method that will register the NeedClose event, but I can't publish the registered events.我可以设计一个名为ChannelToClose的聚合根,并使用将注册NeedClose事件的方法,但我无法发布注册的事件。 Spring Data Common publishes the registered events only when executing save() of a repository, so I don't know how to publish events when aggregate root doesn't need to be persisted . Spring Data Common 只有在执行存储库的save()时才会发布注册的事件,所以我不知道在聚合根不需要持久化时如何发布事件

This is the comment of org.springframework.data.domain.DomainEvents :这是org.springframework.data.domain.DomainEvents的评论:

DomainEvents can be used on methods of aggregate roots managed by Spring Data repositories to publish the events returned by that method as Spring application events. DomainEvents 可用于 Spring Data 存储库管理的聚合根的方法,以将该方法返回的事件发布为 Spring 应用程序事件。

TL;DR: TL;博士:

Just call save anyway or manually register events using an ApplicationEventPublisher .只需调用save或使用ApplicationEventPublisher手动注册事件。

Some background:一些背景:

The underlying problem is that JPA doesn't really lend it well to DDD.潜在的问题是 JPA 并不能很好地适用于 DDD。

Since it saves everything that changed inside the session there is no easy hook to put the required event handling in.由于它保存了会话中更改的所有内容,因此没有简单的挂钩可以放入所需的事件处理。

One might consider inspecting the session itself or relying on JPAs lifecycle events, but these are based on entities with no (clear) way to identify the Aggregate Root it belongs to.人们可能会考虑检查会话本身或依赖 JPA 生命周期事件,但这些都是基于没有(明确)方法来识别它所属的聚合根的实体。

Imagine an Order of which you change the quantity in one of its LineItem s.想象一个Order ,您更改其中一个LineItemquantity JPA will fire an event for the LineItem but not for the Order , but the Order is the Aggregate Root that should fire the events. JPA 将为LineItem触发事件,但不会为Order触发事件,但Order是应该触发事件的聚合根。

So either way, it is a leaky abstraction.所以无论哪种方式,它都是一个泄漏的抽象。 Spring Data went with relying on the save-method which works for most (all?) other stores. Spring Data 依赖于适用于大多数(所有?)其他商店的保存方法。 These stores most of the time have a much clearer way to identify Aggregates.这些商店大多数时候都有更清晰的方法来识别聚合。 MongoDb for example has Documents which pretty much are a 1:1 fit to Aggregates.例如,MongoDb 的文档几乎是 1:1 适合聚合的。

I think the best solution to this problem is described at https://github.com/spring-projects/spring-data-jpa/issues/1727 :我认为这个问题的最佳解决方案在https://github.com/spring-projects/spring-data-jpa/issues/1727中有描述:

My current solution is to force that the save method must be explicitly called at the end of the service.我当前的解决方案是强制必须在服务结束时显式调用 save 方法。 I used a Hibernate interceptor that was inserted into the preFlush, which throws an exception when it finds that the aggregate has some domain events.我使用了一个插入到 preFlush 中的 Hibernate 拦截器,当它发现聚合有一些域事件时会抛出异常。

I found that the issue has become resolved.我发现问题已经解决了。 Can I ask if it is solved or not a problem?请问是解决了还是没有问题?

Also, I can't seem to close the issue, please close it另外,我似乎无法关闭问题,请关闭它

Credits go to https://jira.spring.io/secure/ViewProfile.jspa?name=teclxl学分转到https://jira.spring.io/secure/ViewProfile.jspa?name=teclxl

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 spring 正确发布 DDD 域事件? - How to properly publish DDD domain events with spring? Spring Data JPA如何为实现公共接口的两个域类创建单个存储库? - Spring Data JPA How to create single repository for two domain classes implementing a common interface? Axon框架-对每个聚合根使用单独的Mongo域事件集合 - Axon framework - using separate Mongo collection of domain events for each aggregate root Spring Data Rest域驱动设计-发布非聚合根实体 - Spring data rest Domain Driven Design - Posting non aggregate root entities 就 DDD 而言,是否可以在域服务中为聚合根实体而不是聚合根存储库实现 crud 逻辑? - Can crud logic be implemented in domain service for an aggregate root entity instead of this aggregate root repository in terms of DDD? 如何检索给定域 class 的 spring 数据存储库实例? - How to retrieve spring data repository instance for given domain class? Spring Data Domain事件丢失(?) - Spring Data domain events go missing (?) 在Java / Spring应用程序中使用JSON时如何处理正斜杠? - How to deal with forward slashes when using JSON in a Java/Spring application? 聚合 Spring 数据 Couchbase 存储库方法 - 如何查询给定属性的所有唯一值的列表? - Aggregate Spring Data Couchbase repository methods - how do I query for a list of all unique values for a given property? Spring Data Rest - 如何防止PUT / PATCH更新到聚合根的子实体 - Spring Data Rest - How to prevent PUT/PATCH updates to child entities of aggregate root
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM