Hi,
I wanted to add a quick way to check for staleness in a aggregate without attempting a write operation so I added a couple of methods to the
GetEventStoreRepository
class.
So is there a better way to do this? Is there anything I’m missing here?
Thanks,
Chris
public bool IsStreamAt(string streamName, int version)
{
try {
var currentSlice = _eventStoreConnection.ReadStreamEventsForward(streamName, version, 1, false);
if (currentSlice.Status == SliceReadStatus.StreamNotFound) return false;
if (currentSlice.Status == SliceReadStatus.StreamDeleted) return false;
return currentSlice.IsEndOfStream;
}
catch (Exception ex) { return false; }
}
public bool TryGetstreamVersion(string streamName, out int version)
{
version = 0;
try {
var currentSlice = _eventStoreConnection.ReadStreamEventsBackward(streamName, StreamPosition.End, 1, false);
if (currentSlice.Status == SliceReadStatus.StreamNotFound) return false;
if (currentSlice.Status == SliceReadStatus.StreamDeleted) return false;
version = currentSlice.LastEventNumber;
return true;
}
catch (Exception ex) { return false; }
}
public bool IsCurrent(IAggregate aggregate)
{
if (aggregate.GetUncommittedEvents().Cast().Any()) return false;
if (aggregate.Version < 0) return false;
var streamName = _aggregateIdToStreamName(aggregate.GetType(), aggregate.Id);
return IsStreamAt(streamName, aggregate.Version);
}
``