I\'m struggling to get my head around what should happen when rebuilding the model by replaying events from the EventStore, in particular when events may trigger other events t
When you replay events, you're not replaying all of the domain logic that went along with generating those events. Usually in your domain method you'll raise an event; the raising of that event then should update the overall state of that domain object.
For example:
public class Purchase {
private int _id;
private string _name;
private string _address;
private double _amount;
public Purchase(int id, string name, string address) {
//do some business rule checking to determine if event is raised
//perhaps send an email or do some logging
//etc.
if (should_i_raise_event) {
ApplyEvent(new PurchaseMadeEvent() {
ID = id,
Name = name,
Address = address
});
}
}
public UpdatePurchase(int id, double amount) {
//more checking to see if event is to be raised
if (should_i_raise_event) {
ApplyEvent(new PurchaseUpdatedEvent() {
ID = id,
Amount = amount
});
}
}
protected void OnPurchaseMade(PurchaseMadeEvent e){
_id = e.ID;
_name = e.Name;
_address = e.Address;
}
protected void OnPurchaseUpdated(PurchaseUpdatedEvent e){
_id = e.ID;
_amount = e.Amount;
}
}
In this example, when my events are replayed, the OnPurchaseMade
event handler will get executed, not the domain object constructor. Same with the PurchaseUpdatedEvent
- it's event handler will get executed, not the domain method that raised the event.
The event contains everything that you need to update the domain model (and apply the updates to the read model). The domain methods that get executed get you to the point that an event can be raised.
I hope this helps. Let me know if I need to provide more information.
Good luck!!