if you have an Event Sourcing architecture it means you already store your event in an "append-only" event store repository. From an architecture point of view you need to deserialize events and re-apply them (in memory projection) to reconstruct the state of your aggregate.
EventStore interface would look like something like this:
public interface EventStore {
public void appendWith(EventStreamId aStartingIdentity, List<DomainEvent> anEvents);
public void close();
public EventStream eventStreamSince(EventStreamId anIdentity);
public EventStream fullEventStreamFor(EventStreamId anIdentity);
}
Then in your repository you pass all the event stream to your aggregate, which will be responsible of applying the in memory projection:
public class EventStoreForumRepository
extends EventStoreProvider
implements ForumRepository {
@Override
public Forum forumOfId(Tenant aTenant, ForumId aForumId) {
// snapshots not currently supported; always use version 1
EventStreamId eventId = new EventStreamId(aTenant.id(), aForumId.id());
EventStream eventStream = this.eventStore().eventStreamSince(eventId);
Forum forum = new Forum(eventStream.events(), eventStream.version());
return forum;
}
}
Then the aggregate part:
public abstract class EventSourcedRootEntity {
private List<DomainEvent> mutatingEvents;
private int unmutatedVersion;
public int mutatedVersion() {
return this.unmutatedVersion() + 1;
}
public List<DomainEvent> mutatingEvents() {
return this.mutatingEvents;
}
public int unmutatedVersion() {
return this.unmutatedVersion;
}
protected EventSourcedRootEntity(List<DomainEvent> anEventStream, int aStreamVersion) {
for (DomainEvent event : anEventStream) {
this.mutateWhen(event);
}
this.setUnmutatedVersion(aStreamVersion);
}
}
Your aggregate should extend the EventSourcedRootEntity, and the EventStore should manipulate the mutatingEvents on saving (saving only the new ones).
The samples are written in Java and taken from the repository of Vaughn Vernon, the author of Implementing Domain Driven Design (IDDD) book.
https://github.com/VaughnVernon/IDDD_Samples