示例#1
0
        /// <summary>
        ///     Defer the execution of the <paramref name="query" /> and batch the query command with other
        ///     future queries. The batch is executed when a future query requires a database round trip.
        /// </summary>
        /// <typeparam name="T">The type of elements of the query.</typeparam>
        /// <param name="query">
        ///     The query to defer the execution of and to add in the batch of future
        ///     queries.
        /// </param>
        /// <returns>
        ///     The QueryFutureEnumerable&lt;TEntity&gt; added to the batch of futures queries.
        /// </returns>
        public static QueryFutureEnumerable <T> Future <T>(this IQueryable <T> query)
        {
#if EF5 || EF6
            var objectQuery = query.GetObjectQuery();
            var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);
            var futureQuery = new QueryFutureEnumerable <T>(futureBatch, objectQuery);
#elif EFCORE
            QueryFutureBatch          futureBatch;
            QueryFutureEnumerable <T> futureQuery;
            if (query.IsInMemoryQueryContext())
            {
                var context = query.GetInMemoryContext();
                futureBatch            = QueryFutureManager.AddOrGetBatch(context);
                futureBatch.IsInMemory = true;
                futureQuery            = new QueryFutureEnumerable <T>(futureBatch, query);
            }
            else
            {
                var context = query.GetDbContext();
                futureBatch = QueryFutureManager.AddOrGetBatch(context);
                futureQuery = new QueryFutureEnumerable <T>(futureBatch, query);
            }
#endif
            futureBatch.Queries.Add(futureQuery);

            return(futureQuery);
        }
        /// <summary>
        ///     Defer the execution of the <paramref name="query" /> and batch the query command with other
        ///     future queries. The batch is executed when a future query requires a database round trip.
        /// </summary>
        /// <typeparam name="TResult">The type of the query result.</typeparam>
        /// <param name="query">
        ///     The query to defer the execution of and to add in the batch of future
        ///     queries.
        /// </param>
        /// <returns>
        ///     The QueryFutureValue&lt;TResult,TResult&gt; added to the batch of futures queries.
        /// </returns>
        public static QueryFutureValue <TResult> FutureValue <TResult>(this IQueryable <TResult> query)
        {
            if (!QueryFutureManager.AllowQueryBatch)
            {
                var futureValue = new QueryFutureValue <TResult>(null, null);
                futureValue.GetResultDirectly(query);
                return(futureValue);
            }

#if EF5 || EF6
            var objectQuery = query.GetObjectQuery();
            var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);
            var futureQuery = new QueryFutureValue <TResult>(futureBatch, objectQuery);
#elif EFCORE
            QueryFutureBatch           futureBatch;
            QueryFutureValue <TResult> futureQuery;
            if (query.IsInMemoryQueryContext())
            {
                var context = query.GetInMemoryContext();
                futureBatch            = QueryFutureManager.AddOrGetBatch(context);
                futureBatch.IsInMemory = true;
                futureQuery            = new QueryFutureValue <TResult>(futureBatch, query);
            }
            else
            {
                var context = query.GetDbContext();
                futureBatch = QueryFutureManager.AddOrGetBatch(context);
                futureQuery = new QueryFutureValue <TResult>(futureBatch, query);
            }
#endif
            futureBatch.Queries.Add(futureQuery);

            return(futureQuery);
        }
示例#3
0
        /// <summary>A DelayedQuery&lt;TResult&gt; extension method that future value.</summary>
        /// <typeparam name="TResult">Type of the result.</typeparam>
        /// <param name="query">The query to act on.</param>
        /// <returns>A FutureQueryValue&lt;TResult,TResult&gt;</returns>
        public static QueryFutureValue <TResult> FutureValue <TResult>(this IQueryable <TResult> query)
        {
            var objectQuery = query.GetObjectQuery();
            var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);
            var futureQuery = new QueryFutureValue <TResult>(futureBatch, objectQuery);

            futureBatch.Queries.Add(futureQuery);

            return(futureQuery);
        }
        /// <summary>
        ///     An IQueryable&lt;TEntity&gt; extension method that futures the given query.
        /// </summary>
        /// <typeparam name="TEntity">Type of the entity.</typeparam>
        /// <param name="query">The query to act on.</param>
        /// <returns>A QueryFutureEnumerable&lt;TEntity&gt;</returns>
        public static QueryFutureEnumerable <TEntity> Future <TEntity>(this IQueryable <TEntity> query)
        {
            var objectQuery = query.GetObjectQuery();
            var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);
            var futureQuery = new QueryFutureEnumerable <TEntity>(futureBatch, objectQuery);

            futureBatch.Queries.Add(futureQuery);

            return(futureQuery);
        }
示例#5
0
        /// <summary>
        ///     Defer the execution of the <paramref name="query" /> and batch the query command with other
        ///     future queries. The batch is executed when a future query requires a database round trip.
        /// </summary>
        /// <typeparam name="T">The type of elements of the query.</typeparam>
        /// <param name="query">
        ///     The query to defer the execution of and to add in the batch of future
        ///     queries.
        /// </param>
        /// <returns>
        ///     The QueryFutureEnumerable&lt;TEntity&gt; added to the batch of futures queries.
        /// </returns>
        internal static QueryFutureEnumerable <T> Future <T>(this IQueryable <T> query)
        {
#if EF5 || EF6
            var objectQuery = query.GetObjectQuery();
            var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);
            var futureQuery = new QueryFutureEnumerable <T>(futureBatch, objectQuery);
#elif EF7
            var context     = query.GetDbContext();
            var futureBatch = QueryFutureManager.AddOrGetBatch(context);
            var futureQuery = new QueryFutureEnumerable <T>(futureBatch, query);
#endif
            futureBatch.Queries.Add(futureQuery);

            return(futureQuery);
        }
        /// <summary>
        ///     Defer the execution of the <paramref name="query" /> and batch the query command with other
        ///     future queries. The batch is executed when a future query requires a database round trip.
        /// </summary>
        /// <typeparam name="TResult">The type of the query result.</typeparam>
        /// <param name="query">
        ///     The query to defer the execution of and to add in the batch of future
        ///     queries.
        /// </param>
        /// <returns>
        ///     The QueryFutureValue&lt;TResult,TResult&gt; added to the batch of futures queries.
        /// </returns>
        public static QueryFutureValue <TResult> FutureValue <TResult>(this IQueryable <TResult> query)
        {
#if EF5 || EF6
            var objectQuery = query.GetObjectQuery();
            var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context);
            var futureQuery = new QueryFutureValue <TResult>(futureBatch, objectQuery);
#elif EFCORE
            var context     = query.GetDbContext();
            var futureBatch = QueryFutureManager.AddOrGetBatch(context);
            var futureQuery = new QueryFutureValue <TResult>(futureBatch, query);
#endif
            futureBatch.Queries.Add(futureQuery);

            return(futureQuery);
        }
示例#7
0
        /// <summary>Executes the queries operation.</summary>
        public void ExecuteQueries()
        {
            var connection = (EntityConnection)Context.Connection;
            var command    = CreateCommand();

            var ownConnection = false;

            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                    ownConnection = true;
                }

                using (command)
                {
#if EF5
                    using (var reader = command.ExecuteReader())
#elif EF6
                    var interceptionContext = Context.GetInterceptionContext();
                    using (var reader = DbInterception.Dispatch.Command.Reader(command, new DbCommandInterceptionContext(interceptionContext)))
#endif
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
                    }
                }
            }
            finally
            {
                QueryFutureManager.RemoveBatch(this);

                if (ownConnection)
                {
                    connection.Close();
                }
            }
        }