Пример #1
0
        private async Task PrepareForEventInsertionAsync(IDomainEvent @event)
        {
            var evtType = @event.GetType();
            ISnapshotBehavior behavior = _snapshotBehaviorProvider?.GetBehaviorForEventType(evtType);

            CheckIdAndSetNewIfNeeded(@event);
            ulong?sequence = null;

            if (@event.AggregateId != null)
            {
                await SetSequenceAsync(@event, sequence).ConfigureAwait(false);

                if (_archiveBehavior != SnapshotEventsArchiveBehavior.Disabled &&
                    behavior?.IsSnapshotNeeded(@event) == true)
                {
                    var aggregateState = await GetRehydratedStateAsync(@event.AggregateId, @event.AggregateType).ConfigureAwait(false);

                    var result = behavior.GenerateSnapshot(aggregateState);
                    if (result?.Any() == true)
                    {
                        if (aggregateState != null)
                        {
                            await InsertSnapshotAsync(
                                new Snapshot(@event.AggregateId, @event.AggregateType, aggregateState, behavior.GetType(), DateTime.Now)
                                ).ConfigureAwait(false);
                        }
                        await StoreArchiveEventsAsync(result).ConfigureAwait(false);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Store a domain event in the event store
        /// </summary>
        /// <param name="event">Event instance to be persisted.</param>
        public async Task StoreDomainEventAsync(IDomainEvent @event)
        {
            var eventType = @event.GetType();

            if (eventType.IsDefined(typeof(EventNotPersistedAttribute)))
            {
                return;
            }

            int currentSeq = -1;

            if (@event.AggregateId.HasValue)
            {
                ISnapshotBehavior behavior = _snapshotBehaviorProvider?.GetBehaviorForEventType(eventType);
                if (behavior != null && await behavior.IsSnapshotNeededAsync(@event.AggregateId.Value, @event.AggregateType).ConfigureAwait(false))
                {
                    var result = await behavior.GenerateSnapshotAsync(@event.AggregateId.Value, @event.AggregateType, null).ConfigureAwait(false);

                    if (result.Snapshot != null)
                    {
                        await EventStoreAzureDbContext.Client.CreateDocumentAsync(EventStoreAzureDbContext.SnapshotDatabaseLink, result.Snapshot).ConfigureAwait(false);
                    }
                }
                currentSeq = EventStoreAzureDbContext.Client.CreateDocumentQuery <Event>(EventStoreAzureDbContext.EventsDatabaseLink)
                             .Where(e => e.AggregateId == @event.AggregateId.Value && e.AggregateType == @event.AggregateType.AssemblyQualifiedName).Count();
            }

            await SaveEvent(@event, ++currentSeq).ConfigureAwait(false);
        }