Fair warning: I have no idea I’m doing, so even the asking of this question may go awry.
I’m wanting to update state on a simple object (the Aggregate) and then provide the UI with a projection of the changed object. Here’s my aggregate object (command handler exists, but not shown here).
@Aggregate
public class Widget {
@AggregateIdentifier
private String id;
private String color;
…
@EventSourcingHandler
public void on(ChangeColorEvt evt) {
color = evt.getColor();
}
…
}
…and here is my projection:
public class WidgetProjection {
private final EntityManager entityManager;
private final QueryUpdateEmitter queryUpdateEmitter;
…
@EventHandler
public void on(ChangeColorEvt evt) {
ProjectedWidget projection = entityManager.find(ProjectedWidget.class, evt.getId());
projection.setColor(evt.getColor());
queryUpdateEmitter.emit(FetchWidget.class, query → evt.getId().startsWith(query.getFilter().getIdStartsWith()), projection);
}
…
}
The QueryHandler does what you would expect, finding the instance and returning it.
So here are my questions:
Why do I need the EventSourcingHandler in the Aggregate? That is, it appears to do work, but the result isn’t stored or sent anywhere.
So, that becomes my next question: After executing the EventSourcing Handler, is the resulting instance of Widget (not the projection) stored, seen, or sent anywhere?
If EventSourcingHandler is indeed needed, is there any way to avoid having two independent copies of my business logic (one in EventSourcingHandler, and one EventHandler)? I really hate the idea of having to update the same business logic in two places.
I appreciate any clarity the group can provide. Thanks for your help!
Jonathan