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