Пример #1
0
    async Task <Try> TryUpdateEmbedding(ProjectionKey key, ExecutionContext executionContext, CancellationToken cancellationToken)
    {
        var currentState = await _embeddingStore.TryGet(_embedding, key, cancellationToken).ConfigureAwait(false);

        if (!currentState.Success)
        {
            return(currentState);
        }
        var unprocessedEvents = await TryGetUnprocessedEvents(key, currentState.Result.Version, cancellationToken).ConfigureAwait(false);

        if (!unprocessedEvents.Success)
        {
            return(Try.Failed(unprocessedEvents.Exception));
        }
        if (!unprocessedEvents.Result.Any())
        {
            return(Try.Succeeded());
        }

        var projectedState = await _projectManyEvents.TryProject(currentState, unprocessedEvents, executionContext, cancellationToken).ConfigureAwait(false);

        if (projectedState.Success || projectedState.IsPartialResult)
        {
            var updateState = await TryUpdateOrDeleteState(projectedState.Result, cancellationToken).ConfigureAwait(false);

            if (updateState.Success && projectedState.IsPartialResult)
            {
                return(projectedState.Exception);
            }

            return(updateState);
        }

        return(Try.Failed(projectedState.Exception));
    }
    async Task <Try <EmbeddingCurrentState> > TryProjectNewState(
        EmbeddingCurrentState current,
        Try <UncommittedEvents> newTransitionEvents,
        IEnumerable <ProjectionState> previousStates,
        ExecutionContext executionContext,
        CancellationToken cancellationToken)
    {
        var intermediateState = await _projector.TryProject(current, newTransitionEvents.Result, executionContext, cancellationToken).ConfigureAwait(false);

        if (!intermediateState.Success)
        {
            return(intermediateState.IsPartialResult
                ? new CouldNotProjectAllEvents(_embeddingId, intermediateState.Exception)
                : new FailedProjectingEvents(_embeddingId, intermediateState.Exception));
        }

        var loopDetected = _loopDetector.TryCheckForProjectionStateLoop(intermediateState.Result.State, previousStates);

        return(loopDetected switch
        {
            { Success : false } => loopDetected.Exception,