public async Task SaveEvents_inserts_Correlation_entity_correctly( FakeUserCreated created, Guid correlationId) { RaiseEvents(userId, created); var now = DateTimeOffset.Now; await sut.SaveEvents <FakeUser>(new[] { created }, correlationId); using (var db = new DataContext()) { Correlation correlation = await db .Correlations .Where( c => c.AggregateId == userId && c.CorrelationId == correlationId) .SingleOrDefaultAsync(); correlation.Should().NotBeNull(); correlation.HandledAt.Should().BeCloseTo(now); } }
public async Task SaveEvents_inserts_Correlation_entity_correctly() { FakeUserCreated created = _fixture.Create <FakeUserCreated>(); var correlationId = Guid.NewGuid(); RaiseEvents(_userId, created); DateTime now = DateTime.UtcNow; await _sut.SaveEvents <FakeUser>(new[] { created }, correlationId : correlationId); using (var db = new DataContext()) { Correlation correlation = await db .Correlations .Where( c => c.AggregateId == _userId && c.CorrelationId == correlationId) .SingleOrDefaultAsync(); correlation.Should().NotBeNull(); correlation.HandledAt.Should().BeCloseTo(now); } }
private async Task SaveChanges <T>( EventStoreDbContext context, Guid sourceId, Guid?correlationId, CancellationToken cancellationToken) where T : class, IEventSourced { #if NETSTANDARD2_0 try { using (IDbContextTransaction transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false)) { await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); transaction.Commit(); } } catch (DbUpdateException exception) when(correlationId.HasValue) { IQueryable <Correlation> query = from c in context.Correlations where c.AggregateId == sourceId && c.CorrelationId == correlationId select c; if (await query.AnyAsync().ConfigureAwait(false)) { throw new DuplicateCorrelationException( typeof(T), sourceId, correlationId.Value, exception); } throw; } #else try { await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } catch (DbUpdateException exception) when(exception.InnerException is UpdateException) { var updateException = (UpdateException)exception.InnerException; Correlation correlation = updateException .StateEntries .Select(s => s.Entity) .OfType <Correlation>() .FirstOrDefault(); if (correlation != null) { throw new DuplicateCorrelationException( typeof(T), sourceId, correlation.CorrelationId, exception); } throw; } #endif }