I’m working on a project with CQRS pattern. By reading the Akka Guide :: Akka Guide (lightbend.com), I’m confused with keeping track the id of entity.
- In the
Domain Layer
, at the write side, I useEventSourcedBehavior
to generate events, and use theid
of entity and event class name as the tag. By this way, one id is corresponding to one entity (or one behavior object).
The id of entity is generated by myself in the Application Layer.
def apply(id: String): Behavior[Command] = {
val starter = Starter(id)
EventSourcedBehavior
.withEnforcedReplies[Command, Event, Project](
persistenceId = PersistenceId(entityTypeHint = EntityKey.name, entityId = id),
emptyState = starter,
commandHandler = (state, cmd) => state.onCommand(cmd),
eventHandler = (state, event) => state.onEvent(event)
)
.withTagger(event => Set(id, event.getClass.toString)
}
-
At the read side, I want to build the material of entity by the projection. But I don’t know how to assign the param
numberOfInstances
ofShardedDaemonProcess.init()
to create projection, because there are lots of instances of entity in the system, not only 5 shopping-carts in the guide. And the system will produce more entities later. Should theShardedDaemonProcess.init()
be called one more time? -
I suppose that one projection is corresponding to one tag, then one id of entity. By this way, one projection will handle all events of one entity.
-
At the other side, if I take event class name as element of the tag set in the
EventSourcedBehavior
, should the projection corresponding to the event class name handle only the kind of event of any entity? -
Because nobody keeps the track to all ids of entities, should I keep them with a table by myself? Then either the
sharding.entityRefFor(id)
orEventSourcedProvider.eventsByTag(tag)
can get the id.
Thanks.
3 posts - 2 participants