core-java
core-java copied to clipboard
Fallback routing by type of fields
Suppose, we have an aggregate with ID of type EntityOneId
. It produces events that have the first field of this type (and, of course, producerId
in the EventContext
). Some of these events are related to another entity with IDs of TypeTwoId
. And these relations are reflected in the fields that have indexes starting from 2.
We want to react to such events in the entity with IDs of EntityTwoId
, and in order to achieve this, we need to implement custom routing in the corresponding repository. It would look like this:
public EntityTwoRepository() {
super();
getEventRouting().route(
SomethingHappenedInEntityOne.class,
(event, context) -> ImmutableSet.of(event.getEntityTwoId())
);
}
What if we implement automatic fall-back routing which would use Proto reflection to find a field which matches the type of the IDs of entities managed by the repository? If there is such a field, and its value is not empty, it would do the ImmutableSet.of()
thing so that the user does not have to tune the routing which seems trivial and obvious.