public void Given_setup_policy_When_restoring_from_snapshot_Then_policy_is_the_same_as_before() { var policy = new InsurancePolicy(); var address = Address.New <InsurancePolicy>(); IAggregateRoot policyAggregate = policy; policyAggregate.Apply(new PolicyCreatedEvent(address.Id)); var durationSetEvent = policyAggregate.ApplyEvent(new PolicyDurationSetEvent(address.Id, TimeSpan.FromDays(30))); var policyAmountEvent = policyAggregate.ApplyEvent(new PolicyAmountSetEvent(address.Id, 20m)); var issuedEvent = policyAggregate.ApplyEvent(new PolicyIssuedEvent(address.Id, DateTimeOffset.Now)); policyAggregate.Apply(new PolicyTimePassedEvent(address.Id, DateTimeOffset.Now.AddMinutes(1))); policyAggregate.Apply(new ClaimFulfilledEvent(address.Id, 2m)); policyAggregate.Apply(new PolicyExpiredEvent(address.Id)); var snapshot = (SnapshotState <PolicyState>)(policy as ISupportSnapshots).BuildSnapshot(); snapshot.Address.Should().BeEquivalentTo(address); snapshot.Id.Should().NotBeNullOrEmpty(); snapshot.Version.Should().Be(policy.Version); snapshot.State.Duration.Should().Be(durationSetEvent.Duration); snapshot.State.IssueDate.Should().Be(issuedEvent.Issued); var restoredSnapshot = ((InsurancePolicy) new InsurancePolicy().RestoreFromSnapshot(snapshot)).BuildSnapshot(); snapshot.Should().BeEquivalentTo(restoredSnapshot, o => o.Excluding(s => s.Id)); }
private static void ApplyEventsToAggregate(int versionToLoad, IEventStream stream, IAggregateRoot aggregate) { if (versionToLoad == 0 || aggregate.Version < versionToLoad) { foreach (var @event in stream.CommittedEvents.Select(x => x.Body)) { aggregate.ApplyEvent(@event); } } }
protected TAggregate Reconstruct(IList <EventBase> events) { if (events == null) { throw new ArgumentNullException(nameof(events)); } IAggregateRoot aggregate = AggregateRoot <TAggregate, TKey> .CreateEmpty(); foreach (var item in events) { aggregate.ApplyEvent(item); } return((TAggregate)aggregate); }
private void ApplyEvent(TEvent @event) { _root.ApplyEvent(@event); OnEventApplied(@event); }