private async Task ManageSnapshotBehavior(
            IDomainEvent @event,
            EventStoreDbContext ctx,
            int hashedAggregateId)
        {
            bool IsSnaphostEnabled()
            {
                return(_snapshotBehaviorProvider != null &&
                       _archiveBehavior != SnapshotEventsArchiveBehavior.Disabled);
            }

            if (@event.AggregateId != null && @event.AggregateType != null)
            {
                var evtType = @event.GetType();
                if (IsSnaphostEnabled())
                {
                    var behavior = _snapshotBehaviorProvider !.GetBehaviorForEventType(evtType);
                    if (behavior?.IsSnapshotNeeded(@event) == true)
                    {
                        var aggState = await GetRehydratedAggregateStateAsync(@event.AggregateId, @event.AggregateType, ctx)
                                       .ConfigureAwait(false);

                        var eventsToArchive = behavior.GenerateSnapshot(aggState);

                        if (eventsToArchive?.Any() == true)
                        {
                            if (aggState != null)
                            {
                                var previousSnapshot = await ctx
                                                       .Set <Snapshot>()
                                                       .FirstOrDefaultAsync(s =>
                                                                            s.HashedAggregateId == hashedAggregateId &&
                                                                            s.AggregateType == @event.AggregateType.AssemblyQualifiedName)
                                                       .ConfigureAwait(false);

                                if (previousSnapshot != null)
                                {
                                    ctx.Remove(previousSnapshot);
                                }
                                ctx.Add(new Snapshot
                                {
                                    AggregateType        = @event.AggregateType.AssemblyQualifiedName,
                                    HashedAggregateId    = hashedAggregateId,
                                    SnapshotBehaviorType = behavior.GetType().AssemblyQualifiedName,
                                    SnapshotTime         = DateTime.Now,
                                    SnapshotData         = aggState.ToJson(new AggregateStateSerialisationContract())
                                });
                            }
                            await StoreArchiveEventsAsync(eventsToArchive, ctx).ConfigureAwait(false);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        protected override void Dispose(bool disposing)
        {
            var options = new DbContextOptionsBuilder()
                          .UseSqlServer(_configuration[DataDefaultConnectionString])
                          .Options;

            var context = new EventStoreDbContext(options);

            foreach (var storedEvent in context.StoredEvents.Where(x => x.CorrelationId == _correlationId))
            {
                context.Remove(storedEvent);
            }

            context.SaveChanges();

            base.Dispose(disposing);
        }