2013-11-23 54 views
2

我已經重寫了開始使用事件存儲項目以瞭解正在進行的操作以及現在的測試CanSaveExistingAggregate()我收到了WrongExpectedVersionException。問題是,爲了試圖弄清楚發生了什麼,我想知道預期的版本應該是什麼,我怎麼能找到它?在測試中,該行repository.Save(firstSaved, Guid.NewGuid(), d => { });計算預期的版本爲101,這是它失敗:EventStore上的WrongExpectedVersionException入門項目

 [Test] 
    public void CanSaveExistingAggregate() 
    { 
     var savedId = SaveTestAggregateWithoutCustomHeaders(repository, 100 /* excludes TestAggregateCreated */); 

     var firstSaved = repository.GetById<TestAggregate>(savedId); 
     Console.WriteLine("version:" + firstSaved.Id); 
     firstSaved.ProduceEvents(50); 
     repository.Save(firstSaved, Guid.NewGuid(), d => { }); 

     var secondSaved = repository.GetById<TestAggregate>(savedId); 
     Assert.AreEqual(150, secondSaved.AppliedEventCount); 
    } 

而且在拋出異常的代碼:

 public void Save(CommonDomain.IAggregate aggregate, Guid commitId, Action<IDictionary<string, object>> updateHeaders) 
    { 
     var commitHeaders = new Dictionary<string, object> 
     { 
      {CommitIdHeader, commitId}, 
      {AggregateClrTypeHeader, aggregate.GetType().AssemblyQualifiedName} 
     }; 
     updateHeaders(commitHeaders); 

     var streamName = aggregateIdToStreamName(aggregate.GetType(), aggregate.Id); 
     var newEvents = aggregate.GetUncommittedEvents().Cast<object>().ToList(); 
     var originalVersion = aggregate.Version - newEvents.Count; 
     var expectedVersion = originalVersion == 0 ? ExpectedVersion.NoStream : originalVersion; 
     var eventsToSave = newEvents.Select(e => ToEventData(Guid.NewGuid(), e, commitHeaders)).ToList(); 
     if (eventsToSave.Count < WritePageSize) 
     { 
      eventStoreConnection.AppendToStream(streamName, expectedVersion, eventsToSave); 
     } 
     else 
     { 
      var transaction = eventStoreConnection.StartTransaction(streamName, expectedVersion); 

      var position = 0; 
      while (position < eventsToSave.Count) 
      { 
       var pageEvents = eventsToSave.Skip(position).Take(WritePageSize); 
       transaction.Write(pageEvents); 
       position += WritePageSize; 
      } 

      transaction.Commit(); 
     } 

     aggregate.ClearUncommittedEvents(); 
    } 

所有其他測試通過(除ThrowsOnGetDeletedAggregate()但我稍後會問)但我認爲這是唯一的測試expectedVersion != ThrowsOnGetDeletedAggregate()

回答

4

事實證明,這只是一個錯誤,而不是編寫代碼時

var expectedVersion = originalVersion == 0 ? ExpectedVersion.NoStream : originalVersion; 

應該

var expectedVersion = originalVersion == 0 ? ExpectedVersion.NoStream : originalVersion - 1; 
+1

恰好碰到了這個問題 - 看起來像他們的文檔只是直線上升錯接。感謝您的決議! – eouw0o83hf