Example #1
0
        public void CreateAccountEventIsPublishedToBus()
        {
            using (MassTransitDispatcher massTransitDispatcher = new MassTransitDispatcher(bus))
            {
                PollingClient   pollingClient  = new PollingClient(store.Advanced, 100);
                IObserveCommits commitObserver = pollingClient.ObserveFrom(null);

                IEventHandler <SimpleAggregateCreated> denormalizer = A.Fake <IEventHandler <SimpleAggregateCreated> >();
                AutoResetEvent are = new AutoResetEvent(false);
                A.CallTo(() => denormalizer.Handle(A <SimpleAggregateCreated> .Ignored)).Invokes(() => are.Set());

                bus.Subscribe(denormalizer);

                using (PollingHook pollingHook = new PollingHook(commitObserver))
                {
                    using (var subscription = commitObserver.Subscribe(massTransitDispatcher))
                    {
                        commitObserver.PollNow();
                        commitObserver.Start();

                        Guid aggregateID = Guid.NewGuid();

                        SimpleAggregate aggregate = new SimpleAggregate(aggregateID, DateTime.Now);
                        repository.Save(aggregate, Guid.NewGuid(), (o) => { });

                        are.WaitOne(10000).Should().BeTrue("event should be dispatched and recieved within timeout");
                    }
                }
            }
        }
        public void SimpleAggregateSnapshottingViaSnapshotCreator()
        {
            IStoreEvents          memoryStore      = Wireup.Init().UsingInMemoryPersistence().Build();
            IConstructAggregates  aggregateFactory = A.Fake <IConstructAggregates>();
            IEventStoreRepository repository       = new InMemoryDomainRepository(memoryStore, aggregateFactory, new CommonDomain.Core.ConflictDetector());
            Guid            aggregateID            = Guid.NewGuid();
            SimpleAggregate aggregate = new SimpleAggregate(aggregateID, DateTime.Now);

            repository.Save(aggregate, Guid.NewGuid());

            A.CallTo(() => aggregateFactory.Build(typeof(SimpleAggregate), aggregate.Id, null)).ReturnsLazily(() => new SimpleAggregate());

            SnapshotCreator <SimpleAggregate> snapshotCreator = new SnapshotCreator <SimpleAggregate>(repository, 1);
            Snapshot snapshot = snapshotCreator.SaveSnapShot(aggregate.Id);

            A.CallTo(() => aggregateFactory.Build(typeof(SimpleAggregate), aggregate.Id, A <IMemento> .That.IsNull())).MustHaveHappened(Repeated.Exactly.Once);

            ISnapshot retrievedSnapshot = repository.EventStore.Advanced.GetSnapshot(aggregate.Id, int.MaxValue);

            A.CallTo(() => aggregateFactory.Build(typeof(SimpleAggregate), aggregate.Id, A <IMemento> .That.IsNull())).MustHaveHappened(Repeated.Exactly.Once);

            retrievedSnapshot.Should().NotBeNull();
            retrievedSnapshot.StreamRevision.Should().Be(1);
            retrievedSnapshot.ShouldBeEquivalentTo(snapshot);

            SimpleAggregate retrievedAggregate = repository.GetById <SimpleAggregate>(aggregate.Id);

            retrievedAggregate.Should().NotBeNull();
            aggregate.Version.Should().Be(1);
            retrievedAggregate.ShouldBeEquivalentTo(aggregate);
            A.CallTo(aggregateFactory).Where(o => o.Method.Name != "Build").MustNotHaveHappened();
            A.CallTo(() => aggregateFactory.Build(typeof(SimpleAggregate), aggregate.Id, A <IMemento> .That.IsNull())).MustHaveHappened(Repeated.Exactly.Once);

            A.CallTo(() => aggregateFactory.Build(typeof(SimpleAggregate), aggregate.Id, A <IMemento> .That.Not.IsNull())).MustHaveHappened();
        }
        public void SimpleAggregateSnapshottingViaSnapshotCreatorAndConcreteAggregateFactory()
        {
            IStoreEvents          memoryStore      = Wireup.Init().UsingInMemoryPersistence().Build();
            IConstructAggregates  aggregateFactory = new AggregateFactory();
            IEventStoreRepository repository       = new InMemoryDomainRepository(memoryStore, aggregateFactory, new CommonDomain.Core.ConflictDetector());
            Guid            aggregateID            = Guid.NewGuid();
            SimpleAggregate aggregate = new SimpleAggregate(aggregateID, DateTime.Now);

            repository.Save(aggregate, Guid.NewGuid());
            aggregate.Version.Should().Be(1);

            SnapshotCreator <SimpleAggregate> snapshotCreator = new SnapshotCreator <SimpleAggregate>(repository, 1);
            ISnapshot snapshot = snapshotCreator.SaveSnapShot(aggregate.Id);

            ISnapshot retrievedSnapshot = repository.EventStore.Advanced.GetSnapshot(aggregate.Id, int.MaxValue);

            retrievedSnapshot.Should().NotBeNull();
            retrievedSnapshot.StreamRevision.Should().Be(1);
            retrievedSnapshot.ShouldBeEquivalentTo(snapshot);

            SimpleAggregate retrievedAggregate = repository.GetById <SimpleAggregate>(aggregate.Id);

            retrievedAggregate.Should().NotBeNull();
            aggregate.Version.Should().Be(1);
            retrievedAggregate.ShouldBeEquivalentTo(aggregate);
        }
