How to determine the last event in a stream or projection

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);
}

``

Another way of doing this would be to just read back the last event in the stream and look at its version number (eg readbackwards one event)

Sounds good, the I’ll just wire the first function into the second,
public bool IsStreamAt(string streamName, int version)

{

int position;

return (TryGetstreamVersion(streamName, out position)) && position == version;

}

``

Thanks!

Actually the fastest way would be to use single event read instead of stream read. Just ReadEvent(stream, -1).