예제 #1
0
        public async Task Save(TAggregateRoot aggregateRoot)
        {
            // Check for concurrency issues
            await ConcurrencyCheck(aggregateRoot);

            // Persist Events
            foreach (var uncommittedEvent in aggregateRoot.UncommittedEvents)
            {
                TEventSet @event = (TEventSet)Activator.CreateInstance(typeof(TEventSet), uncommittedEvent);
                await Client.CreateDocumentAsync(_databaseCollectionUri, @event);
            }

            // Check if snapshot is required
            bool snapshotRequired = CanProcessSnapshots &&
                                    aggregateRoot.UncommittedEvents.Any(
                eventWrapper => eventWrapper.Sequence % SnapshotRepository.SnapshotInterval == 0
                );

            if (snapshotRequired)
            {
                await SnapshotRepository.SaveSnapshot(aggregateRoot);
            }

            // Publish events if possible
            if (CanPublishEvents)
            {
                foreach (var uncommittedEvent in aggregateRoot.UncommittedEvents)
                {
                    await DomainEventBus.Publish(uncommittedEvent.Event);
                }
            }

            // Mark aggregate root as committed
            aggregateRoot.MarkAsCommitted();
        }
예제 #2
0
 /// <summary>
 /// Remove
 /// </summary>
 public virtual void Remove()
 {
     repository.Remove((T)this);
     DomainEventBus.Publish(new DefaultAggregationRemoveDomainEvent <T>()
     {
         Object = this as T
     });
 }
        public async Task Execute(ProductId id, ProductName name)
        {
            var product = Product.Create(id, name);

            await productRepository.Save(product).ConfigureAwait(false);

            eventBus.Publish(product.GetDomainEvents());
        }
예제 #4
0
        /// <summary>
        /// Remove
        /// </summary>
        public virtual async Task RemoveAsync()
        {
            await repository.RemoveAsync((T)this).ConfigureAwait(false);

            DomainEventBus.Publish(new DefaultAggregationRemoveDomainEvent <T>()
            {
                Object = this as T
            });
        }
예제 #5
0
        /// <summary>
        /// Save
        /// </summary>
        public virtual Result <T> Save()
        {
            var saveData = repository.Save(this as T);

            if (saveData == null)
            {
                return(Result <T> .FailedResult("Data saved failed"));
            }
            DomainEventBus.Publish(new DefaultAggregationSaveDomainEvent <T>()
            {
                Object = saveData
            });
            return(Result <T> .SuccessResult("Data saved successfully", "", saveData));
        }
예제 #6
0
        /// <summary>
        /// Remove
        /// </summary>
        public virtual Result Remove()
        {
            if (IdentityValueIsNone())
            {
                throw new EZNEWException("The object does not have an identity value set and cannot perform a remove");
            }
            T removeData = this as T;

            repository.Remove(removeData);
            DomainEventBus.Publish(new DefaultAggregationRemoveDomainEvent <T>()
            {
                Object = removeData
            });
            return(Result.SuccessResult("Data removed successfully"));
        }
예제 #7
0
        public void WhenHandlerRegisteredForBaseType_ThenHandlesOnRaise()
        {
            var args = new FooArgs {
                Id = 5
            };
            var handler = new Mock <DomainEventHandler <int, BaseArgs> > {
                CallBase = true
            };

            var bus = new DomainEventBus <int>(new[] { handler.Object });

            bus.Publish(new TestAggregate(), args);

            handler.Verify(x => x.Handle(5, It.IsAny <BaseArgs>()));
        }
예제 #8
0
        public void WhenAsyncHandlerRegisteredForSpecificType_ThenCanUseDefaultAsynRunner()
        {
            var args = new FooArgs {
                Id = 5
            };
            var handler = new Mock <DomainEventHandler <int, FooArgs> > {
                CallBase = true
            };

            handler.Setup(x => x.IsAsync).Returns(true);

            var bus = new DomainEventBus <int>(new[] { handler.Object });

            bus.Publish(new TestAggregate(), args);

            handler.Verify(x => x.Handle(5, args), Times.Never());
        }
예제 #9
0
        public void WhenAsyncHandlerRegisteredForSpecificType_ThenInvokesAsyncRunner()
        {
            var args = new FooArgs {
                Id = 5
            };
            var handler = new Mock <DomainEventHandler <int, FooArgs> > {
                CallBase = true
            };

            handler.Setup(x => x.IsAsync).Returns(true);
            var             asyncCalled = false;
            Action <Action> asyncRunner = action => asyncCalled = true;

            var bus = new DomainEventBus <int>(new[] { handler.Object }, asyncRunner);

            bus.Publish(new TestAggregate(), args);

            handler.Verify(x => x.Handle(5, args), Times.Never());
            Assert.True(asyncCalled);
        }
        public virtual void Save(TAggregateRoot aggregateRoot)
        {
            var set = Context.Set <TEventSet>();

            //Check for concurrency issues
            ConcurrencyCheck <TAggregateRoot, TEventSet> .Process(aggregateRoot, set);

            //Persist Events
            foreach (var uncommittedEvent in aggregateRoot.UncommittedEvents)
            {
                var @event = CreateDomainEventRecord(uncommittedEvent);
                PersistedEvents.Add(@event);
            }

            //Check if snapshot is required
            var snapshotRequired = CanProcessSnapshots &&
                                   aggregateRoot.UncommittedEvents.Any(
                e => e.Sequence % SnapshotRepository.SnapshotInterval == 0);

            if (snapshotRequired)
            {
                SnapshotRepository.SaveSnapshot(aggregateRoot);
            }

            Context.SaveChanges();

            //Publish events if possible
            if (CanPublishEvents)
            {
                foreach (var uncommittedEvent in aggregateRoot.UncommittedEvents)
                {
                    DomainEventBus.Publish(uncommittedEvent.Event);
                }
            }

            //Mark aggregate root as committed
            aggregateRoot.MarkAsCommitted();
        }
예제 #11
0
        public void WhenPublishNullAggregate_ThenThrows()
        {
            var bus = new DomainEventBus <int>(Enumerable.Empty <DomainEventHandler>());

            Assert.Throws <ArgumentNullException>(() => bus.Publish((AggregateRoot <int>)null, new FooArgs()));
        }
예제 #12
0
        public void WhenPublishNullEvent_ThenThrows()
        {
            var bus = new DomainEventBus <int>(Enumerable.Empty <DomainEventHandler>());

            Assert.Throws <ArgumentNullException>(() => bus.Publish(new TestAggregate(), default(FooArgs)));
        }