Пример #1
0
        public async Task SendQuery <T>(ConsumeContext <T> context, ISagaQuery <TSaga> query, IPipe <SagaRepositoryQueryContext <TSaga, T> > next)
            where T : class
        {
            var repositoryContext = new MongoDbSagaRepositoryContext <TSaga, T>(_mongoCollection, context, _factory);

            IList <TSaga> instances = await _mongoCollection.Find(query.FilterExpression)
                                      .ToListAsync(repositoryContext.CancellationToken)
                                      .ConfigureAwait(false);

            var queryContext = new LoadedSagaRepositoryQueryContext <TSaga, T>(repositoryContext, instances);

            await next.Send(queryContext).ConfigureAwait(false);
        }
Пример #2
0
        public async Task SendQuery <T>(ConsumeContext <T> context, ISagaQuery <TSaga> query, IPipe <SagaRepositoryQueryContext <TSaga, T> > next)
            where T : class
        {
            using DapperDatabaseContext <TSaga> databaseContext = await CreateDatabaseContext(context.CancellationToken).ConfigureAwait(false);

            IEnumerable <TSaga> instances = await databaseContext.QueryAsync(query.FilterExpression, context.CancellationToken).ConfigureAwait(false);

            var repositoryContext = new DapperSagaRepositoryContext <TSaga, T>(databaseContext, context, _factory);

            var queryContext = new LoadedSagaRepositoryQueryContext <TSaga, T>(repositoryContext, instances);

            await next.Send(queryContext).ConfigureAwait(false);

            databaseContext.Commit();
        }
Пример #3
0
        public async Task SendQuery <T>(ConsumeContext <T> context, ISagaQuery <TSaga> query, IPipe <SagaRepositoryQueryContext <TSaga, T> > next)
            where T : class
        {
            using var session = _documentStore.DirtyTrackedSession();

            var repositoryContext = new MartenSagaRepositoryContext <TSaga, T>(session, context, _factory);

            IReadOnlyList <TSaga> instances = await session.Query <TSaga>()
                                              .Where(query.FilterExpression)
                                              .ToListAsync().ConfigureAwait(false);


            var queryContext = new LoadedSagaRepositoryQueryContext <TSaga, T>(repositoryContext, instances.ToList());

            await next.Send(queryContext).ConfigureAwait(false);
        }
        public async Task SendQuery <T>(ConsumeContext <T> context, ISagaQuery <TSaga> query, IPipe <SagaRepositoryQueryContext <TSaga, T> > next)
            where T : class
        {
            // This will not work for Document Db because the .Where needs to look for [JsonProperty("id")],
            // and if you pass in CorrelationId property, the ISaga doesn't have that property. Can we .Select() it out?
            IEnumerable <TSaga> sagas = await _context.Client.CreateDocumentQuery <TSaga>(_context.Collection, _context.FeedOptions)
                                        .Where(query.FilterExpression)
                                        .QueryAsync(context.CancellationToken)
                                        .ConfigureAwait(false);

            var repositoryContext = new DocumentDbSagaRepositoryContext <TSaga, T>(_context, context, _factory);

            var queryContext = new LoadedSagaRepositoryQueryContext <TSaga, T>(repositoryContext, sagas.ToList());

            await next.Send(queryContext).ConfigureAwait(false);
        }
Пример #5
0
        public async Task SendQuery <T>(ConsumeContext <T> context, ISagaQuery <TSaga> query, IPipe <SagaRepositoryQueryContext <TSaga, T> > next)
            where T : class
        {
            var session = _sessionFactory.OpenSession();

            ITransaction transaction = null;

            try
            {
                transaction = session.BeginTransaction();

                var repositoryContext = new NHibernateSagaRepositoryContext <TSaga, T>(session, context, _factory);

                IList <TSaga> instances = await session.QueryOver <TSaga>()
                                          .Where(query.FilterExpression)
                                          .ListAsync(repositoryContext.CancellationToken)
                                          .ConfigureAwait(false);

                var queryContext = new LoadedSagaRepositoryQueryContext <TSaga, T>(repositoryContext, instances);

                await next.Send(queryContext).ConfigureAwait(false);

                await transaction.CommitAsync(repositoryContext.CancellationToken).ConfigureAwait(false);
            }
            catch (Exception)
            {
                if (transaction != null && transaction.IsActive)
                {
                    try
                    {
                        await transaction.RollbackAsync(context.CancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception rollbackException)
                    {
                        LogContext.Warning?.Log(rollbackException, "Failed to rollback transaction");
                    }
                }

                throw;
            }
            finally
            {
                transaction?.Dispose();

                session.Dispose();
            }
        }
        public async Task SendQuery <T>(ConsumeContext <T> context, ISagaQuery <TSaga> query, IPipe <SagaRepositoryQueryContext <TSaga, T> > next)
            where T : class
        {
            var dbContext = _dbContextFactory.CreateScoped(context);

            try
            {
                async Task Send()
                {
                    var lockContext = await _lockStrategy.CreateLockContext(dbContext, query, context.CancellationToken).ConfigureAwait(false);

                    var repositoryContext = new DbContextSagaRepositoryContext <TSaga, T>(dbContext, context, _consumeContextFactory, _lockStrategy);

                    await WithinTransaction(dbContext, context.CancellationToken, async() =>
                    {
                        var instances = await lockContext.Load().ConfigureAwait(false);

                        var queryContext = new LoadedSagaRepositoryQueryContext <TSaga, T>(repositoryContext, instances);

                        await next.Send(queryContext).ConfigureAwait(false);
                    }).ConfigureAwait(false);
                }

                var executionStrategy = dbContext.Database.CreateExecutionStrategy();
                if (executionStrategy is ExecutionStrategy)
                {
                    await executionStrategy.ExecuteAsync(Send).ConfigureAwait(false);
                }
                else
                {
                    await Send().ConfigureAwait(false);
                }
            }
            finally
            {
                _dbContextFactory.Release(dbContext);
            }
        }