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>();

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

            sut = new TestCompositeUnitOfWorkDomainRepository(eventHandlerResolver, unitOfWorkEventHandlerResolver, unitOfWork);
        }
Beispiel #2
0
        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 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);
        }
Beispiel #4
0
        /// <summary>
        /// Publishes the given domain event to all registered event handlers for the event type.
        /// </summary>
        /// <typeparam name="TDomainEvent">The type of the domain event.</typeparam>
        /// <param name="domainEvent">The domain event instance to execute.</param>
        protected override void Publish <TDomainEvent>(TDomainEvent domainEvent)
        {
            IEnumerable <IUnitOfWorkDomainEventHandler <TDomainEvent, TUnitOfWork> > eventHandlers =
                eventHandlerResolver.ResolveEventHandlers <IUnitOfWorkDomainEventHandler <TDomainEvent, TUnitOfWork> >();

            foreach (IUnitOfWorkDomainEventHandler <TDomainEvent, TUnitOfWork> eventHandler in eventHandlers)
            {
                eventHandler.Execute(domainEvent, unitOfWork);
            }

            IEnumerable <IAsyncUnitOfWorkDomainEventHandler <TDomainEvent, TUnitOfWork> > asyncEventHandlers =
                eventHandlerResolver.ResolveEventHandlers <IAsyncUnitOfWorkDomainEventHandler <TDomainEvent, TUnitOfWork> >();

            foreach (IAsyncUnitOfWorkDomainEventHandler <TDomainEvent, TUnitOfWork> eventHandler in asyncEventHandlers)
            {
                eventHandler.ExecuteAsync(domainEvent, unitOfWork).Wait();
            }
        }
        public void InaccessibleTypeParametersCanBeUsed()
        {
            // This test is necessary for checking that the repository can be used with inaccessible type parameters.
            // See the bug in .NET Framework: https://connect.microsoft.com/VisualStudio/feedback/details/672411/bug-in-dynamic-dispatch-when-using-generic-type-parameters

            IUnitOfWorkDomainEventHandler <TestDomainEvent, InternalUnitOfWork> domainEventHandler =
                Substitute.For <IUnitOfWorkDomainEventHandler <TestDomainEvent, InternalUnitOfWork> >();
            IEnumerable <IUnitOfWorkDomainEventHandler <TestDomainEvent, InternalUnitOfWork> > eventHandlers = new List
                                                                                                               <IUnitOfWorkDomainEventHandler <TestDomainEvent, InternalUnitOfWork> >
            {
                domainEventHandler
            };

            unitOfWorkEventHandlerResolver.ResolveEventHandlers <IUnitOfWorkDomainEventHandler <TestDomainEvent, InternalUnitOfWork> >().Returns(eventHandlers);
            unitOfWorkEventHandlerResolver.ResolveEventHandlers <IUnitOfWorkDomainEventHandler <TestDomainEvent, InternalUnitOfWork> >().Returns(eventHandlers);

            InternalAggregateRoot aggregateRoot = new InternalAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent1);

            TestAggregateRoot2 aggregateRoot2 = new TestAggregateRoot2(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent2);

            InternalUnitOfWork internalUnitOfWork = new InternalUnitOfWork();

            var privateSut = new InternalCompositeRepository(eventHandlerResolver, unitOfWorkEventHandlerResolver, internalUnitOfWork);

            privateSut.ApplyChanges(aggregateRoot, aggregateRoot2);

            Received.InOrder(() =>
            {
                domainEventHandler.Execute(domainEvent1, internalUnitOfWork);
                domainEventHandler.Execute(domainEvent2, internalUnitOfWork);
                internalUnitOfWork.SaveChanges();
            });
        }
        public void Setup()
        {
            unitOfWork               = new TestUnitOfWork();
            domainEventHandler1      = Substitute.For <IUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >();
            domainEventHandler2      = Substitute.For <IUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >();
            domainEventHandler3      = Substitute.For <IUnitOfWorkDomainEventHandler <AnotherTestDomainEvent, TestUnitOfWork> >();
            asyncDomainEventHandler1 = Substitute.For <IAsyncUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >();
            asyncDomainEventHandler2 = Substitute.For <IAsyncUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >();
            asyncDomainEventHandler3 = Substitute.For <IAsyncUnitOfWorkDomainEventHandler <AnotherTestDomainEvent, TestUnitOfWork> >();

            resolver = Substitute.For <IUnitOfWorkDomainEventHandlerResolver>();
            resolver.ResolveEventHandlers <IUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >().Returns(new[] { domainEventHandler1, domainEventHandler2 });
            resolver.ResolveEventHandlers <IUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >().Returns(new[] { domainEventHandler1, domainEventHandler2 });
            resolver.ResolveEventHandlers <IUnitOfWorkDomainEventHandler <AnotherTestDomainEvent, TestUnitOfWork> >().Returns(new[] { domainEventHandler3 });
            resolver.ResolveEventHandlers <IAsyncUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >().Returns(new[] { asyncDomainEventHandler1, asyncDomainEventHandler2 });
            resolver.ResolveEventHandlers <IAsyncUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >().Returns(new[] { asyncDomainEventHandler1, asyncDomainEventHandler2 });
            resolver.ResolveEventHandlers <IAsyncUnitOfWorkDomainEventHandler <AnotherTestDomainEvent, TestUnitOfWork> >().Returns(new[] { asyncDomainEventHandler3 });

            sut = new UnitOfWorkDomainEventBroker <TestUnitOfWork>(resolver, unitOfWork);
        }
        public void Setup()
        {
            unitOfWork = new TestUnitOfWork();
            domainEventHandler1 = Substitute.For<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>();
            domainEventHandler2 = Substitute.For<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>();
            domainEventHandler3 = Substitute.For<IUnitOfWorkDomainEventHandler<AnotherTestDomainEvent, TestUnitOfWork>>();
            asyncDomainEventHandler1 = Substitute.For<IAsyncUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>();
            asyncDomainEventHandler2 = Substitute.For<IAsyncUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>();
            asyncDomainEventHandler3 = Substitute.For<IAsyncUnitOfWorkDomainEventHandler<AnotherTestDomainEvent, TestUnitOfWork>>();

            resolver = Substitute.For<IUnitOfWorkDomainEventHandlerResolver>();
            resolver.ResolveEventHandlers<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>().Returns(new[] { domainEventHandler1, domainEventHandler2 });
            resolver.ResolveEventHandlers<IUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>().Returns(new[] { domainEventHandler1, domainEventHandler2 });
            resolver.ResolveEventHandlers<IUnitOfWorkDomainEventHandler<AnotherTestDomainEvent, TestUnitOfWork>>().Returns(new[] { domainEventHandler3 });
            resolver.ResolveEventHandlers<IAsyncUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>().Returns(new[] { asyncDomainEventHandler1, asyncDomainEventHandler2 });
            resolver.ResolveEventHandlers<IAsyncUnitOfWorkDomainEventHandler<TestDomainEvent, TestUnitOfWork>>().Returns(new[] { asyncDomainEventHandler1, asyncDomainEventHandler2 });
            resolver.ResolveEventHandlers<IAsyncUnitOfWorkDomainEventHandler<AnotherTestDomainEvent, TestUnitOfWork>>().Returns(new[] { asyncDomainEventHandler3 });

            sut = new UnitOfWorkDomainEventBroker<TestUnitOfWork>(resolver, unitOfWork);
        }