Example #4
0
        public void ShouldCreateANewEmptyAggregate()
        {
            AggregateFactory aggregateFactory = new AggregateFactory();
            SimpleAggregate  aggregate        = new SimpleAggregate();
            IAggregate       rebuiltAggregate = aggregateFactory.Build(aggregate.GetType(), aggregate.Id, null);

            rebuiltAggregate.ShouldBeEquivalentTo(aggregate);
        }
Example #5
0
        public void ShouldRehydrateTheAggregateFromSnapshot()
        {
            AggregateFactory aggregateFactory = new AggregateFactory();
            SimpleAggregate  aggregate        = new SimpleAggregate(Guid.NewGuid(), DateTime.Now);

            IAggregate rebuiltAggregate = aggregateFactory.Build(aggregate.GetType(), aggregate.Id, ((IMementoCreator)aggregate).CreateMemento());

            rebuiltAggregate.ShouldBeEquivalentTo(aggregate);
        }
        public void SimpleAggregateHasSnapshotBeforeSnapshotting()
        {
            IStoreEvents          memoryStore      = Wireup.Init().UsingInMemoryPersistence().Build();
            IConstructAggregates  aggregateFactory = new AggregateFactory();
            IEventStoreRepository repository       = new InMemoryDomainRepository(memoryStore, aggregateFactory, new CommonDomain.Core.ConflictDetector());
            Guid            aggregateID            = Guid.NewGuid();
            SimpleAggregate aggregate = new SimpleAggregate(aggregateID, DateTime.Now);

            repository.Save(aggregate, Guid.NewGuid());
            aggregate.Version.Should().Be(1);

            ISnapshot retrievedSnapshot = repository.EventStore.Advanced.GetSnapshot(aggregate.Id, int.MaxValue);

            retrievedSnapshot.Should().BeNull();
        }
        public void SimpleAggregateRetrieval()
        {
            IStoreEvents          memoryStore      = Wireup.Init().UsingInMemoryPersistence().Build();
            IConstructAggregates  aggregateFactory = A.Fake <IConstructAggregates>();
            IEventStoreRepository repository       = new InMemoryDomainRepository(memoryStore, aggregateFactory, new CommonDomain.Core.ConflictDetector());
            Guid            aggregateID            = Guid.NewGuid();
            SimpleAggregate aggregate = new SimpleAggregate(aggregateID, DateTime.Now);

            repository.Save(aggregate, Guid.NewGuid());

            A.CallTo(() => aggregateFactory.Build(typeof(SimpleAggregate), aggregate.Id, null)).Returns(new SimpleAggregate(aggregateID, DateTime.Now));

            repository.GetById <SimpleAggregate>(aggregate.Id);

            A.CallTo(aggregateFactory).MustHaveHappened();
            A.CallTo(aggregateFactory).Where(o => o.Method.Name != "Build").MustNotHaveHappened();
            A.CallTo(() => aggregateFactory.Build(typeof(SimpleAggregate), aggregate.Id, A <IMemento> .That.Not.IsNull())).MustNotHaveHappened();
        }
        public void SimpleAggregateDirectSnapshotting()
        {
            IStoreEvents          memoryStore      = Wireup.Init().UsingInMemoryPersistence().Build();
            IConstructAggregates  aggregateFactory = A.Fake <IConstructAggregates>();
            IEventStoreRepository repository       = new InMemoryDomainRepository(memoryStore, aggregateFactory, new CommonDomain.Core.ConflictDetector());
            Guid            aggregateID            = Guid.NewGuid();
            SimpleAggregate aggregate = new SimpleAggregate(aggregateID, DateTime.Now);

            repository.Save(aggregate, Guid.NewGuid());
            IMemento  memento  = aggregate.CreateMemento();
            ISnapshot snapshot = new Snapshot(aggregate.Id.ToString(), aggregate.Version, memento);

            repository.EventStore.Advanced.AddSnapshot(snapshot);

            ISnapshot retrievedSnapshot = repository.EventStore.Advanced.GetSnapshot(aggregate.Id, int.MaxValue);

            retrievedSnapshot.Should().NotBeNull();
            retrievedSnapshot.StreamRevision.Should().Be(1);
            retrievedSnapshot.ShouldBeEquivalentTo(snapshot);

            repository.GetById <SimpleAggregate>(aggregate.Id);

            A.CallTo(() => aggregateFactory.Build(typeof(SimpleAggregate), aggregate.Id, A <IMemento> .That.Not.IsNull())).MustHaveHappened();
        }