質問

I have re-written the getting-started-with-event-store project to learn what's going on and now for the test CanSaveExistingAggregate() I am getting a WrongExpectedVersionException. The thing is, in order to try and work out what's going on I would like to know what the expected version should be, how can I find this out? In the test, the line repository.Save(firstSaved, Guid.NewGuid(), d => { }); calculates the expected version as 101 and this is where it fails:

        [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);
    }

And the code where the exception is thrown:

        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();
    }

All the other tests pass (except ThrowsOnGetDeletedAggregate() but I'll ask about that later) but I think this is the only test that has expectedVersion != ThrowsOnGetDeletedAggregate()

役に立ちましたか?

解決

Well it turns out it was just a mistake when writing the code, rather than

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

it should be

var expectedVersion = originalVersion == 0 ? ExpectedVersion.NoStream : originalVersion - 1;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top