Ejemplo n.º 1
0
        public async Task <IEnumerable <TEntity> > FindAllAsync(
            IEnumerable <ISpecification <TEntity> > specifications,
            IFindOptions <TEntity> options      = null,
            CancellationToken cancellationToken = default)
        {
            var specificationsArray = specifications as ISpecification <TEntity>[] ?? specifications.ToArray();
            var expressions         = specificationsArray.Safe().Select(s => s.ToExpression());

            if (options?.HasOrders() == true)
            {
                return(await this.Options.DbContext.Set <TEntity>() // .AsAsyncEnumerable()
                       .TrackChangesIf(options.TrackChanges)
                       .AsExpandable()
                       .WhereExpressions(expressions)
                       .IncludeIf(options)
                       .SkipIf(options?.Skip)
                       .TakeIf(options?.Take)
                       .OrderByIf(options).ToListAsyncSafe(cancellationToken).AnyContext());
            }
            else
            {
                return(await this.Options.DbContext.Set <TEntity>() // .AsAsyncEnumerable()
                       .TrackChangesIf(options.TrackChanges)
                       .AsExpandable()
                       .WhereExpressions(expressions)
                       .IncludeIf(options)
                       .SkipIf(options?.Skip)
                       .TakeIf(options?.Take).ToListAsyncSafe(cancellationToken).AnyContext());
            }
        }
Ejemplo n.º 2
0
        public static IQueryable <TEntity> IncludeIf <TEntity>(
            this IQueryable <TEntity> source,
            IFindOptions <TEntity> options)
            where TEntity : class, IEntity, IAggregateRoot
        {
            if (options?.Include == null && options?.Includes == null)
            {
                return(source);
            }

            foreach (var include in (options?.Includes ?? new List <IncludeOption <TEntity> >()).Insert(options?.Include))
            {
                if (include.Expression != null)
                {
                    source.Include(include.Expression);
                }

                if (!include.Path.IsNullOrEmpty())
                {
                    source.Include(include.Path);
                }
            }

            return(source);
        }
Ejemplo n.º 3
0
 public async Task <IEnumerable <TEntity> > FindAllAsync(
     ISpecification <TEntity> specification,
     IFindOptions <TEntity> options      = null,
     CancellationToken cancellationToken = default)
 {
     return(await this.FindAllAsync(new[] { specification }, options, cancellationToken).AnyContext());
 }
Ejemplo n.º 4
0
        public static IQueryable <TDestination> IncludeIf <TEntity, TDestination>(
            this IQueryable <TDestination> source,
            IFindOptions <TEntity> options,
            IEntityMapper mapper)
            where TEntity : class, IEntity, IAggregateRoot
            where TDestination : class
        {
            EnsureArg.IsNotNull(mapper, nameof(mapper));

            if (options?.Include == null && options?.Includes == null)
            {
                return(source);
            }

            foreach (var include in (options?.Includes ?? new List <IncludeOption <TEntity> >()).Insert(options?.Include))
            {
                if (include.Expression != null)
                {
                    source.Include(
                        mapper.MapExpression <Expression <Func <TDestination, object> > >(include.Expression));
                }

                if (!include.Path.IsNullOrEmpty())
                {
                    source.Include(include.Path);
                }
            }

            return(source);
        }
Ejemplo n.º 5
0
        public static IOrderedQueryable <TDestination> OrderByIf <TEntity, TDestination>(
            this IQueryable <TDestination> source,
            IFindOptions <TEntity> options,
            IEntityMapper mapper)
            where TEntity : class, IEntity, IAggregateRoot
        {
            if (options?.Order == null && options?.Orders.IsNullOrEmpty() == true)
            {
                return(source as IOrderedQueryable <TDestination>); // TODO: this returns null, find a way to return an IOrderedQueryable event if no orders are provided. possible?
            }

            IOrderedQueryable <TDestination> result = null;

            foreach (var order in (options?.Orders ?? new List <OrderOption <TEntity> >()).Insert(options?.Order))
            {
                result = result == null
                        ? order.Direction == OrderDirection.Ascending
                            ? Queryable.OrderBy(source, mapper.MapExpression <Expression <Func <TDestination, object> > >(order.Expression)) // replace wit CompileFast()? https://github.com/dadhi/FastExpressionCompiler
                            : Queryable.OrderByDescending(source, mapper.MapExpression <Expression <Func <TDestination, object> > >(order.Expression))
                        : order.Direction == OrderDirection.Ascending
                            ? result.ThenBy(mapper.MapExpression <Expression <Func <TDestination, object> > >(order.Expression))
                            : result.ThenByDescending(mapper.MapExpression <Expression <Func <TDestination, object> > >(order.Expression));
            }

            return(result);
        }
