Esempio n. 1
0
        protected async Task <TEntity[]> SynchronizeEntities(TRequest request, IServiceProvider provider, CancellationToken ct)
        {
            var mapper = provider.ProvideInstance <IMapper>();

            await request.RunRequestHooks(RequestConfig, provider, ct).Configure();

            var itemSource = RequestConfig.GetRequestItemSourceFor <TEntity>();
            var items      = ((IEnumerable <object>)itemSource.ItemSource(request)).ToArray();

            items = await request.RunItemHooks <TEntity>(RequestConfig, provider, items, ct).Configure();

            var deletedEntities = await DeleteEntities(request, provider, ct).Configure();

            ct.ThrowIfCancellationRequested();

            var entities = await Context.Set <TEntity>()
                           .SelectWith(request, RequestConfig)
                           .FilterWith(request, RequestConfig, provider)
                           .ToArrayAsync(ct)
                           .Configure();

            var auditEntities = entities
                                .Select(x => (mapper.Map <TEntity, TEntity>(x), x))
                                .ToArray();

            ct.ThrowIfCancellationRequested();

            var joinedItems = RequestConfig
                              .Join(items.Where(x => x != null).ToArray(), entities)
                              .ToArray();

            var createdEntities = await CreateEntities(request,
                                                       provider,
                                                       joinedItems.Where(x => x.Item2 == null).Select(x => x.Item1), ct).Configure();

            ct.ThrowIfCancellationRequested();

            var updatedEntities = await UpdateEntities(
                request,
                provider,
                joinedItems.Where(x => x.Item2 != null), ct).Configure();

            ct.ThrowIfCancellationRequested();

            var mergedEntities = updatedEntities.Concat(createdEntities).ToArray();

            await Context.ApplyChangesAsync(ct).Configure();

            ct.ThrowIfCancellationRequested();

            await request
            .RunAuditHooks(RequestConfig, provider, deletedEntities.Concat(auditEntities), ct)
            .Configure();

            return(mergedEntities);
        }
        private async Task <TEntity[]> UpdateEntities(TRequest request,
                                                      IServiceProvider provider,
                                                      IEnumerable <Tuple <object, TEntity> > items,
                                                      CancellationToken ct)
        {
            var entities = await request.UpdateEntities(RequestConfig, provider, items, ct).Configure();

            await request.RunEntityHooks <TEntity>(RequestConfig, provider, entities, ct).Configure();

            entities = await Context.Set <TEntity>().UpdateAsync(DataContext, entities, ct).Configure();

            ct.ThrowIfCancellationRequested();

            return(entities);
        }
        public async Task <PagedGetAllResult <TOut> > _HandleAsync(TRequest request,
                                                                   IServiceProvider provider,
                                                                   CancellationToken token)
        {
            await request.RunRequestHooks(RequestConfig, provider, token).Configure();

            var entities = Context
                           .Set <TEntity>()
                           .FilterWith(request, RequestConfig, provider)
                           .SortWith(request, RequestConfig, provider);

            var totalItemCount = await entities.CountAsync(token).Configure();

            token.ThrowIfCancellationRequested();

            var pageSize       = request.PageSize < 1 ? totalItemCount : request.PageSize;
            var totalPageCount = totalItemCount == 0 ? 1 : (totalItemCount + pageSize - 1) / pageSize;
            var pageNumber     = Math.Max(1, Math.Min(request.PageNumber, totalPageCount));
            var startIndex     = (pageNumber - 1) * pageSize;

            PagedGetAllResult <TOut> result;

            if (totalItemCount != 0)
            {
                entities = entities.Skip(startIndex).Take(pageSize);
                var items = await GetItems(request, entities, provider, token).Configure();

                result = new PagedGetAllResult <TOut>(items, pageNumber, pageSize, totalPageCount, totalItemCount);
            }
            else
            {
                result = new PagedGetAllResult <TOut>(Array.Empty <TOut>(), 1, pageSize, 1, 0);
            }

            return(await request.RunResultHooks(RequestConfig, provider, result, token).Configure());
        }
Esempio n. 4
0
        private async Task <(TEntity, TEntity)[]> DeleteEntities(TRequest request, IServiceProvider provider, CancellationToken ct)
        {
            var mapper = provider.ProvideInstance <IMapper>();

            var whereClause    = RequestConfig.GetSelectorFor <TEntity>().Get <TEntity>()(request);
            var notWhereClause = whereClause.Update(
                Expression.NotEqual(whereClause.Body, Expression.Constant(true)),
                whereClause.Parameters);

            var deleteEntities = await Context.Set <TEntity>()
                                 .FilterWith(request, RequestConfig, provider)
                                 .Where(notWhereClause)
                                 .ToArrayAsync(ct);

            var pairedEntities = deleteEntities
                                 .Select(x => (mapper.Map <TEntity, TEntity>(x), x))
                                 .ToArray();

            await Context.Set <TEntity>().DeleteAsync(DataContext, deleteEntities, ct);

            ct.ThrowIfCancellationRequested();

            return(pairedEntities);
        }
        private async Task <GetAllResult <TOut> > _HandleAsync(TRequest request, IServiceProvider provider, CancellationToken token)
        {
            var mapper = provider.ProvideInstance <IMapper>();

            await request.RunRequestHooks(RequestConfig, provider, token).Configure();

            var entities = Context.Set <TEntity>()
                           .FilterWith(request, RequestConfig, provider)
                           .SortWith(request, RequestConfig, provider);

            var items = Array.Empty <TOut>();

            if (Options.UseProjection)
            {
                items = await entities.ProjectToArrayAsync <TEntity, TOut>(mapper.ConfigurationProvider, token).Configure();

                token.ThrowIfCancellationRequested();

                if (items.Length == 0)
                {
                    if (RequestConfig.ErrorConfig.FailedToFindInGetAllIsError)
                    {
                        throw new FailedToFindException {
                                  EntityTypeProperty = typeof(TEntity)
                        }
                    }
                    ;

                    var defaultEntity = RequestConfig.GetDefaultFor <TEntity>();
                    if (defaultEntity != null)
                    {
                        items = new TOut[]
                        {
                            await defaultEntity.CreateResult <TRequest, TEntity, TOut>(request, RequestConfig, provider, token).Configure()
                        };
                    }
                }
            }
            else
            {
                var resultEntities = await entities.ToArrayAsync(token).Configure();

                token.ThrowIfCancellationRequested();

                if (resultEntities.Length == 0)
                {
                    if (RequestConfig.ErrorConfig.FailedToFindInGetAllIsError)
                    {
                        throw new FailedToFindException {
                                  EntityTypeProperty = typeof(TEntity)
                        }
                    }
                    ;

                    var defaultEntity = RequestConfig.GetDefaultFor <TEntity>();
                    if (defaultEntity != null)
                    {
                        resultEntities = new TEntity[] { RequestConfig.GetDefaultFor <TEntity>() }
                    }
                    ;
                }

                await request.RunEntityHooks <TEntity>(RequestConfig, provider, entities, token).Configure();

                items = await resultEntities.CreateResults <TRequest, TEntity, TOut>(request, RequestConfig, provider, token).Configure();
            }

            token.ThrowIfCancellationRequested();

            var result = new GetAllResult <TOut>(items);

            return(await request.RunResultHooks(RequestConfig, provider, result, token).Configure());
        }
    }
}
Esempio n. 6
0
 public InMemoryContext(ServiceProviderContainer container)
 {
     _provider = container.GetProvider();
 }