Exemplo n.º 1
0
        private async Task SaveMemento(
            Guid sourceId,
            IMemento memento,
            CancellationToken cancellationToken)
        {
            using (IMementoStoreDbContext context = _dbContextFactory.Invoke())
            {
                Memento entity = await context
                                 .Mementoes
                                 .Where(m => m.AggregateId == sourceId)
                                 .SingleOrDefaultAsync(cancellationToken)
                                 .ConfigureAwait(false);

                if (entity == null)
                {
                    entity = new Memento {
                        AggregateId = sourceId
                    };
                    context.Mementoes.Add(entity);
                }

                entity.MementoJson = _serializer.Serialize(memento);

                await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
            }
        }
Exemplo n.º 2
0
        private async Task <IMemento> FindMemento(
            Guid sourceId,
            CancellationToken cancellationToken)
        {
            using (IMementoStoreDbContext context = _dbContextFactory.Invoke())
            {
                Memento entity = await context
                                 .Mementoes
                                 .Where(m => m.AggregateId == sourceId)
                                 .SingleOrDefaultAsync(cancellationToken)
                                 .ConfigureAwait(false);

                if (entity == null)
                {
                    return(null);
                }

                object memento = _serializer.Deserialize(entity.MementoJson);

                return((IMemento)memento);
            }
        }
Exemplo n.º 3
0
        private async Task DeleteMemento(
            Guid sourceId,
            CancellationToken cancellationToken)
        {
            using (IMementoStoreDbContext context = _dbContextFactory.Invoke())
            {
                Memento entity = await context
                                 .Mementoes
                                 .Where(m => m.AggregateId == sourceId)
                                 .SingleOrDefaultAsync()
                                 .ConfigureAwait(false);

                if (entity == null)
                {
                    return;
                }

                context.Mementoes.Remove(entity);

                await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
            }
        }