Ejemplo n.º 6
0
 public async Task <IEnumerable <TEntity> > FindAllAsync(IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
 {
     using (var scope = this.tracer?.BuildSpan($"findall {this.name}", LogKeys.DomainRepository)
                        .WithTag(SpanTagKey.DbType, "sql").Activate(this.logger))
     {
         return(await this.inner.FindAllAsync(options, cancellationToken).AnyContext());
     }
 }
Ejemplo n.º 7
0
        public async Task <IEnumerable <TEntity> > FindAllAsync(IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
        {
            foreach (var order in (options?.Orders ?? new List <OrderOption <TEntity> >()).Insert(options?.Order))
            {
                this.logger.LogDebug($"{LogEventKeys.DomainRepository} order: {order.Expression.ToExpressionString()}");
            }

            return(await this.decoratee.FindAllAsync(options, cancellationToken).AnyContext());
        }
Ejemplo n.º 8
0
        private IFindOptions <TEntity> EnsureOptions(IFindOptions <TEntity> options)
        {
            if (options == null)
            {
                options = new FindOptions <TEntity>();
            }

            options.Order = new OrderOption <TEntity>(this.orderExpression, this.orderDirection);
            return(options);
        }
Ejemplo n.º 9
0
        private IFindOptions <TEntity> EnsureOptions(IFindOptions <TEntity> options)
        {
            if (options == null)
            {
                options = new FindOptions <TEntity>();
            }

            options.Orders = options.Orders.Insert(new OrderOption <TEntity>(this.expression, this.direction));
            return(options);
        }
Ejemplo n.º 10
0
        public async Task <IEnumerable <TEntity> > FindAllAsync(IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
        {
            this.logger.LogInformation($"{{LogKey:l}} findall {typeof(TEntity).PrettyName()}", LogKeys.DomainRepository);

            foreach (var order in (options?.Orders ?? new List <OrderOption <TEntity> >()).Insert(options?.Order))
            {
                this.logger.LogDebug($"{LogKeys.DomainRepository} order: {order.Expression.ToExpressionString()}");
            }

            return(await this.inner.FindAllAsync(options, cancellationToken).AnyContext());
        }
Ejemplo n.º 11
0
        public async Task <IEnumerable <TEntity> > FindAllAsync(IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
        {
            var order    = (options?.Orders ?? new List <OrderOption <TEntity> >()).Insert(options?.Order).FirstOrDefault(); // cosmos only supports single orderby
            var entities = await this.options.Provider
                           .WhereAsync(
                count : options?.Take ?? -1,    // TODO: implement cosmosdb skip/take once available https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/6350987--documentdb-allow-paging-skip-take
                orderExpression : order?.Expression,
                orderDescending : order?.Direction == OrderDirection.Descending).AnyContext();

            return(entities.ToList());
        }
Ejemplo n.º 12
0
        private IFindOptions<TEntity> EnsureOptions(IFindOptions<TEntity> options)
        {
            if (options == null)
            {
                options = new FindOptions<TEntity>();
            }

            options.Includes = options.Includes.InsertRange(
                this.expressions.Select(e => new IncludeOption<TEntity>(e)));

            return options;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Finds all asynchronous.
        /// </summary>
        /// <typeparam name="TEntity">Type of the result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="tenantId">The tenant identifier.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static async Task <IEnumerable <TEntity> > FindAllAsync <TEntity>( // causes issues when used in mapped repos <TEntity, TDesitination>, Unable to cast object of type 'xxxDto' to type 'Naos.Core.Domain.ITenantEntity'. better use the tenant decorator for this
            this IRepository <TEntity> source,
            string tenantId,
            IFindOptions <TEntity> options = null)
            where TEntity : class, IEntity, ITenantEntity, IAggregateRoot
        {
            EnsureArg.IsNotNullOrEmpty(tenantId);

            return(await source.FindAllAsync(
                       HasTenantSpecification <TEntity> .Factory.Create(tenantId),
                       options).AnyContext());
        }
Ejemplo n.º 14
0
        private Expression <Func <T, bool> > CreateIfModifiedFilterExperssion <T>(IFindOptions <T> options)
        {
            if (options == null || !options.IfModifiedSince.HasValue)
            {
                return(null);
            }
            var entity = Expression.Parameter(typeof(T));
            var body   = Expression.GreaterThan(
                Expression.Property(entity, MetaFields.LastUpdated),
                Expression.Constant(options.IfModifiedSince.GetValueOrDefault()));

            return(Expression.Lambda <Func <T, bool> >(body, entity));
        }
Ejemplo n.º 15
0
        public static void IfModifiedSince <T>(this IRestBox box, IFindOptions <T> options)
        {
            if (options == null)
            {
                return;
            }
            if (!options.IfModifiedSince.HasValue)
            {
                return;
            }
            EnsureHeaders(box);

            box.Headers.Add("If-Modified-Since", options.IfModifiedSince.Value.ToString("r"));
        }
 public async Task <IEnumerable <TEntity> > FindAllAsync(IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
 {
     if (options?.HasOrders() == true)
     {
         return(await this.options.DbContext.Set <TEntity>()
                .TakeIf(options?.Take)
                .OrderByIf(options).ToListAsyncSafe(cancellationToken).AnyContext());
     }
     else
     {
         return(await this.options.DbContext.Set <TEntity>()
                .TakeIf(options?.Take).ToListAsyncSafe(cancellationToken).AnyContext());
     }
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Finds all asynchronous.
        /// </summary>
        /// <param name="specifications">The specifications.</param>
        /// <param name="options">The options.</param>
        /// /// <param name="cancellationToken">The cancellationToken.</param>
        /// <returns></returns>
        public virtual async Task <IEnumerable <TEntity> > FindAllAsync(
            IEnumerable <ISpecification <TEntity> > specifications,
            IFindOptions <TEntity> options      = null,
            CancellationToken cancellationToken = default)
        {
            var result = this.context.Entities.AsEnumerable();

            foreach (var specification in specifications.Safe())
            {
                result = result.Where(this.EnsurePredicate(specification));
            }

            return(await Task.FromResult(this.FindAll(result, options, cancellationToken).ToList()).AnyContext());
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Finds all asynchronous.
        /// </summary>
        /// <param name="specifications">The specifications.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellationToken.</param>
        /// <returns></returns>
        public override async Task <IEnumerable <TEntity> > FindAllAsync(
            IEnumerable <ISpecification <TEntity> > specifications,
            IFindOptions <TEntity> options      = null,
            CancellationToken cancellationToken = default)
        {
            var result = this.context.Entities.Safe().Select(e => this.Options.Mapper.Map <TDestination>(e)); // work on destination objects

            foreach (var specification in specifications.Safe())
            {
                result = result.Where(this.EnsurePredicate(specification)); // translate specification to destination predicate
            }

            return(await Task.FromResult(this.FindAll(result, options)));
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Finds all asynchronous.
        /// </summary>
        /// <typeparam name="TEntity">Type of the result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="tenantId">The tenant identifier.</param>
        /// <param name="specification">The specification.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static async Task <IEnumerable <TEntity> > FindAllAsync <TEntity>(
            this IRepository <TEntity> source,
            string tenantId,
            Specification <TEntity> specification,
            IFindOptions <TEntity> options = null)
            where TEntity : class, IEntity, ITenantEntity, IAggregateRoot
        {
            EnsureArg.IsNotNullOrEmpty(tenantId);
            EnsureArg.IsNotNull(specification);

            return(await source.FindAllAsync(
                       new List <ISpecification <TEntity> >
            {
                specification,
                HasTenantSpecification <TEntity> .Factory.Create(tenantId)
            },
                       options).AnyContext());
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Finds all asynchronous.
        /// </summary>
        /// <typeparam name="TEntity">Type of the result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="tenantId">The tenant identifier.</param>
        /// <param name="specifications">The specifications.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static async Task <IEnumerable <TEntity> > FindAllAsync <TEntity>(
            this IRepository <TEntity> source,
            string tenantId,
            IEnumerable <Specification <TEntity> > specifications,
            IFindOptions <TEntity> options = null)
            where TEntity : class, IEntity, ITenantEntity, IAggregateRoot
        {
            EnsureArg.IsNotNullOrEmpty(tenantId);
            var specificationsArray = specifications as Specification <TEntity>[] ?? specifications.ToArray();

            EnsureArg.IsNotNull(specificationsArray);
            EnsureArg.IsTrue(specificationsArray.Any());

            return(await source.FindAllAsync(
                       new List <Specification <TEntity> >(specificationsArray)
            {
                HasTenantSpecification <TEntity> .Factory.Create(tenantId)
            },
                       options).AnyContext());
        }
Ejemplo n.º 21
0
        //public static IQueryable<T> OrderByIf<T>(
        //    this IQueryable<T> source,
        //    IFindOptions<T> options)
        //{
        //    if (options == null)
        //    {
        //        return source;
        //    }

        //    IOrderedEnumerable<T> orderedResult = null;
        //    foreach (var order in (options?.Orders ?? new List<OrderOption<T>>()).Insert(options?.Order))
        //    {
        //        orderedResult = orderedResult == null
        //                ? order.Direction == OrderDirection.Ascending
        //                    ? source.OrderBy(order.Expression.Compile()) // replace wit CompileFast()? https://github.com/dadhi/FastExpressionCompiler
        //                    : source.OrderByDescending(order.Expression.Compile())
        //                : order.Direction == OrderDirection.Ascending
        //                    ? orderedResult.ThenBy(order.Expression.Compile())
        //                    : orderedResult.ThenByDescending(order.Expression.Compile());
        //    }

        //    return orderedResult != null ? orderedResult.AsQueryable() : source;
        //}

        public static IOrderedQueryable <T> OrderByIf <T>(
            this IQueryable <T> source,
            IFindOptions <T> options)
        {
            if (options?.Order == null && options?.Orders.IsNullOrEmpty() == true)
            {
                return(source as IOrderedQueryable <T>); // TODO: this returns null, find a way to return an IOrderedQueryable event if no orders are provided. possible?
            }

            IOrderedQueryable <T> result = null;

            foreach (var order in (options?.Orders ?? new List <OrderOption <T> >()).Insert(options?.Order))
            {
                result = result == null
                        ? order.Direction == OrderDirection.Ascending
                            ? Queryable.OrderBy(source, order.Expression) // replace wit CompileFast()? https://github.com/dadhi/FastExpressionCompiler
                            : Queryable.OrderByDescending(source, order.Expression)
                        : order.Direction == OrderDirection.Ascending
                            ? result.ThenBy(order.Expression)
                            : result.ThenByDescending(order.Expression);
            }

            return(result);
        }
Ejemplo n.º 22
0
        public static IFindFluent <TEntity, TProjection> Sort <TEntity, TProjection>(
            this IFindFluent <TEntity, TProjection> source,
            IFindOptions <TEntity> options)
            where TEntity : class, IEntity, IAggregateRoot
        {
            if (options?.Order == null && options?.Orders.IsNullOrEmpty() == true)
            {
                return(source);
            }

            foreach (var order in (options?.Orders ?? new List <OrderOption <TEntity> >()).Insert(options?.Order))
            {
                if (order.Direction == OrderDirection.Ascending)
                {
                    source.SortBy(order.Expression);
                }
                else
                {
                    source.SortByDescending(order.Expression);
                }
            }

            return(source);
        }
Ejemplo n.º 23
0
 public virtual Task <List <T> > Find <T>(Expression <Func <T, bool> > filter, IFindOptions <T> options = null)
 {
     throw new NotImplementedException();
 }
        public async Task <IEnumerable <TEntity> > FindAllAsync(IEnumerable <ISpecification <TEntity> > specifications, IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
        {
            var specificationsArray = specifications as ISpecification <TEntity>[] ?? specifications.ToArray();
            var expressions         = specificationsArray.Safe()
                                      .Select(s => this.Options.Mapper.MapSpecification <TEntity, TDestination>(s).Expand()); // expand fixes Invoke in expression issue
            var order = (options?.Orders ?? new List <OrderOption <TEntity> >()).Insert(options?.Order).FirstOrDefault();     // cosmos only supports single orderby

            var result = await this.Provider
                         .WhereAsync(
                expressions : expressions,
                skip : options?.Skip ?? -1,
                take : options?.Take ?? -1,
                orderExpression : this.Options.Mapper.MapExpression <Expression <Func <TDestination, object> > >(order?.Expression),
                orderDescending : order?.Direction == OrderDirection.Descending).AnyContext();

            return(result.Select(d => this.Options.Mapper.Map <TEntity>(d)));
        }
Ejemplo n.º 25
0
        public override async Task <List <T> > Find <T>(Expression <Func <T, bool> > filter, IFindOptions <T> options = null)
        {
            ValidateProperties();

            var filters = new List <FilterDefinition <T> >();

            filters.Add(filter == null ?
                        new ExpressionFilterDefinition <T>(_ => true) :
                        new ExpressionFilterDefinition <T>(filter));

            var ifModifedSinceExpression = CreateIfModifiedFilterExperssion(options);

            if (ifModifedSinceExpression != null)
            {
                filters.Add(ifModifedSinceExpression);
            }

            return((await Collection <T>().FindAsync(Builders <T> .Filter.And(filters))).ToList());
        }
Ejemplo n.º 26
0
 public async Task <IEnumerable <TEntity> > FindAllAsync(IEnumerable <ISpecification <TEntity> > specifications, IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
 {
     return(await this.decoratee.FindAllAsync(
                new[] { this.specification }.Concat(specifications.Safe()),
                options, cancellationToken).AnyContext());
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Finds all entities.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns></returns>
 public virtual async Task <IEnumerable <TEntity> > FindAllAsync(IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
 {
     return(await this.decoratee.FindAllAsync(options, cancellationToken).AnyContext());
 }
Ejemplo n.º 28
0
 public static Task <List <T> > Find <T>(this IBox box, IFindOptions <T> options = null)
 {
     return(box.Find <T>(null, options));
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Finds all entities.
 /// </summary>
 /// <param name="specifications">The specifications.</param>
 /// <param name="options">The options.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 public virtual async Task <IEnumerable <TEntity> > FindAllAsync(IEnumerable <ISpecification <TEntity> > specifications, IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
 {
     return(await this.inner.FindAllAsync(specifications, options, cancellationToken).AnyContext());
 }
Ejemplo n.º 30
0
        public async Task <IEnumerable <TEntity> > FindAllAsync(IEnumerable <ISpecification <TEntity> > specifications, IFindOptions <TEntity> options = null, CancellationToken cancellationToken = default)
        {
            var query = $@"
{this.configuration.LogName} |
 where {this.BuildQueryWhereParts(specifications).ToString(" and ")} |
 top {options?.Take ?? 2000} by LogProperties_{LogPropertyKeys.Ticks}_d desc"; // by TimeGenerated

            //order by LogProperties_{LogEventPropertyKeys.Ticks}_d desc |
            //skip ({page}-1) * {pageSize} | top {pageSize}
            // limit 100 | // 5000 = max

            this.logger.LogInformation($"{{LogKey:l}} log analytics query: {query}", LogKeys.Operations);

            // query docs: https://docs.microsoft.com/en-us/azure/log-analytics/query-language/get-started-queries
            //             https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-search-reference
            return(await this.FindAllAsync(query, cancellationToken).AnyContext());
        }