public async Task Send <T>(ConsumeContext <T> context, IPipe <SagaRepositoryContext <TSaga, T> > next) where T : class { var dbContext = _dbContextFactory.CreateScoped(context); try { Task Send() => WithinTransaction(dbContext, context.CancellationToken, () => { var repositoryContext = new DbContextSagaRepositoryContext <TSaga, T>(dbContext, context, _consumeContextFactory, _lockStrategy); return(next.Send(repositoryContext)); }); var executionStrategy = dbContext.Database.CreateExecutionStrategy(); if (executionStrategy is ExecutionStrategy) { await executionStrategy.ExecuteAsync(Send).ConfigureAwait(false); } else { await Send().ConfigureAwait(false); } } finally { _dbContextFactory.Release(dbContext); } }
public async Task <T> Execute <T>(Func <SagaRepositoryContext <TSaga>, Task <T> > asyncMethod, CancellationToken cancellationToken = default) where T : class { var dbContext = _dbContextFactory.Create(); try { Task <T> Send() => WithinTransaction(dbContext, cancellationToken, () => { var sagaRepositoryContext = new DbContextSagaRepositoryContext <TSaga>(dbContext, cancellationToken); return(asyncMethod(sagaRepositoryContext)); }); var executionStrategy = dbContext.Database.CreateExecutionStrategy(); if (executionStrategy is ExecutionStrategy) { return(await executionStrategy.ExecuteAsync(Send).ConfigureAwait(false)); } else { return(await Send().ConfigureAwait(false)); } } finally { _dbContextFactory.Release(dbContext); } }
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); } }