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);
            }
        }
Exemple #2
0
        async Task SendToInstance <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next, TSaga instance)
            where T : class
        {
            try
            {
                SagaConsumeContext <TSaga, T> sagaConsumeContext =
                    _documentDbSagaConsumeContextFactory.Create(_client, _databaseName, _collectionName, context, instance, true, _requestOptions);

                sagaConsumeContext.LogUsed();

                await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false);

                if (!sagaConsumeContext.IsCompleted)
                {
                    await UpdateDocumentDbSaga(context, instance).ConfigureAwait(false);
                }
            }
            catch (SagaException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SagaException(ex.Message, typeof(TSaga), typeof(T), instance.CorrelationId, ex);
            }
        }
Exemple #3
0
        async Task SendToInstance <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next, TSaga instance)
            where T : class
        {
            try
            {
                SagaConsumeContext <TSaga, T> sagaConsumeContext = _mongoDbSagaConsumeContextFactory.Create(_collection, context, instance);

                sagaConsumeContext.LogUsed();

                await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false);

                if (!sagaConsumeContext.IsCompleted)
                {
                    await UpdateMongoDbSaga(context, instance).ConfigureAwait(false);
                }
            }
            catch (SagaException sex)
            {
                context.LogFault(this, sex, instance?.CorrelationId);

                throw;
            }
            catch (Exception ex)
            {
                throw new SagaException(ex.Message, typeof(TSaga), typeof(T), instance.CorrelationId, ex);
            }
        }
Exemple #4
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);
            }
        }
Exemple #5
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);
            }

            if (sagaConsumeContext == null)
            {
                sagaConsumeContext = await context.Load(_correlationId).ConfigureAwait(false);
            }

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

                    await _policy.Existing(sagaConsumeContext, _next).ConfigureAwait(false);
                }
                finally
                {
                    switch (sagaConsumeContext)
                    {
                    case IAsyncDisposable asyncDisposable:
                        await asyncDisposable.DisposeAsync().ConfigureAwait(false);

                        break;

                    case IDisposable disposable:
                        disposable.Dispose();
                        break;
                    }
                }
            }
            else
            {
                var missingPipe = new MissingSagaPipe <TSaga, T>(context, _next);

                await _policy.Missing(context, missingPipe).ConfigureAwait(false);
            }
        }