public ProjectionEvent Get(Projection projection) { Guard.AgainstNull(projection, nameof(projection)); var projectionAggregation = _eventProcessor.GetProjectionAggregation(projection.AggregationId); Guard.AgainstNull(projectionAggregation, nameof(projectionAggregation)); if (!projectionAggregation.ContainsProjection(projection.Name)) { throw new InvalidOperationException(string.Format(Resources.ProjectionNotInAggregationException, projection.Name, projectionAggregation.Id)); } lock (projectionAggregation) { var sequenceNumber = projection.SequenceNumber + 1; if (!projectionAggregation.ContainsPrimitiveEvent(sequenceNumber)) { foreach (var primitiveEvent in _repository.Get(sequenceNumber, sequenceNumber + _configuration.ProjectionEventFetchCount, projectionAggregation.EventTypes)) { projectionAggregation.AddPrimitiveEvent(primitiveEvent); if (primitiveEvent.SequenceNumber > _sequenceNumberHead) { _sequenceNumberHead = primitiveEvent.SequenceNumber; } } } var result = projectionAggregation.GetNextPrimitiveEvent(projection.SequenceNumber); return(result == null ? new ProjectionEvent(_sequenceNumberHead) : new ProjectionEvent(result)); } }