Hi all,
what is the best solution/pattern to extract all events for an aggregate ?
I’ve write something like this :
`
List retour = new ArrayList<>();
DomainEventStream domainEventStream = eventStore.readEvents(id);
while(domainEventStream.hasNext()) {
DomainEventMessage<?> event = domainEventStream.next();
retour.add(event.getPayloadType().toString());
}
return retour;
`
Thanks
Allard
(Allard)
March 10, 2017, 10:59am
2
Hi Philippe,
your code is fine, except that the DomainEventStream may eventually contain more items than you’d want to put in a List. You can also work with java Streams by using domainEventStream.asStream().
In any case, you’d want to use the EventStore interface for this. Leave the EventStorageEngine as a technical storage detail as much as possible.
Cheers,
Allard
Hi Allard,
thanks for your response.
I’ve wrote this endpoint to access of the history of an aggregrate (for the moment i haven’t implemented items limit)
`
@RequestMapping (method = GET, value = “/{id}/history”, produces = MediaType.APPLICATION_JSON_VALUE)
public List history(
@PathVariable String id)
{
return eventStore.readEvents(id).asStream().map(this::domainEventToAggregateHistory).collect(Collectors.toList());
}
private AggregateHistoryDTO domainEventToAggregateHistory(DomainEventMessage<?> event)
{
return new AggregateHistoryDTO(event.getPayloadType().getSimpleName(), (BopEvent) event.getPayload(), event.getSequenceNumber(),
event.getTimestamp());
}
`
`
@AllArgsConstructor
@Getter
public class AggregateHistoryDTO
{
private String name;
private BopEvent event; // All my events extends BopEvent
private long sequenceNumber;
private Instant timestamp;
}
`
Cheers