satish
satish

Reputation: 2441

Event Sourcing and rebuilding logic

I am new to event sourcing and i am bit confused about Rebuilding the Objects from the event Stream.

I believe we need to run load all the events happened from the chronological order to rebuild the object state. So for Example

If i have a Object Called customer.

Public class Customer
{
   public void Correctname(string firstName,string lastName)
    {
        CustomerNameChanged(new nameChangedEvent(firstName,lastName);
    }
}

If the Customer changed the Name twice we will be storing the event twice in the eventlog and when i rebuild the events to object i will get the event twice . Is it needed to take the previous event or archieve the events so that we dont rerun the last event again

Upvotes: 0

Views: 718

Answers (2)

Martijn van den Broek
Martijn van den Broek

Reputation: 1381

When rebuilding an object you process the entire event stream for that object. Performance wise this is usually not an isssue, even for large numbers of events. You can mitigate this by using Rolling Snapshots.

With snapshots you store the state of your object at a particular point of the event stream. Rebuilding is simply loading that snapshot + events that happened after the snapshot was taken.

Upvotes: 2

David Masters
David Masters

Reputation: 8295

You would re-apply both events to the Customer object. Because you apply them in chronological order the Customer object will be in the correct current state. If you are concerned about the number of events being applied that no longer represent the current state, you should look at Snapshots

Upvotes: 3

Related Questions