Esempio n. 1
0
        /// <summary>
        /// Process a command through a command handler and aggregate root.  Additional processing through event handlers and aggregates enrolled by process managers may
        /// occur depending on how the domain engine is configured.
        /// </summary>
        /// <param name="command">The command to process.</param>
        /// <returns>Any events gernerated as a result of processing the command.</returns>
        public IEnumerable <IEvent> Process(ICommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            using (var transactionScope = DomainTransaction.DefaultTransactionScope())
            {
                try
                {
                    var resultingEvents = this.ProcessCommand(command);

                    this.DispatchEvents(resultingEvents);

                    this.EnqueueEvents(resultingEvents);

                    if (!this.domainOptions.Contains(DomainOption.CacheRuntimeModel))
                    {
                        this.runtimeModel.Clear();
                    }

                    transactionScope.Complete();

                    return(resultingEvents);
                }
                catch (Exception)
                {
                    this.runtimeModel.Clear();
                    throw;
                }
            }
        }
Esempio n. 2
0
        public IEnumerable <IEvent> BrokerEvent <TEvent>(IEvent <TEvent> domainEvent)
        {
            if (domainEvent == null)
            {
                throw new ArgumentNullException(nameof(domainEvent));
            }

            var results = new List <IEvent>();

            using (var transactionScope = DomainTransaction.DefaultTransactionScope())
            {
                var customEventHandlers = this.boundedContextModel.GetEventHandlerTypes(domainEvent.EventBody.GetType());

                var commands = new List <ICommand>();

                foreach (var eventHandlerType in customEventHandlers)
                {
                    var eventHandlerId = this.boundedContextModel.EventHandlerModel(eventHandlerType).GetEventHandlerId(domainEvent);

                    commands.AddRange(this.eventHandler.Handle(domainEvent, eventHandlerId, eventHandlerType));
                }

                foreach (var command in commands)
                {
                    results.AddRange(this.BrokerCommand(command.AsFullyTypedCommand(command.CommandBody)));
                }

                transactionScope.Complete();
            }

            return(results);
        }
        public void DispatchEvents(IEnumerable <IEvent> domainEvents)
        {
            if (domainEvents == null)
            {
                throw new ArgumentNullException(nameof(domainEvents));
            }

            using (var transactionScope = DomainTransaction.DefaultTransactionScope())
            {
                domainEvents.ToList().ForEach(e => this.DispatchFullyTypedEvent(e.AsFullyTypedEvent(e.EventBody)));

                transactionScope.Complete();
            }
        }
        public void DispatchEvent(IEvent domainEvent)
        {
            if (domainEvent == null)
            {
                throw new ArgumentNullException(nameof(domainEvent));
            }

            using (var transactionScope = DomainTransaction.DefaultTransactionScope())
            {
                this.DispatchFullyTypedEvent(domainEvent.AsFullyTypedEvent(domainEvent.EventBody));

                transactionScope.Complete();
            }
        }
Esempio n. 5
0
        public void Store(string streamId, IEnumerable <IEvent> events)
        {
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }

            if (string.IsNullOrWhiteSpace(streamId))
            {
                throw new ArgumentNullException(nameof(streamId));
            }

            if (events.Count() == 0)
            {
                return;
            }

            events = events.OrderBy(e => e.AggregateVersion);


            using (var transactionScope = DomainTransaction.DefaultTransactionScope())
            {
                using (var eventStore = CreateEventContext())
                {
                    var q = (from e in eventStore.Events
                             where e.StreamId == streamId
                             orderby e.Version descending
                             select new { ExpectedNextVersion = e.Version + 1, ExpectedAggregateType = e.StreamType });


                    var expectedValues = q.FirstOrDefault() ?? new { ExpectedNextVersion = 1, ExpectedAggregateType = events.First().AggregateType };

                    SqlEventStore.GuardStreamConcurency(events.First(), expectedValues.ExpectedNextVersion);

                    SqlEventStore.GuardEventArgregateTypes(events, expectedValues.ExpectedAggregateType);

                    SqlEventStore.GuardEventVersions(events, expectedValues.ExpectedNextVersion);

                    foreach (var item in events)
                    {
                        eventStore.Events.Add(ConvertEvent(streamId, item));
                    }

                    eventStore.SaveChanges();
                }

                transactionScope.Complete();
            }
        }
        private IEnumerable <ICommand> ProcessEvent <TEvent>(IEvent <TEvent> domainEvent, string eventHandlerId, Type eventHandlerType)
        {
            IEnumerable <ICommand> resultingCommands;

            using (var transactionScope = DomainTransaction.DefaultTransactionScope())
            {
                resultingCommands = this.eventHandler.Handle(domainEvent, eventHandlerId, eventHandlerType);

                if (!this.cacheRuntimeModel)
                {
                    this.runtimeModel.Clear();
                }

                transactionScope.Complete();
            }

            return(resultingCommands);
        }
Esempio n. 7
0
        public void Store <TAggregate>(string aggregateId, int aggregateVersion, TAggregate aggregate)
        {
            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            using (var transactionScope = DomainTransaction.DefaultTransactionScope())
            {
                using (var context = CreateEventContext())
                {
                    var existingSnapshot = context.Snapshots.Where(s => s.SnapshotId == aggregateId).FirstOrDefault();

                    if (existingSnapshot == null)
                    {
                        context.Snapshots.Add(new Snapshot()
                        {
                            SnapshotId   = aggregateId,
                            CreatedBy    = $"{Environment.UserDomainName}\\{Environment.UserName}",
                            CreatedUtc   = DateTime.UtcNow,
                            SnapshotType = aggregate.GetType().FullName,
                            SnapshotData = snapshotSerialization.SerializeSnapshot(aggregate),
                            Version      = aggregateVersion
                        });
                    }
                    else
                    {
                        existingSnapshot.CreatedBy    = $"{Environment.UserDomainName}\\{Environment.UserName}";
                        existingSnapshot.CreatedUtc   = DateTime.UtcNow;
                        existingSnapshot.SnapshotType = aggregate.GetType().FullName;
                        existingSnapshot.SnapshotData = snapshotSerialization.SerializeSnapshot(aggregate);
                        existingSnapshot.Version      = aggregateVersion;
                    }

                    context.SaveChanges();
                }

                transactionScope.Complete();
            }
        }
Esempio n. 8
0
        public IEnumerable <IEvent> BrokerCommand <TCommand>(ICommand <TCommand> command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var results = new List <IEvent>();

            using (var transactionScope = DomainTransaction.DefaultTransactionScope())
            {
                var events = this.commandHandler.HandleCommand(command, command.AggregateId, this.boundedContextModel.GetAggregateType(command.CommandBody.GetType()));

                foreach (var @event in events)
                {
                    results.Add(@event);
                    results.AddRange(this.BrokerEvent(@event.AsFullyTypedEvent(@event.EventBody)));
                }

                transactionScope.Complete();
            }

            return(results);
        }