public async Task <CommittedSnapshot> GetSnapshotAsync(
            Type aggregateType,
            IIdentity identity,
            CancellationToken cancellationToken)
        {
            var aggregateName = aggregateType.GetAggregateName().Value;

            using (var dbContext = _contextProvider.CreateContext())
            {
                var snapshot = await dbContext.Set <SnapshotEntity>()
                               .AsNoTracking()
                               .Where(s => s.AggregateName == aggregateName &&
                                      s.AggregateId == identity.Value)
                               .OrderByDescending(s => s.AggregateSequenceNumber)
                               .Select(s => new SnapshotEntity
                {
                    Metadata = s.Metadata,
                    Data     = s.Data
                })
                               .FirstOrDefaultAsync(cancellationToken)
                               .ConfigureAwait(false);

                return(snapshot == null
                    ? null
                    : new CommittedSnapshot(snapshot.Metadata, snapshot.Data));
            }
        }
        public async Task <AllCommittedEventsPage> LoadAllCommittedEvents(GlobalPosition globalPosition, int pageSize,
                                                                          CancellationToken cancellationToken)
        {
            var startPosition = globalPosition.IsStart
                ? 0
                : long.Parse(globalPosition.Value);
            var endPosition = startPosition + pageSize;

            using (var context = _contextProvider.CreateContext())
            {
                var entities = await context
                               .Set <EventEntity>()
                               .Where(e => e.GlobalSequenceNumber >= startPosition &&
                                      e.GlobalSequenceNumber <= endPosition)
                               .OrderBy(e => e.GlobalSequenceNumber)
                               .ToListAsync(cancellationToken)
                               .ConfigureAwait(false);

                var nextPosition = entities.Any()
                    ? entities.Max(e => e.GlobalSequenceNumber) + 1
                    : startPosition;

                return(new AllCommittedEventsPage(new GlobalPosition(nextPosition.ToString()), entities));
            }
        }
Пример #3
0
 public Task <OrderReadModel> ExecuteQueryAsync(GetOrderQuery query, CancellationToken cancellationToken)
 {
     using (var context = _dbContextProvider.CreateContext())
     {
         var data = context.Orders.SingleOrDefault(x => x.OrderNumber.ToString() == query.id);
         return(Task.FromResult(data));
     };
 }
Пример #4
0
        public static async Task <int> Delete <TContext, TEntity, TProjection>(IDbContextProvider <TContext> contextProvider,
                                                                               int batchSize,
                                                                               CancellationToken cancellationToken,
                                                                               Expression <Func <TEntity, TProjection> > projection,
                                                                               Expression <Func <TEntity, bool> > condition = null,
                                                                               Action <TProjection, EntityEntry <TEntity> > setProperties = null)
            where TContext : DbContext
            where TEntity : class, new()
        {
            int rowsAffected = 0;

            while (!cancellationToken.IsCancellationRequested)
            {
                using (var dbContext = contextProvider.CreateContext())
                {
                    IQueryable <TEntity> query = dbContext
                                                 .Set <TEntity>()
                                                 .AsNoTracking();

                    if (condition != null)
                    {
                        query = query.Where(condition);
                    }

                    IEnumerable <TProjection> items = await query
                                                      .Take(batchSize)
                                                      .Select(projection)
                                                      .ToArrayAsync(cancellationToken)
                                                      .ConfigureAwait(false);

                    if (!items.Any())
                    {
                        return(rowsAffected);
                    }

                    if (setProperties == null)
                    {
                        dbContext.RemoveRange((IEnumerable <object>)items);
                    }
                    else
                    {
                        foreach (var item in items)
                        {
                            var entity = new TEntity();
                            var entry  = dbContext.Attach(entity);
                            setProperties.Invoke(item, entry);
                            entry.State = EntityState.Deleted;
                        }
                    }

                    rowsAffected += await dbContext.SaveChangesAsync(cancellationToken)
                                    .ConfigureAwait(false);
                }
            }

            return(rowsAffected);
        }
        public async Task <long?> ExecuteQueryAsync(ThingyGetVersionQuery query, CancellationToken cancellationToken)
        {
            using (var context = _dbContextProvider.CreateContext())
            {
                var entity = await context.Thingys.FindAsync(query.ThingyId.Value);

                return(entity?.Version);
            }
        }
            public async Task <Result> ExecuteQueryAsync(Query query, CancellationToken cancellationToken)
            {
                using (var context = _movieContext.CreateContext())
                {
                    var movie = await GetMovie(query.Id, context);

                    return(movie);
                }
            }
 public EntityFrameworkPersistence(
     ILogger <EntityFrameworkPersistence <TDbContext> > logger,
     IDbContextProvider <TDbContext> contextProvider,
     IUniqueConstraintDetectionStrategy strategy
     )
 {
     _logger   = logger;
     _context  = contextProvider.CreateContext();
     _strategy = strategy;
 }
