lagom如何识别哪些事件是针对哪个实体的

how does lagom identify which events are for which entity

根据概念上关于 Lagom 的讨论之一,我阅读了这篇 link

The entities are not persisted anywhere - that's the point of event sourcing, you just store the events, which can be implemented very simply, be easily distributed, and done with very high performance since it's just an append operation. When an entity needs to be loaded, the events for that entity are loaded, and the event handlers that you've declared to handle the events then process each event to produce the current entity state.

我的问题是lagom如何识别哪些事件属于哪个实体。让我们举个例子: 我有一个为用户执行 CRUD 操作的休息服务。对于事件源,我创建了一个 UserEntity class,我在其中为 CUD 操作定义了不同的命令处理程序。 现在,对我来说,当应用程序是 运行 时,lagom 会生成一个 UserEntity 实例 class。现在,如果我触发 "Alice" 的创建请求并创建 "Bob" 的请求,然后是 "Alice" 的更新请求,这里有两个实体,Alice 和 Bob。那么lagom如何识别实体"Alice",有两个事件,创建和更新,而对于Bob,只有一个事件创建。它如何将事件绑定到实体?

对您的描述的误解是 lagom 不会创建 a UserEntity。正确的句子是:"Lagom creates one instance of UserEntity for each user in my system".

诀窍在于,无论何时您想要将事件发送到持久实体实例,您都必须按类型和 ID 请求实例:

PersistentEntityRegistry persistentEntities = ...;
PersistentEntityRef<UserCommand> refAlice = 
  persistentEntities.refFor(UserEntity.class, "Alice");

在上面的代码片段中,Lagom 保证您发送到 refAlice 的所有命令只会由该实例处理。

然后,refAlice 发出的任何事件都将绑定到 class 和 ID,因此它们不会与其他实例发出的事件混淆。