public virtual async Task <TDto> Get([FromODataUri] TKey key, CancellationToken cancellationToken)
        {
            IQueryable <TDto> baseQuery = WhereByKeyBuilders.WhereByKey(await GetAll(cancellationToken), key);

            IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider = null;

            if (DataProviderSpecificMethodsProviders != null)
            {
                dataProviderSpecificMethodsProvider = DataProviderSpecificMethodsProviders.FirstOrDefault(asyncQueryableExecuter => asyncQueryableExecuter.SupportsQueryable <TDto>(baseQuery));
            }

            TDto dtoResult = default(TDto);

            if (dataProviderSpecificMethodsProvider != null)
            {
                dtoResult = await dataProviderSpecificMethodsProvider.FirstOrDefaultAsync(baseQuery, cancellationToken);
            }
            else
            {
                dtoResult = baseQuery.FirstOrDefault();
            }

            if (dtoResult == null)
            {
                throw new ResourceNotFoundException();
            }

            return(dtoResult);
        }
        protected virtual async Task <TDto> GetById(TKey key, CancellationToken cancellationToken)
        {
            IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider = null;

            IQueryable <TDto> getByIdQuery = await GetByIdQuery(key, cancellationToken);

            if (DataProviderSpecificMethodsProviders != null)
            {
                dataProviderSpecificMethodsProvider = DataProviderSpecificMethodsProviders.FirstOrDefault(asyncQueryableExecuter => asyncQueryableExecuter.SupportsQueryable <TDto>(getByIdQuery));
            }

            if (dataProviderSpecificMethodsProvider == null)
            {
                dataProviderSpecificMethodsProvider = DefaultDataProviderSpecificMethodsProvider.Current;
            }

            TDto result = await dataProviderSpecificMethodsProvider.FirstOrDefaultAsync(getByIdQuery, cancellationToken);

            if (result == null)
            {
                throw new ResourceNotFoundException();
            }

            return(result);
        }
        public virtual async Task <object> ToListAsync <T>(IQueryable <T> query, IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider, int?takeCount, int?skipCount, CancellationToken cancellationToken)
            where T : class
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            if (dataProviderSpecificMethodsProvider == null)
            {
                throw new ArgumentNullException(nameof(dataProviderSpecificMethodsProvider));
            }

            if (skipCount.HasValue == true)
            {
                query = dataProviderSpecificMethodsProvider.Skip(query, skipCount.Value);
            }

            if (takeCount.HasValue == true)
            {
                query = dataProviderSpecificMethodsProvider.Take(query, takeCount.Value);
            }

            return(await dataProviderSpecificMethodsProvider.ToListAsync(query, cancellationToken));
        }
        public virtual async Task <long> GetCountAsync <T>(IQueryable <T> query, IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider, CancellationToken cancellationToken)
            where T : class
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            if (dataProviderSpecificMethodsProvider == null)
            {
                throw new ArgumentNullException(nameof(dataProviderSpecificMethodsProvider));
            }

            return(await dataProviderSpecificMethodsProvider.LongCountAsync(query, cancellationToken));
        }
        private async Task <IQueryable <TDto> > GetByIdQuery(TKey key, CancellationToken cancellationToken)
        {
            IQueryable <TDto> baseQuery = await GetAll(cancellationToken);

            IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider = null;

            if (DataProviderSpecificMethodsProviders != null)
            {
                dataProviderSpecificMethodsProvider = DataProviderSpecificMethodsProviders.FirstOrDefault(asyncQueryableExecuter => asyncQueryableExecuter.SupportsQueryable <TDto>(baseQuery));
            }

            if (dataProviderSpecificMethodsProvider == null)
            {
                dataProviderSpecificMethodsProvider = DefaultDataProviderSpecificMethodsProvider.Current;
            }

            baseQuery = dataProviderSpecificMethodsProvider.ApplyWhereByKeys(baseQuery, key);

            return(baseQuery);
        }
