Ejemplo n.º 1
0
        protected async Task TransitionState(ISagaInstance instance, int expectedVersion, SagaMessageContext context)
        {
            instance.ValidateChanges();

            var logName     = instance.Metadata.SagaType.Name;
            var persistence = Configuration.Persistence.Map(logName);
            var commit      = new Commit()
            {
                Id      = instance.SagaId,
                Version = expectedVersion + 1,
                Saga    = instance,
            };

            foreach (var @event in instance.UncommittedEvents)
            {
                commit.Messages.Add(@event.GetDescriptor());
                commit.Events.Add(@event);
            }

            foreach (var message in context.Messages)
            {
                commit.Messages.Add(message);
            }

            await persistence.Persist(commit, context.CancellationToken).ConfigureAwait(false);

            instance.AcceptChanges();
            instance.Update();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Transitions the state of a saga.
        /// </summary>
        /// <param name="transaction">The current <see cref="DbTransaction">database transaction</see>.</param>
        /// <param name="saga">The <see cref="ISagaInstance">saga instance</see> to perform a state transition on.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken">token</see> that can be used to cancel the operation.</param>
        /// <returns>A <see cref="Task">task</see> representing the asynchronous operation.</returns>
        /// <remarks>If the <paramref name="saga"/> is <c>null</c>, no action is performed.</remarks>
        protected virtual async Task TransitionState(DbTransaction transaction, ISagaInstance saga, CancellationToken cancellationToken)
        {
            Arg.NotNull(transaction, nameof(transaction));

            if (saga == null)
            {
                return;
            }

            var connection = transaction.Connection;

            if (saga.Completed)
            {
                if (!saga.IsNew)
                {
                    using (var command = Configuration.Sagas.NewCompleteCommand(saga.Data))
                    {
                        command.Connection  = connection;
                        command.Transaction = transaction;
                        await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                    }
                }

                saga.Complete();
            }
            else
            {
                using (var stream = Configuration.Sagas.Serialize(saga.Data))
                    using (var command = Configuration.Sagas.NewStoreCommand(saga.Data, saga.CorrelationProperty, stream))
                    {
                        command.Connection  = connection;
                        command.Transaction = transaction;
                        await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                    }

                saga.Update();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Transitions the state of a saga.
        /// </summary>
        /// <param name="saga">The <see cref="ISagaInstance">saga instance</see> to perform a state transition on.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken">token</see> that can be used to cancel the operation.</param>
        /// <returns>A <see cref="Task">task</see> representing the asynchronous operation.</returns>
        /// <remarks>If the <paramref name="saga"/> is <c>null</c>, no action is performed.</remarks>
        protected virtual async Task TransitionState(ISagaInstance saga, CancellationToken cancellationToken)
        {
            if (saga == null)
            {
                return;
            }

            if (saga.Completed)
            {
                if (!saga.IsNew)
                {
                    await SagaStorage.Complete(saga.Data, cancellationToken).ConfigureAwait(false);
                }

                saga.Complete();
            }
            else
            {
                await SagaStorage.Store(saga.Data, saga.CorrelationProperty, cancellationToken).ConfigureAwait(false);

                saga.Update();
            }
        }