public async Task Send(SagaRepositoryQueryContext <TSaga, T> context)
        {
            if (context.Count > 0)
            {
                async Task LoadInstance(Guid correlationId)
                {
                    SagaConsumeContext <TSaga, T> sagaConsumeContext = await context.Load(correlationId).ConfigureAwait(false);

                    if (sagaConsumeContext != null)
                    {
                        sagaConsumeContext.LogUsed();

                        try
                        {
                            await _policy.Existing(sagaConsumeContext, _next).ConfigureAwait(false);

                            if (_policy.IsReadOnly)
                            {
                                await context.Undo(sagaConsumeContext).ConfigureAwait(false);
                            }
                            else
                            {
                                if (sagaConsumeContext.IsCompleted)
                                {
                                    await context.Delete(sagaConsumeContext).ConfigureAwait(false);

                                    sagaConsumeContext.LogRemoved();
                                }
                                else
                                {
                                    await context.Update(sagaConsumeContext).ConfigureAwait(false);
                                }
                            }
                        }
                        finally
                        {
                            switch (sagaConsumeContext)
                            {
                            case IAsyncDisposable asyncDisposable:
                                await asyncDisposable.DisposeAsync().ConfigureAwait(false);

                                break;

                            case IDisposable disposable:
                                disposable.Dispose();
                                break;
                            }
                        }
                    }
                }

                await Task.WhenAll(context.Select(LoadInstance)).ConfigureAwait(false);
            }
            else
            {
                var missingPipe = new MissingSagaPipe <TSaga, T>(context, _next);

                await _policy.Missing(context, missingPipe).ConfigureAwait(false);
            }
        }
Example #2
0
        public async Task Send(SagaRepositoryContext <TSaga, T> context)
        {
            SagaConsumeContext <TSaga, T> sagaConsumeContext = null;

            if (_policy.PreInsertInstance(context, out var instance))
            {
                sagaConsumeContext = await context.Insert(instance).ConfigureAwait(false);
            }

            sagaConsumeContext ??= await context.Load(_correlationId).ConfigureAwait(false);

            if (sagaConsumeContext != null)
            {
                try
                {
                    sagaConsumeContext.LogUsed();

                    await _policy.Existing(sagaConsumeContext, _next).ConfigureAwait(false);

                    if (_policy.IsReadOnly)
                    {
                        await context.Undo(sagaConsumeContext).ConfigureAwait(false);
                    }
                    else
                    {
                        if (sagaConsumeContext.IsCompleted)
                        {
                            await context.Delete(sagaConsumeContext).ConfigureAwait(false);

                            sagaConsumeContext.LogRemoved();
                        }
                        else
                        {
                            await context.Update(sagaConsumeContext).ConfigureAwait(false);
                        }
                    }
                }
                finally
                {
                    switch (sagaConsumeContext)
                    {
                    case IAsyncDisposable asyncDisposable:
                        await asyncDisposable.DisposeAsync().ConfigureAwait(false);

                        break;

                    case IDisposable disposable:
                        disposable.Dispose();
                        break;
                    }
                }
            }
            else
            {
                await _policy.Missing(context, new MissingSagaPipe <TSaga, T>(context, _next)).ConfigureAwait(false);
            }
        }