Can I have DomainEvent as a field of my aggregate root?

Hi all,
Here I want to implement a feature that undo the last action. Can I have a field of type DomainEvent like this?

Let the code speak:

public class AggregateRoot{

private DomainEvent lastEvent;

@EventHandler
protected void handle(XXEvent event) {
//change some other state
lastEvent = event;
}

public undo(){
if ( lastEvent instance of XXEvent){
//apply some specific event to revert the state
}
}
}

Keats

Hi,

it should be possible to keep track of the last event like this. My initial thought was to make it a transient field and have it reconstructed by event sourcing, but then you’d have a problem if the aggregate is reconstructed (directly) from a snapshot.

Since domain events are (read: should be) serializable, doing it like this should not get you in trouble. I haven’t tried yet, though.

Cheers,

Allard