public async Task ItShouldStartWithNewSnapshot() { _snapshots .Load <Shopping>("customerId", "messageId") .Returns(Task.FromResult(new Snapshot( -1, "customerId", new Shopping() ))); _events .Load <Shopping>("customerId", "messageId", -1) .Returns(new Envelope[0].ToAsync()); await _events .Append <Shopping>(Arg.Do <IEnumerable <Envelope> >(envelopes => { var first = envelopes.First(); first.Sequence.ShouldBe(0); ((ProductChosen)first.Message).ShouldNotBeNull(); ((ProductChosen)first.Message).CustomerId.ShouldBe("customerId"); ((ProductChosen)first.Message).ProductId.ShouldBe("productId"); })); await _snapshots .Save <Shopping>(Arg.Do <Snapshot>(snapshot => { snapshot.Sequence.ShouldBe(0); snapshot.Identity.ShouldBe("customerId"); snapshot.Snap.ShouldBeOfType <Shopping>(); })); await new CommandHandler <Shopping>(_events, _snapshots, "customerId") .Handle(new ChooseProduct("customerId", "productId"), "messageId"); }
public async Task Handle(object cmd, string messageId = null) { if (null == _snapshot) { _snapshot = await _snapshots.Load <TInvariant>(_identity, messageId); await foreach (var envelope in _events.Load <TInvariant>(_identity, messageId, _snapshot.Sequence)) { _snapshot.Sequence++; _snapshot.Snap = ((IArrange <TInvariant>)_snapshot.Snap).Arrange(envelope.Message); } } _snapshot.Snap = await((IAct <TInvariant>)_snapshot.Snap).Act(cmd); var envelopes = ((IAct <TInvariant>)_snapshot.Snap) .Commit() .Select((evt, i) => new Envelope( _identity, typeof(TInvariant).Name, ++_snapshot.Sequence, evt ).CausedBy(messageId ?? Guid.NewGuid().ToString())) .ToArray(); if (!envelopes.Any()) { return; } await _events.Append <TInvariant>(envelopes); await _snapshots.Save <TInvariant>(_snapshot); }
async Task SetupSrc() { _entities = new List <Guid>(); for (var i = 0; i < MaxEntities; i++) { var id = Guid.NewGuid(); _entities.Add(id); var @event = i % 2 == 0?(object)_fixture.Create <Event1>():_fixture.Create <Event2>(); _events.Add(@event); await _src.Append(id, Guid.NewGuid(), @event); } }