public async Task returns_failure_status_when_conditionally_appending_with_version_mismatch() { var stream = _fixture.GetStreamName(); var result = await _fixture.Client.ConditionalAppendToStreamAsync(stream, new StreamRevision(7), _fixture.CreateTestEvents()); Assert.Equal( ConditionalWriteResult.FromWrongExpectedVersion(new WrongExpectedVersionException(stream, 7, -1)), result); }
public static void CheckConditionalWriteResultStatus(ConditionalWriteResult writeResult, string id) { switch (writeResult.Status) { case ConditionalWriteStatus.Succeeded: break; case ConditionalWriteStatus.VersionMismatch: throw new ConcurrencyException(id, null); case ConditionalWriteStatus.StreamDeleted: throw new InvalidOperationException($"Stream was deleted. StreamId: {id}"); default: throw new InvalidOperationException($"Unexpected write result: {writeResult.Status}"); } }
public async Task HandleAsync(ICommand _command) { var cmd = _command as I本を借りるCommand; if (cmd == null) { throw new ArgumentException(nameof(_command), "I本を借りるCommand型ではありません。"); } var 利用者EventData = new EventData( Guid.NewGuid(), Domain.RentalSubDomain.Events.User.LendedBookVer100, true, JsonSerializer.Serialize(Domain.RentalSubDomain.Events.User.LendedBookDTOVer100.Create(cmd.利用者のID.ID文字列, cmd.本のID.ID文字列)), new byte[] { } ); var 本EventData = new EventData( Guid.NewGuid(), Domain.RentalSubDomain.Events.Book.LendedBookVer100, true, JsonSerializer.Serialize( Domain.RentalSubDomain.Events.Book.LendedBookDTOVer100.Create( cmd.本のID.ID文字列, cmd.利用者のID.ID文字列, cmd.貸出期間.貸出期間自DateTime, cmd.貸出期間.貸出期間至DateTime ) ), new byte[] { } ); using (var c = EventStoreConnection.Create( ConnectionSettings.Create() .SetDefaultUserCredentials(Connection.UserCredentials()) // .UseConsoleLogger() , Connection.EventStoreUri())) { try { await c.ConnectAsync(); ConditionalWriteResult 利用者Result = await c.ConditionalAppendToStreamAsync(cmd.利用者のID.ID文字列, ExpectedVersion.Any, new [] { 利用者EventData }); ConditionalWriteResult 本Result = await c.ConditionalAppendToStreamAsync(cmd.本のID.ID文字列, ExpectedVersion.Any, new [] { 本EventData }); if (ConditionalWriteStatus.Succeeded.Equals(利用者Result.Status) && ConditionalWriteStatus.Succeeded.Equals(本Result.Status)) { await c.AppendToStreamAsync(cmd.利用者のID.ID文字列, ExpectedVersion.Any, new [] { 利用者EventData }); await c.AppendToStreamAsync(cmd.本のID.ID文字列, ExpectedVersion.Any, new [] { 本EventData }); } else { Logger.LogError($"利用者Result.Status = {Enum.GetName(typeof(ConditionalWriteStatus), 利用者Result.Status)}, 本Result.Status = {Enum.GetName(typeof(ConditionalWriteStatus), 本Result.Status)}"); } } finally { c.Close(); } } }
public async Task HandleAsync(ICommand _command) { var cmd = _command as I本を登録するCommand; if (cmd == null) { throw new ArgumentException(nameof(_command), "I本を登録するCommand型ではありません。"); } var _書籍のID = 書籍のID.New(); var _本のID = 本のID.New(); var 書籍のEventData = new EventData( Guid.NewGuid(), Domain.RentalSubDomain.Events.BookInfo.AddedBookInfoVer100, true, JsonSerializer.Serialize(Domain.RentalSubDomain.Events.BookInfo.AddedBookInfoDTOVer100.Create(_書籍のID.ID文字列, cmd.タイトル.タイトル, cmd.ISBN.ISBN)), new byte[] { } ); var 本のEventData = new EventData( Guid.NewGuid(), Domain.RentalSubDomain.Events.Book.AddedBookVer100, true, JsonSerializer.Serialize(Domain.RentalSubDomain.Events.Book.AddedBookDTOVer100.Create(_本のID.ID文字列, _書籍のID.ID文字列)), new byte[] { } ); using (var c = EventStoreConnection.Create( ConnectionSettings.Create().SetDefaultUserCredentials(Connection.UserCredentials()) , Connection.EventStoreUri())) { try { await c.ConnectAsync(); ConditionalWriteResult 書籍Result = await c.ConditionalAppendToStreamAsync(_書籍のID.ID文字列, ExpectedVersion.NoStream, new [] { 書籍のEventData }); ConditionalWriteResult 本Result = await c.ConditionalAppendToStreamAsync(_本のID.ID文字列, ExpectedVersion.NoStream, new [] { 本のEventData }); if (ConditionalWriteStatus.Succeeded.Equals(書籍Result.Status) && ConditionalWriteStatus.Succeeded.Equals(本Result.Status)) { await c.AppendToStreamAsync(_書籍のID.ID文字列, ExpectedVersion.NoStream, 書籍のEventData); await c.AppendToStreamAsync(_本のID.ID文字列, ExpectedVersion.NoStream, 本のEventData); await c.CreatePersistentSubscriptionAsync(_書籍のID.ID文字列, Domain.GeneralSubDomain.Aggregate.BookInfo, PersistentSubscriptionSettings.Create(), Connection.UserCredentials()); await c.CreatePersistentSubscriptionAsync(_本のID.ID文字列, Domain.GeneralSubDomain.Aggregate.Book, PersistentSubscriptionSettings.Create(), Connection.UserCredentials()); MessageBroker.Publish <I本が登録されたEvent>(本が登録されたEvent.Create(_書籍のID)); } else { Logger.LogError($"書籍Result.Status = {Enum.GetName(typeof(ConditionalWriteStatus), 書籍Result.Status)}, 本Result.Status = {Enum.GetName(typeof(ConditionalWriteStatus), 本Result.Status)}"); } } finally { c.Close(); } } }