Example #1
0
        public void Save(AggregateRoot aggregateRoot, int expectedVersion)
        {
            var events          = aggregateRoot.GetUncommittedChanges();
            var lastStoredEvent = Events
                                  .Where(e => e.Id == aggregateRoot.Id)
                                  .OrderByDescending(ev => ev.Version)
                                  .FirstOrDefault();
            var lastVersion = lastStoredEvent == null ? -1 : lastStoredEvent.Version;

            if (lastVersion != expectedVersion)
            {
                throw new ConcurrencyException();
            }
            foreach (var @event in events)
            {
                var serializedEvent = _eventsSerializer.SerializeEvent(@event);
                lastVersion = @event.Version;
                Events.Add(new EventDescriptor
                {
                    Version = @event.Version,
                    Id      = aggregateRoot.Id,
                    Data    = serializedEvent.Data,
                    Type    = serializedEvent.Type
                });
                _bus.Send(@event);
            }
            var snapshottableAggregateRoot = aggregateRoot as SnapshottableAggregateRoot;

            if (snapshottableAggregateRoot != null && snapshottableAggregateRoot.ShouldCreateSnapshot())
            {
                var snapshot = snapshottableAggregateRoot.GetSnapshot();
                snapshot.Version = lastVersion;
                _snapshotStore.SaveSnapshot(snapshot);
            }
        }
 public when_saving_a_new_snapshotFixture(): base()
 {
     Snapshot = new Snapshot(Guid.NewGuid(), 1, new TestSnapshot { Name = "TestName" });
     FolderName = Snapshot.EventSourceId.ToString().Substring(0, 2);
     FileName = Snapshot.EventSourceId.ToString().Substring(2) + ".ss";
     SnapshotStore.SaveSnapshot(Snapshot);
 }
Example #3
0
 public when_getting_the_snapshot()
 {
     Snapshot = new Snapshot(Guid.NewGuid(), 1, new TestSnapshot {
         Name = "Name"
     });
     SnapshotStore.SaveSnapshot(Snapshot);
     _retrieved = SnapshotStore.GetSnapshot(Snapshot.EventSourceId, long.MaxValue);
 }
 public void SetUp()
 {
     Snapshot = new Snapshot(Guid.NewGuid(), 1, new TestSnapshot {
         Name = "Name"
     });
     SnapshotStore.SaveSnapshot(Snapshot);
     _retrieved = SnapshotStore.GetSnapshot(Snapshot.EventSourceId, long.MaxValue);
 }
 public void SetUp()
 {
     Snapshot = new Snapshot(Guid.NewGuid(), 1, new TestSnapshot {
         Name = "TestName"
     });
     _foldername = Snapshot.EventSourceId.ToString().Substring(0, 2);
     _filename   = Snapshot.EventSourceId.ToString().Substring(2) + ".ss";
     SnapshotStore.SaveSnapshot(Snapshot);
 }
Example #6
0
        public void SnapshotStoreLoadsSnapshotFromPersistentStore()
        {
            var persistentSnapshotStore = new InMemorySnapshotStore();
            var snapshotStore           = new SnapshotStore(persistentSnapshotStore);

            var guid      = Guid.NewGuid();
            var aggregate = new TestSnapshotAggregate(guid, "Test aggregate");

            snapshotStore.SaveSnapshot(aggregate);

            snapshotStore.GetSnapshot(guid).Should().NotBeNull();
            snapshotStore.GetSnapshot(guid).AggregateId.Should().Be(guid);
        }
Example #7
0
        public void SnapshotStoreSavesSnapshotToPersistentStore()
        {
            var persistentSnapshotStore = new InMemorySnapshotStore();
            var snapshotStore           = new SnapshotStore(persistentSnapshotStore);

            var guid      = Guid.NewGuid();
            var aggregate = new TestSnapshotAggregate(guid, "Test aggregate");

            snapshotStore.SaveSnapshot(aggregate);

            persistentSnapshotStore.Find(guid).FirstOrDefault().Should().NotBeNull();
            persistentSnapshotStore.Find(guid).FirstOrDefault().AggregateId.Should().Be(guid);
        }
Example #8
0
        public void SnapshotStoreShouldReturnTheCorrectAggregateType()
        {
            var persistentSnapshotStore = new InMemorySnapshotStore();
            var snapshotStore           = new SnapshotStore(persistentSnapshotStore);

            var guid      = Guid.NewGuid();
            var aggregate = new TestSnapshotAggregate(guid, "Test aggregate");

            snapshotStore.SaveSnapshot(aggregate);

            var snapshot = snapshotStore.GetSnapshot(guid) as TestAggregateSnapshot;

            snapshot.Should().NotBeNull();
        }
Example #9
0
        public void SnapshotShouldBeSuccessfullyAppliedToAggregate()
        {
            var persistentSnapshotStore = new InMemorySnapshotStore();
            var snapshotStore           = new SnapshotStore(persistentSnapshotStore);

            var          guid      = Guid.NewGuid();
            const string name      = "Test aggregate";
            var          aggregate = new TestSnapshotAggregate(guid, name);

            snapshotStore.SaveSnapshot(aggregate);
            var snapshot = snapshotStore.GetSnapshot(guid) as TestAggregateSnapshot;

            var newAggregate = new TestSnapshotAggregate(Guid.NewGuid(), String.Empty);

            newAggregate.SetSnapshot(snapshot);

            newAggregate.Id.Should().Be(guid);
            newAggregate.ReturnNameForTest().Should().Be(name);
        }