Exemple #6
0
        protected virtual async Task <TDto> GetDtoById(TKey key, CancellationToken cancellationToken)
        {
            IQueryable <TDto> getByIdQuery = await GetQueryById(key, cancellationToken);

            IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider = null;

            if (DataProviderSpecificMethodsProviders != null)
            {
                dataProviderSpecificMethodsProvider = DataProviderSpecificMethodsProviders.FirstOrDefault(asyncQueryableExecutor => asyncQueryableExecutor.SupportsQueryable <TDto>(getByIdQuery));
            }

            if (dataProviderSpecificMethodsProvider == null)
            {
                dataProviderSpecificMethodsProvider = DefaultDataProviderSpecificMethodsProvider.Current;
            }

            TDto dto = await dataProviderSpecificMethodsProvider.FirstOrDefaultAsync(getByIdQuery, cancellationToken);

            return(dto);
        }
 public EfCoreRepository(IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider, TDbContext dbContext) : base(dbContext)
 {
     _efDataProviderSpecificMethodsProvider = dataProviderSpecificMethodsProvider;
 }
Exemple #8
0
 public ATARepository(IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider,
                      ATADbContext dbContext, IUserInfoProvider userInfoProvider)
     : base(dataProviderSpecificMethodsProvider, dbContext)
 {
     UserInfoProvider = userInfoProvider;
 }
        public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            if (actionExecutedContext?.Response?.Content is ObjectContent &&
                actionExecutedContext?.Response?.IsSuccessStatusCode == true)
            {
                ObjectContent objContent = ((ObjectContent)(actionExecutedContext.Response.Content));

                if (objContent.Value == null)
                {
                    actionExecutedContext.Response.StatusCode = HttpStatusCode.NoContent;
                    actionExecutedContext.Response.Content    = null;
                }
                else
                {
                    TypeInfo actionReturnType = objContent.Value.GetType().GetTypeInfo();

                    if (typeof(string) != actionReturnType && typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(actionReturnType))
                    {
                        bool isIQueryable = typeof(IQueryable).GetTypeInfo().IsAssignableFrom(actionReturnType);

                        TypeInfo queryElementType = actionReturnType.HasElementType ? actionReturnType.GetElementType().GetTypeInfo() : actionReturnType.GetGenericArguments().First() /* Why not calling Single() ? http://stackoverflow.com/questions/41718323/why-variable-of-type-ienumerablesomething-gettype-getgenericargsuments-c */.GetTypeInfo();
                        IDataProviderSpecificMethodsProvider dataProviderSpecificMethodsProvider = null;

                        if (isIQueryable == true)
                        {
                            IEnumerable <IDataProviderSpecificMethodsProvider> dataProviderSpecificMethodsProviders = actionExecutedContext.Request.GetOwinContext().GetDependencyResolver().ResolveAll <IDataProviderSpecificMethodsProvider>();
                            dataProviderSpecificMethodsProvider = (IDataProviderSpecificMethodsProvider)typeof(ODataEnableQueryAttribute).GetMethod(nameof(FindDataProviderSpecificMethodsProvider)).MakeGenericMethod(queryElementType).Invoke(this, new object[] { objContent.Value, dataProviderSpecificMethodsProviders });
                        }
                        else
                        {
                            objContent.Value = typeof(ODataEnableQueryAttribute).GetMethod(nameof(ToQueryable)).MakeGenericMethod(queryElementType).Invoke(this, new object[] { objContent.Value });
                        }

                        HttpRequestMessageProperties requestODataProps        = actionExecutedContext.Request.ODataProperties();
                        ODataQueryContext            currentOdataQueryContext = new ODataQueryContext(actionExecutedContext.Request.GetModel(), queryElementType, requestODataProps.Path);
                        ODataQueryOptions            currentOdataQueryOptions = new ODataQueryOptions(currentOdataQueryContext, actionExecutedContext.Request);
                        ODataQuerySettings           globalODataQuerySettings = new ODataQuerySettings
                        {
                            EnableConstantParameterization = this.EnableConstantParameterization,
                            EnsureStableOrdering           = this.EnsureStableOrdering,
                            HandleNullPropagation          = this.HandleNullPropagation,
                            PageSize = this.DefaultPageSize
                        };

                        ValidateQuery(actionExecutedContext.Request, currentOdataQueryOptions);

                        int?currentQueryPageSize = currentOdataQueryOptions?.Top?.Value;
                        int?globalQuerypageSize  = globalODataQuerySettings.PageSize;
                        int?takeCount            = null;
                        int?skipCount            = currentOdataQueryOptions?.Skip?.Value;

                        if (currentQueryPageSize.HasValue)
                        {
                            takeCount = currentQueryPageSize.Value;
                        }
                        else if (globalQuerypageSize.HasValue == true)
                        {
                            takeCount = globalQuerypageSize.Value;
                        }
                        else
                        {
                            takeCount = null;
                        }

                        globalODataQuerySettings.PageSize = null; // ApplyTo will enumerates the query for values other than null. We are gonna apply take in ToList & ToListAsync methods.

                        if (currentOdataQueryOptions.Filter != null)
                        {
                            objContent.Value = currentOdataQueryOptions.Filter.ApplyTo(query: (IQueryable)objContent.Value, querySettings: globalODataQuerySettings);
                        }

                        if (currentOdataQueryOptions.Count?.Value == true && takeCount.HasValue == true)
                        {
                            long count = default(long);
                            if (dataProviderSpecificMethodsProvider != null)
                            {
                                count = await(Task <long>) typeof(ODataEnableQueryAttribute).GetMethod(nameof(GetCountAsync)).MakeGenericMethod(queryElementType).Invoke(this, new object[] { objContent.Value, dataProviderSpecificMethodsProvider, cancellationToken });
                            }
                            else
                            {
                                count = (long)typeof(ODataEnableQueryAttribute).GetMethod(nameof(GetCount)).MakeGenericMethod(queryElementType).Invoke(this, new object[] { objContent.Value });
                            }

                            actionExecutedContext.Request.Properties["System.Web.OData.TotalCountFunc"] = new Func <long>(() => count);
                        }

                        objContent.Value = currentOdataQueryOptions.ApplyTo(query: (IQueryable)objContent.Value, querySettings: globalODataQuerySettings, ignoreQueryOptions: AllowedQueryOptions.Filter | AllowedQueryOptions.Skip | AllowedQueryOptions.Top);

                        if (currentOdataQueryOptions.SelectExpand != null)
                        {
                            queryElementType = objContent.Value.GetType().GetTypeInfo().GetGenericArguments().Single().GetTypeInfo();
                        }

                        if (dataProviderSpecificMethodsProvider != null)
                        {
                            objContent.Value = await(Task <object>) typeof(ODataEnableQueryAttribute).GetMethod(nameof(ToListAsync)).MakeGenericMethod(queryElementType).Invoke(this, new object[] { objContent.Value, dataProviderSpecificMethodsProvider, takeCount, skipCount, cancellationToken });
                        }
                        else
                        {
                            objContent.Value = typeof(ODataEnableQueryAttribute).GetMethod(nameof(ToList)).MakeGenericMethod(queryElementType).Invoke(this, new object[] { objContent.Value, takeCount, skipCount });
                        }

                        if (currentOdataQueryOptions.Count?.Value == true && takeCount.HasValue == false)
                        {
                            // We've no paging becuase there is no global config for max top and there is no top specified by the client's request, so the retured result of query's length is equivalent to total count of the query
                            long count = ((IList)objContent.Value).Count;
                            actionExecutedContext.Request.Properties["System.Web.OData.TotalCountFunc"] = new Func <long>(() => count);
                        }
                    }
                }
            }
        }