public void Setup()
        {
            unitOfWorkDomainEventHandler1 = Substitute.For<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>();
            unitOfWorkDomainEventHandler2 = Substitute.For<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>();
            
            unitOfWorkDomainEventHandlers = new List<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>
            {
                unitOfWorkDomainEventHandler1,
                unitOfWorkDomainEventHandler2
            };

            domainEventHandler3 = Substitute.For<IDomainEventHandler<TestDomainEvent>>();
            domainEventHandler4 = Substitute.For<IDomainEventHandler<TestDomainEvent>>();
            domainEventHandlers = new List<IDomainEventHandler<TestDomainEvent>>
            {
                domainEventHandler3,
                domainEventHandler4
            };

            eventHandlerResolver = Substitute.For<IDomainEventHandlerResolver>();
            unitOfWorkEventHandlerResolver = Substitute.For<IUnitOfWorkDomainEventHandlerResolver>();
            unitOfWork = Substitute.For<TestUnitOfWork>();

            domainEvent = new TestDomainEvent(Guid.NewGuid());
            unitOfWorkEventHandlerResolver.ResolveEventHandlers<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>().Returns(unitOfWorkDomainEventHandlers);
            eventHandlerResolver.ResolveEventHandlers<IDomainEventHandler<TestDomainEvent>>().Returns(domainEventHandlers);

            sut = new TestUnitOfWorkDomainRepository(eventHandlerResolver, unitOfWorkEventHandlerResolver, unitOfWork);
        }
        public void AppliedEventIsReturned()
        {
            var domainEvent = new TestDomainEvent(Guid.NewGuid());
            sut.ApplyEvent(domainEvent);
            IEnumerable<IDomainEvent> appliedEvents = sut.GetAppliedEvents();

            Assert.AreEqual(1, appliedEvents.Count());
            Assert.AreSame(domainEvent, appliedEvents.Single());
        }
        public void AppliedEventsAreReturnedInAppliedOrder()
        {
            var domainEvent1 = new TestDomainEvent(Guid.NewGuid());
            var domainEvent2 = new TestDomainEvent(Guid.NewGuid());
            sut.ApplyEvent(domainEvent2);
            sut.ApplyEvent(domainEvent1);

            IEnumerable<IDomainEvent> appliedEvents = sut.GetAppliedEvents();

            Assert.AreEqual(2, appliedEvents.Count());
            Assert.AreSame(domainEvent2, appliedEvents.ElementAt(0));
            Assert.AreSame(domainEvent1, appliedEvents.ElementAt(1));
        }
        public void Setup()
        {
            domainEventHandler1 = Substitute.For<IAsyncDomainEventHandler<TestDomainEvent>>();
            domainEventHandler2 = Substitute.For<IAsyncDomainEventHandler<TestDomainEvent>>();

            IEnumerable<IAsyncDomainEventHandler<TestDomainEvent>> eventHandlers = new List<IAsyncDomainEventHandler<TestDomainEvent>>
            {
                domainEventHandler1,
                domainEventHandler2
            };

            domainEvent = new TestDomainEvent(Guid.NewGuid());
            eventHandlerResolver = Substitute.For<IDomainEventHandlerResolver>();
            eventHandlerResolver.ResolveEventHandlers<IAsyncDomainEventHandler<TestDomainEvent>>().Returns(eventHandlers);

            sut = new TestAsyncDomainRepository(eventHandlerResolver);
        }
        public void MultipleChangesInUnitOfWorkAreSavedOnce()
        {
            TestAggregateRoot aggregateRoot1 = new TestAggregateRoot(Guid.NewGuid());
            aggregateRoot1.ApplyEvent(domainEvent);

            TestDomainEvent domainEvent2 = new TestDomainEvent(Guid.NewGuid());
            unitOfWorkEventHandlerResolver.ResolveEventHandlers<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>().Returns(unitOfWorkDomainEventHandlers);

            TestAggregateRoot aggregateRoot2 = new TestAggregateRoot(Guid.NewGuid());
            aggregateRoot2.ApplyEvent(domainEvent2);

            sut.ApplyChanges(new List<TestAggregateRoot> { aggregateRoot1, aggregateRoot2 });

            Received.InOrder(() =>
            {
                unitOfWorkDomainEventHandler1.Execute(domainEvent, unitOfWork);
                unitOfWorkDomainEventHandler1.Execute(domainEvent2, unitOfWork);
                unitOfWork.SaveChanges();
            });
        }
        public void VersionIsSetToPreviouslyGeneratedEvents()
        {
            const long expectedVersion = 313;

            var domainEvent1 = new TestDomainEvent(Guid.NewGuid(), 1);
            var domainEvent2 = new TestDomainEvent(Guid.NewGuid(), 2);
            sut.ApplyEvent(domainEvent2);
            sut.ApplyEvent(domainEvent1);

            sut.SetVersion(expectedVersion);

            Assert.AreEqual(expectedVersion, domainEvent1.EntityVersion);
            Assert.AreEqual(expectedVersion, domainEvent2.EntityVersion);
        }
 private DomainEvent ApplyNewEvent(TestAggregateRoot aggregateRoot)
 {
     var domainEvent = new TestDomainEvent(Guid.NewGuid());
     aggregateRoot.ApplyEvent(domainEvent);
     return domainEvent;
 }