/// <summary> /// Apply the query information to a LINQ statement /// </summary> /// <typeparam name="T">Model type</typeparam> /// <param name="instance">contraints instance</param> /// <param name="query">LINQ queryable</param> /// <returns>Modified query</returns> public static IQueryable <T> ApplyTo <T>(this IQueryConstraints <T> instance, IQueryable <T> query) where T : class { if (instance == null) { throw new ArgumentNullException("instance"); } if (query == null) { throw new ArgumentNullException("query"); } if (!string.IsNullOrEmpty(instance.SortPropertyName)) { query = instance.SortOrder == SortOrder.Ascending ? query.OrderBy(instance.SortPropertyName) : query.OrderByDescending(instance.SortPropertyName); } if (instance.PageNumber >= 1) { query = query.Skip((instance.PageNumber - 1) * instance.PageSize).Take(instance.PageSize); } return(query); }
public static T ToSingleOrDefaultSearchResult <T>(this DbQuery <T> dbQuery, IQueryConstraints <T> constraints) where T : class { if (dbQuery == null) { throw new ArgumentNullException(nameof(dbQuery)); } if (constraints == null) { throw new ArgumentNullException(nameof(constraints)); } foreach (string item in constraints.IncludePaths) { dbQuery = dbQuery.Include(item); } IQueryable <T> query = dbQuery.AsNoTracking(); if (constraints.Predicate != null) { return(query.SingleOrDefault(constraints.Predicate)); } return(query.SingleOrDefault()); }
public static DbQuery ApplyIncludedPath <T>(this DbQuery <T> dbQuery, IQueryConstraints <T> instance) where T : class { foreach (string item in instance.IncludePaths) { dbQuery = dbQuery.Include(item); } return(dbQuery); }
public static IQueryConstraints <T> OrElseIf <T>(this IQueryConstraints <T> constraints, Expression <Func <T, bool> > predicate, bool condition) where T : class { if (condition) { constraints.OrElse(predicate); } return(constraints); }
/// <summary> /// Returns custom graph of single BusinessObject of table that satisfies the given <see cref="IQueryConstraints{T}"/> or <see cref="EntityNotFoundException"/> will be thrown. /// </summary> /// <typeparam name="TBusinessEntity">The type of BusinessObject of source table.</typeparam> /// <param name="constraints">QueryConstraints object to be satisfied.</param> /// <returns>BusinessObject the satisfy the <paramref name="constraints"/>.</returns> /// <exception cref="EntityNotFoundException">When no related businessObject is found with the given <paramref name="constraints"/>.</exception> /// <exception cref="RepositoryException">When an error on the database level is occurred and couldn't be recovered.</exception> public TBusinessEntity Single <TBusinessEntity>(IQueryConstraints <TBusinessEntity> constraints) where TBusinessEntity : class, new() { var entity = SingleOrDefault <TBusinessEntity>(constraints); if (entity == null) { throw new EntityNotFoundException(typeof(TBusinessEntity).Name, -1); } return(entity); }
/// <summary> /// Apply the query information to a LINQ statement /// </summary> /// <typeparam name="T">Model type</typeparam> /// <param name="instance">constraints instance</param> /// <param name="dbQuery">LINQ queryable</param> /// <returns>Modified query</returns> public static IQueryable <T> ApplyTo <T>(this IQueryConstraints <T> instance, DbQuery <T> dbQuery) where T : class { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (dbQuery == null) { throw new ArgumentNullException(nameof(dbQuery)); } foreach (string item in instance.IncludePaths) { dbQuery = dbQuery.Include(item); } IQueryable <T> query = dbQuery.AsNoTracking(); if (instance.Predicate != null) { query = query.Where(instance.Predicate); } IOrderedQueryable <T> orderedQuery = null; for (int i = 0; i < instance.SortOrder.Count(); i++) { SortOrderEntry item = instance.SortOrder.ElementAt(i); if (i == 0) { orderedQuery = item.SortOrder == SortOrderEnum.Ascending ? query.OrderBy(item.SortPropertyName) : query.OrderByDescending(item.SortPropertyName); } else { orderedQuery = item.SortOrder == SortOrderEnum.Ascending ? orderedQuery.ThenBy(item.SortPropertyName) : orderedQuery.ThenByDescending(item.SortPropertyName); } } query = orderedQuery ?? query; if (instance.SortOrder.Any() && instance.PageNumber >= 1) { query = query.Skip(instance.StartRecord).Take(instance.PageSize); } return(query); }
public TEntity Single <TEntity>(IQueryConstraints <TEntity> constraints) where TEntity : class, new() { try { using (AppDbContext context = new AppDbContext()) return(context.LoadAggregate(constraints.Predicate)); } catch (Exception ex) { throw ThrowHelper.ReThrow <TEntity>(ex); } }
public IQueryResult<User> FindAll(IQueryConstraints<User> constraints) { var cmd = _connection.CreateCommand(); cmd.CommandText = "SELECT * FROM Users"; // count var count = cmd.Count(); // page cmd.CommandText = ApplyConstraints(constraints, cmd.CommandText); var result = cmd.ExecuteLazyQuery<User>(); return new QueryResult<User>(result, count); }
public int GetCount <TEntity>(IQueryConstraints <TEntity> constraints) where TEntity : class, new() { try { using (AppDbContext context = new AppDbContext()) return(context.Set <TEntity>().Count(constraints.Predicate)); } catch (Exception ex) { throw ThrowHelper.ReThrow <TEntity>(ex); } }
public static void ApplyConstraints(this RestRequest request, IQueryConstraints constraints) { if (constraints == null) { return; } request.AddParameter("p", constraints.PageNumber); request.AddParameter("order", constraints.SortPropertyName); request.AddParameter("sort", constraints.SortOrder == SortOrder.Ascending ? "ASC" : "DESC"); foreach (var item in constraints.FilterParameters) { request.AddParameter(item.Key, item.Value); } }
public IQueryResult <User> FindAll(IQueryConstraints <User> constraints) { var cmd = _connection.CreateCommand(); cmd.CommandText = "SELECT * FROM Users"; // count var count = cmd.Count(); // page cmd.CommandText = ApplyConstraints(constraints, cmd.CommandText); var result = cmd.ExecuteLazyQuery <User>(); return(new QueryResult <User>(result, count)); }
public TEntity SingleOrDefault <TEntity>(IQueryConstraints <TEntity> constraints) where TEntity : class, new() { try { using (AppDbContext context = new AppDbContext()) return(context.Set <TEntity>() .ToSearchResult <TEntity>(constraints) .Items .FirstOrDefault()); } catch (Exception ex) { throw ThrowHelper.ReThrow <TEntity>(ex); } }
public static IQueryConstraints <T> PageAndSort <T>(this IQueryConstraints <T> constraints, SearchCriteria criteria, Expression <Func <T, object> > defaultSortExpression) where T : class { constraints.Page(criteria.PageNumber, criteria.PageSize); if (!string.IsNullOrEmpty(criteria.Sort)) { constraints.SortBy(criteria.Sort, criteria.SortDirection); } else { constraints.SortByDescending(defaultSortExpression); } return(constraints); }
public virtual async Task <PagedResult <Beer> > GetBeers(IQueryConstraints constraints = null) { var request = new RestRequest { Resource = "beers" }; request.AddParameter("hasLabels", "Y"); request.ApplyConstraints(constraints); var response = await Api.Execute <RootObject <Beer> >(request); if (response.Data == null) { return(PagedResult <Beer> .Empty); } return(new PagedResult <Beer>(response.Data, response.CurrentPage, response.NumberOfPages)); }
private static string ApplyConstraints(IQueryConstraints<User> constraints, string sql) { if (!string.IsNullOrEmpty(constraints.SortPropertyName)) { sql += " ORDER BY " + constraints.SortPropertyName; if (constraints.SortOrder == SortOrder.Descending) sql += " DESC"; } if (constraints.PageNumber != -1) { var context = new DbPagerContext(sql, constraints.PageNumber, constraints.PageSize); var pager = new SqlServerCePager(); sql = pager.ApplyTo(context); } return sql; }
/// <summary> /// Execute LINQ query and fill a result. /// </summary> /// <typeparam name="TFrom">Database Model type</typeparam> /// <typeparam name="TTo">Query result item type</typeparam> /// <param name="instanz">The instanz.</param> /// <param name="constraints">The constraints.</param> /// <param name="converter">Method used to convert the result </param> /// <returns>Search reuslt</returns> public static IQueryResult <TTo> ToSearchResult <TFrom, TTo>(this IQueryable <TFrom> instanz, IQueryConstraints <TFrom> constraints, Func <TFrom, TTo> converter) where TFrom : class where TTo : class { if (instanz == null) { throw new ArgumentNullException("instanz"); } if (constraints == null) { throw new ArgumentNullException("constraints"); } var totalCount = instanz.Count(); var limitedQuery = constraints.ApplyTo(instanz); return(new QueryResult <TTo>(limitedQuery.Select(converter), totalCount)); }
private static string ApplyConstraints(IQueryConstraints <User> constraints, string sql) { if (!string.IsNullOrEmpty(constraints.SortPropertyName)) { sql += " ORDER BY " + constraints.SortPropertyName; if (constraints.SortOrder == SortOrder.Descending) { sql += " DESC"; } } if (constraints.PageNumber != -1) { var context = new DbPagerContext(sql, constraints.PageNumber, constraints.PageSize); var pager = new SqlServerCePager(); sql = pager.ApplyTo(context); } return(sql); }
/// <summary> /// Execute LINQ query and fill a result. /// </summary> /// <typeparam name="T">Model type</typeparam> /// <param name="dbQuery">The dbQuery.</param> /// <param name="constraints">The constraints.</param> /// <returns>Search result</returns> public static IQueryResult <T> ToSearchResult <T>(this DbQuery <T> dbQuery, IQueryConstraints <T> constraints) where T : class { if (dbQuery == null) { throw new ArgumentNullException("dbQuery"); } if (constraints == null) { throw new ArgumentNullException("constraints"); } var totalCount = constraints.Predicate == null ? dbQuery.Count() : dbQuery.Count(constraints.Predicate); var limitedQuery = constraints.ApplyTo(dbQuery).ToList(); return(new QueryResult <T>(limitedQuery, totalCount, constraints.PageNumber, constraints.PageSize)); }
/// <summary> /// Execute LINQ query and fill a result. /// </summary> /// <typeparam name="T">Model type</typeparam> /// <param name="dbQuery">The dbQuery.</param> /// <param name="constraints">The constraints.</param> /// <returns>Search result</returns> public static IQueryResult <T> ToSearchResult <T>(this DbQuery <T> dbQuery, IQueryConstraints <T> constraints) where T : class { if (dbQuery == null) { throw new ArgumentNullException(nameof(dbQuery)); } if (constraints == null) { throw new ArgumentNullException(nameof(constraints)); } int totalCount = constraints.Predicate == null ? dbQuery.Count() : dbQuery.Count(constraints.Predicate); List <T> limitedQuery = constraints.ApplyTo(dbQuery).ToList(); return(new QueryResult <T>(limitedQuery, totalCount)); }
public IQueryResult <TEntity> Find <TEntity>(IQueryConstraints <TEntity> constraints) where TEntity : class, new() { try { using (AppDbContext context = new AppDbContext()) { IQueryResult <TEntity> results = null; // get the appropriate cache key for this query string key = Cache.CacheKey(constraints); // check if it's in the cache already if (!Cache.Provider.TryGet(key, out results)) { Debug.WriteLine($"NOT FOUND cache for: {key}"); // if not, then run the query results = context.Set <TEntity>().ToSearchResult(constraints); // and cache the results for next time Cache.Provider.Set(key, results); } else { Debug.WriteLine($"Found cache for: {key}"); } // return the results either from cache or that were just run return(results); } } catch (Exception ex) { throw ThrowHelper.ReThrow <TEntity>(ex); } }
internal static string CacheKey <TBusinessEntity>(IQueryConstraints <TBusinessEntity> constraints) where TBusinessEntity : class { Expression predicate = constraints.Predicate; string pagePart = $"Page[{constraints.PageNumber}, {constraints.PageSize}]"; string includePart = constraints.IncludePaths.Any() ? $"Include[{constraints.IncludePaths.Aggregate((c, n) => c + ", " + n)}]" : ""; string sortPart = constraints.SortOrder.Any() ? $"Sort[{constraints.SortOrder.Select(x => x.SortPropertyName + " " + x.SortOrder.ToString()).Aggregate((c, n) => c + ", " + n)}]" : ""; StringBuilder key = new StringBuilder(); key.Append(pagePart); if (string.IsNullOrEmpty(includePart) == false) { key.Append(", " + includePart); } if (string.IsNullOrEmpty(sortPart) == false) { key.Append(", " + sortPart); } if (constraints.Predicate != null) { key.Append(", " + $"Where[{GetHash(predicate)}]"); } return(string.Concat(typeof(TBusinessEntity).Name, "/", GetGeneration <TBusinessEntity>(), "/", key)); }
public Task <IEnumerable <T> > Filter <T>(string measurement, Expression <Func <T, bool> > criteria, IQueryConstraints <T> constraints = null) where T : class, IRecord, new() { return(Task.FromResult(Enumerable.Empty <T>())); }
/// <summary> /// Execute LINQ query and fill a result. /// </summary> /// <typeparam name="TFrom">Database Model type</typeparam> /// <typeparam name="TTo">Query result item type</typeparam> /// <param name="dbQuery">The dbQuery.</param> /// <param name="constraints">The constraints.</param> /// <param name="converter">Method used to convert the result </param> /// <returns>Search result</returns> public static IQueryResult <TTo> ToSearchResult <TFrom, TTo>(this DbQuery <TFrom> dbQuery, IQueryConstraints <TFrom> constraints, Func <TFrom, TTo> converter) where TFrom : class where TTo : class { if (dbQuery == null) { throw new ArgumentNullException(nameof(dbQuery)); } if (constraints == null) { throw new ArgumentNullException(nameof(constraints)); } int totalCount = constraints.Predicate == null ? dbQuery.Count() : dbQuery.Count(constraints.Predicate); IQueryable <TFrom> limitedQuery = constraints.ApplyTo(dbQuery); return(new QueryResult <TTo>(limitedQuery.Select(converter), totalCount)); }
public Task <long> Count <T>(string measurement, Expression <Func <T, bool> > criteria, IQueryConstraints <T> constraints = null) where T : class, IRecord, new() { return(Task.FromResult(0L)); }
/// <summary> /// Execute LINQ query and fill a result. /// </summary> /// <typeparam name="T">Model type</typeparam> /// <param name="instanz">The instanz.</param> /// <param name="constraints">The constraints.</param> /// <returns>Search reuslt</returns> public static IQueryResult <T> ToSearchResult <T>(this IQueryable <T> instanz, IQueryConstraints <T> constraints) where T : class { if (instanz == null) { throw new ArgumentNullException("instanz"); } if (constraints == null) { throw new ArgumentNullException("constraints"); } var totalCount = instanz.Count(); var limitedQuery = constraints.ApplyTo(instanz); return(new QueryResult <T>(limitedQuery, totalCount)); }
public Task <IEnumerable <TAggregate> > Group <T, TAggregate>(string measurement, Expression <Func <T, bool> > criteria, IQueryConstraints <T> constraints) where T : class, IRecord, new() where TAggregate : class, IRecord, new() { return(Task.FromResult(Enumerable.Empty <TAggregate>())); }
public virtual async Task <PagedResult <Beer> > GetByGlasswareId(int glasswareId, IQueryConstraints constraints = null) { var request = new RestRequest { Resource = "beers" }; var response = await Api.Execute <RootObject <Beer> >(request); if (response.Data == null) { return(PagedResult <Beer> .Empty); } return(new PagedResult <Beer>(response.Data, response.CurrentPage, response.TotalResults)); }
/// <summary> /// Execute LINQ query and fill a result. /// </summary> /// <typeparam name="TFrom">Database Model type</typeparam> /// <typeparam name="TTo">Query result item type</typeparam> /// <param name="dbQuery">The dbQuery.</param> /// <param name="constraints">The constraints.</param> /// <param name="converter">Method used to convert the result </param> /// <returns>Search result</returns> public static IQueryResult <TTo> ToSearchResult <TFrom, TTo>(this DbQuery <TFrom> dbQuery, IQueryConstraints <TFrom> constraints, Func <TFrom, TTo> converter) where TFrom : class where TTo : class { if (dbQuery == null) { throw new ArgumentNullException("dbQuery"); } if (constraints == null) { throw new ArgumentNullException("constraints"); } var totalCount = constraints.Predicate == null ? dbQuery.Count() : dbQuery.Count(constraints.Predicate); var limitedQuery = constraints.ApplyTo(dbQuery); return(new QueryResult <TTo>(limitedQuery.Select(converter), totalCount, constraints.PageNumber, constraints.PageSize)); }
public virtual async Task <PagedResult <Beer> > GetByStyleId(int styleId, IQueryConstraints constraints = null) { throw new NotImplementedException(); }