Пример #8
0
        public async Task <Person> ExecuteQueryAsync(PersonGetQuery query, CancellationToken cancellationToken)
        {
            await using var context = _dbContextProvider.CreateContext();
            var entity = await context.Persons
                         .Include(x => x.Addresses)
                         .SingleOrDefaultAsync(x => x.AggregateId == query.PersonId.Value, cancellationToken)
                         .ConfigureAwait(false);

            return(entity?.ToPerson());
        }
        public async Task <ProcesoReadModel> ExecuteQueryAsync(GetProcesoByFechaCorteQuery query, CancellationToken cancellationToken)
        {
            using (var context = _contextProvider.CreateContext())
            {
                var readModel = await context.Procesos
                                .SingleOrDefaultAsync(x => x.FechaCorte == query.FechaCorte, cancellationToken);

                return(readModel);
            }
        }
        public async Task <IReadOnlyCollection <ThingyMessage> > ExecuteQueryAsync(ThingyGetMessagesQuery query,
                                                                                   CancellationToken cancellationToken)
        {
            using (var context = _dbContextProvider.CreateContext())
            {
                var entities = await context.ThingyMessages
                               .Where(m => m.ThingyId == query.ThingyId.Value)
                               .Select(m => m.ToThingyMessage())
                               .ToArrayAsync(cancellationToken);

                return(entities);
            }
        }
        public async Task <BookingReadModel> ExecuteQueryAsync(BookingIdQuery query, CancellationToken cancellationToken)
        {
            using (var context = _contextProvider.CreateContext())
            {
                var readModel = await context.Bookings
                                .Include(x => x.Journeys)
                                .ThenInclude(x => x.Flight)
                                .Include(x => x.Passengers)
                                .SingleAsync(x => x.Id == query.BookingId, cancellationToken);

                return(readModel);
            }
        }
Пример #12
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Start preparing downstream infrastructure");

            Task.Run(() =>
            {
                _eventStoreContextProvider.CreateContext();
                _readModelContextProvider.CreateContext();

                _startupHostedServiceHealthCheck.StartupTaskCompleted = true;

                _logger.LogInformation($"Downstream infrastructure is ready");
            }, cancellationToken);

            return(Task.CompletedTask);
        }
Пример #13
0
        public async Task <IReadOnlyCollection <TReadModel> > FindAsync(Predicate <TReadModel> predicate, CancellationToken cancellationToken)
        {
            using (var dbContext = _dbContextProvider.CreateContext()) {
                var readModelType = typeof(TReadModel);

                var entity = await dbContext.Set <TReadModel>()
                             .Where(x => predicate(x))
                             .ToListAsync(cancellationToken);

                if (!entity.Any())
                {
                    return(Enumerable.Empty <TReadModel>().ToList());
                }

                return(entity);
            }
        }
        public async Task <List <OrderSummaryReadModel> > ExecuteQueryAsync(GetOrdersFromUserQuery query, CancellationToken cancellationToken)
        {
            using (var context = _dbContextProvider.CreateContext())
            {
                var data =
                    from tOrder in context.Orders
                    where tOrder.BuyerIdentityGuid == ("buyer-" + query.userId.ToString())
                    select new OrderSummaryReadModel
                {
                    OrderNumber = tOrder.OrderNumber,
                    Date        = tOrder.Date,
                    Status      = tOrder.Status,
                    Total       = tOrder.Total
                };

                return(await data.ToListAsync());
            }
        }