public bool Save(TAggregate aggregate) { var newEvents = aggregate.Events; if (newEvents == null || !newEvents.Any()) { return(false); } var streamName = AggregateIdToStreamName(aggregate.GetType(), aggregate.Id); var originalVersion = aggregate.Version - newEvents.Count; var expectedVersion = originalVersion < 0 ? ExpectedVersion.NoStream : originalVersion; var eventsToSave = newEvents.Select(e => ToEventData(Guid.NewGuid(), e)).ToList(); using (var aggregateTransaction = EventStoreConnection.StartTransactionAsync(streamName, expectedVersion).Result) { try { aggregateTransaction.WriteAsync(eventsToSave).Wait(); aggregateTransaction.CommitAsync().Wait(); aggregate.Events.Clear(); } catch { aggregateTransaction.Rollback(); throw; } } return(true); }
public void should_validate_expectations_on_commit() { const string stream = "should_validate_expectations_on_commit"; using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint)) { var start = store.StartTransactionAsync(stream, 100500); Assert.DoesNotThrow(start.Wait); var write = store.TransactionalWriteAsync(start.Result.TransactionId, stream, new[] { new TestEvent() }); Assert.DoesNotThrow(write.Wait); var commit = store.CommitTransactionAsync(start.Result.TransactionId, start.Result.Stream); Assert.That(() => commit.Wait(), Throws.Exception.TypeOf <AggregateException>().With.InnerException.TypeOf <WrongExpectedVersionException>()); } }
public void should_start_on_non_existing_stream_with_exp_ver_any_and_create_stream_on_commit() { const string stream = "should_start_on_non_existing_stream_with_exp_ver_any_and_create_stream_on_commit"; using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint)) { var start = store.StartTransactionAsync(stream, ExpectedVersion.Any); Assert.DoesNotThrow(start.Wait); var write = store.TransactionalWriteAsync(start.Result.TransactionId, stream, new[] { new TestEvent() }); Assert.DoesNotThrow(write.Wait); var commit = store.CommitTransactionAsync(start.Result.TransactionId, start.Result.Stream); Assert.DoesNotThrow(commit.Wait); } }
public void should_create_stream_if_commits_no_events_to_empty_stream() { const string stream = "should_create_stream_if_commits_no_events_to_empty_stream"; using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint)) { var start = store.StartTransactionAsync(stream, ExpectedVersion.NoStream); Assert.DoesNotThrow(start.Wait); var commit = store.CommitTransactionAsync(start.Result.TransactionId, start.Result.Stream); Assert.DoesNotThrow(commit.Wait); var streamCreated = store.ReadEventStreamForwardAsync(stream, 0, 1); Assert.DoesNotThrow(streamCreated.Wait); Assert.That(streamCreated.Result.Events.Length, Is.EqualTo(1));//stream created event } }
public void should_fail_to_commit_if_started_with_correct_ver_but_on_commit_stream_was_deleted() { const string stream = "should_fail_to_commit_if_started_with_correct_ver_but_on_commit_stream_was_deleted"; using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint)) store.CreateStream(stream, new byte[0]); using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint)) { var start = store.StartTransactionAsync(stream, ExpectedVersion.EmptyStream); Assert.DoesNotThrow(start.Wait); var write = store.TransactionalWriteAsync(start.Result.TransactionId, start.Result.Stream, new[] { new TestEvent() }); Assert.DoesNotThrow(write.Wait); var delete = store.DeleteStreamAsync(stream, ExpectedVersion.EmptyStream); Assert.DoesNotThrow(delete.Wait); var commit = store.CommitTransactionAsync(start.Result.TransactionId, start.Result.Stream); Assert.That(() => commit.Wait(), Throws.Exception.TypeOf <AggregateException>().With.InnerException.TypeOf <StreamDeletedException>()); } }
public void should_not_fail_to_commit_if_started_with_wrong_ver_but_committing_with_correct_ver() { const string stream = "should_not_fail_to_commit_if_started_with_wrong_ver_but_committing_with_correct_ver"; using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint)) store.CreateStream(stream, new byte[0]); using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint)) { var start = store.StartTransactionAsync(stream, 1); Assert.DoesNotThrow(start.Wait); var append = store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, new[] { new TestEvent() }); Assert.DoesNotThrow(append.Wait); var write = store.TransactionalWriteAsync(start.Result.TransactionId, start.Result.Stream, new[] { new TestEvent() }); Assert.DoesNotThrow(write.Wait); var commit = store.CommitTransactionAsync(start.Result.TransactionId, start.Result.Stream); Assert.DoesNotThrow(commit.Wait); } }