/// <summary>
 /// Adds a query to the sharded batch.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="query">The query.</param>
 /// <param name="afterLoad">Callback to execute when query is loaded. Loaded results are provided as action parameter.</param>
 /// <typeparam name="TResult">The type of the query result elements.</typeparam>
 /// <exception cref="InvalidOperationException">Thrown if the batch has already been executed.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="query"/> is <see langword="null"/>.</exception>
 /// <returns>The batch instance for method chain.</returns>
 public static IShardedQueryBatch Add <TResult>(this IShardedQueryBatch shardedBatch, IQueryable <TResult> query, Action <IList <TResult> > afterLoad = null)
 {
     shardedBatch.Add(new ShardedQueryBatchItem <TResult>(ToShardedQuery(query))
     {
         AfterLoadCallback = afterLoad
     });
     return(shardedBatch);
 }
 /// <summary>
 /// Adds a query to the sharded batch.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="query">The query.</param>
 /// <param name="afterLoad">Callback to execute when query is loaded. Loaded results are provided as action parameter.</param>
 /// <typeparam name="TResult">The type of the query result elements.</typeparam>
 /// <exception cref="InvalidOperationException">Thrown if the batch has already been executed.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="query"/> is <see langword="null"/>.</exception>
 /// <returns>The batch instance for method chain.</returns>
 public static IShardedQueryBatch Add <TResult>(this IShardedQueryBatch shardedBatch, DetachedCriteria query, Action <IList <TResult> > afterLoad = null)
 {
     shardedBatch.Add(new ShardedCriteriaBatchItem <TResult>(ToShardedCriteria(query, shardedBatch))
     {
         AfterLoadCallback = afterLoad
     });
     return(shardedBatch);
 }
        /// <summary>
        /// Adds a query to the sharded batch.
        /// </summary>
        /// <param name="shardedBatch">The batch.</param>
        /// <param name="query">The query.</param>
        /// <param name="selector">An aggregation function to apply to <paramref name="query"/>.</param>
        /// <param name="afterLoad">Callback to execute when query is loaded. Loaded results are provided as action parameter.</param>
        /// <typeparam name="TSource">The type of the query elements before aggregation.</typeparam>
        /// <typeparam name="TResult">The type resulting of the query result aggregation.</typeparam>
        /// <exception cref="InvalidOperationException">Thrown if the batch has already been executed.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="query"/> is <see langword="null"/>.</exception>
        /// <returns>The batch instance for method chain.</returns>
        public static IShardedQueryBatch Add <TSource, TResult>(this IShardedQueryBatch shardedBatch,
                                                                IQueryable <TSource> query, Expression <Func <IQueryable <TSource>, TResult> > selector, Action <TResult> afterLoad = null)
        {
            var batchItem = new ShardedQueryBatchItem <TResult>(ToShardedQuery(query, selector));

            if (afterLoad != null)
            {
                batchItem.AfterLoadCallback += list => afterLoad(list[0]);
            }
            shardedBatch.Add(batchItem);
            return(shardedBatch);
        }
 /// <summary>
 /// Adds a query to the sharded batch, returning it as an <see cref="IFutureValue{T}"/>.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="query">The query.</param>
 /// <typeparam name="TResult">The type of the query result elements.</typeparam>
 /// <returns>A future query which execution will be handled by the batch.</returns>
 public static IFutureValue <TResult> AddAsFutureValue <TResult>(this IShardedQueryBatch shardedBatch, IQuery query)
 {
     return(AddAsFutureValue(shardedBatch, new ShardedQueryBatchItem <TResult>(query)));
 }
 /// <summary>
 /// Adds an item to the sharded batch, returning it as an <see cref="IFutureValue{T}"/>.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="shardBatchItem">The batch item.</param>
 /// <typeparam name="TResult">The type of the query result elements.</typeparam>
 /// <returns>A future query which execution will be handled by the batch.</returns>
 public static IFutureValue <TResult> AddAsFutureValue <TResult>(this IShardedQueryBatch shardedBatch, IQueryBatchItem <TResult> shardBatchItem)
 {
     shardedBatch.Add(shardBatchItem);
     return(new FutureResult <TResult>(shardedBatch, shardedBatch.Count));
 }
 /// <summary>
 /// Adds a query to the sharded batch.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="key">A key for retrieval of the query result.</param>
 /// <param name="query">The query.</param>
 /// <typeparam name="TResult">The type of the query result elements.</typeparam>
 /// <exception cref="InvalidOperationException">Thrown if the batch has already been executed.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="query"/> is <see langword="null"/>.</exception>
 /// <returns>The batch instance for method chain.</returns>
 public static IShardedQueryBatch Add <TResult>(this IShardedQueryBatch shardedBatch, string key, IQuery query)
 {
     shardedBatch.Add(key, new ShardedQueryBatchItem <TResult>(query));
     return(shardedBatch);
 }
 /// <summary>
 /// Adds a query to the sharded batch, returning it as an <see cref="IFutureEnumerable{T}"/>.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="query">The query.</param>
 /// <typeparam name="TResult">The type of the query result elements.</typeparam>
 /// <returns>A future query which execution will be handled by the batch.</returns>
 public static IFutureEnumerable <TResult> AddAsFuture <TResult>(this IShardedQueryBatch shardedBatch, IQueryOver <TResult> query)
 {
     return(AddAsFuture(shardedBatch, new ShardedQueryOverBatchItem <TResult>(query)));
 }
        private static ICriteria ToShardedCriteria(DetachedCriteria query, IShardedQueryBatch shardedBatch)
        {
            var session = ((IShardedQueryBatchImplementor)shardedBatch).Session;

            return(query.GetExecutableCriteria(session));
        }
 /// <summary>
 /// Adds a query to the sharded batch, returning it as an <see cref="IFutureValue{T}"/>.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="query">The query.</param>
 /// <typeparam name="TResult">The type of the query result elements.</typeparam>
 /// <returns>A future query which execution will be handled by the batch.</returns>
 public static IFutureValue <TResult> AddAsFutureValue <TResult>(this IShardedQueryBatch shardedBatch, DetachedCriteria query)
 {
     return(AddAsFutureValue(shardedBatch, new ShardedCriteriaBatchItem <TResult>(ToShardedCriteria(query, shardedBatch))));
 }
 /// <summary>
 /// Adds a query to the sharded batch.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="key">A key for retrieval of the query result.</param>
 /// <param name="query">The query.</param>
 /// <typeparam name="TResult">The type of the query result elements.</typeparam>
 /// <exception cref="InvalidOperationException">Thrown if the batch has already been executed.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="query"/> is <see langword="null"/>.</exception>
 /// <returns>The batch instance for method chain.</returns>
 public static IShardedQueryBatch Add <TResult>(this IShardedQueryBatch shardedBatch, string key, DetachedCriteria query)
 {
     shardedBatch.Add(new ShardedCriteriaBatchItem <TResult>(ToShardedCriteria(query, shardedBatch)));
     return(shardedBatch);
 }
 /// <summary>
 /// Adds a query to the sharded batch, returning it as an <see cref="IFutureValue{T}"/>.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="query">The query.</param>
 /// <param name="selector">An aggregation function to apply to <paramref name="query"/>.</param>
 /// <typeparam name="TSource">The type of the query elements before aggregation.</typeparam>
 /// <typeparam name="TResult">The type resulting of the query result aggregation.</typeparam>
 /// <returns>A future query which execution will be handled by the batch.</returns>
 public static IFutureValue <TResult> AddAsFutureValue <TSource, TResult>(this IShardedQueryBatch shardedBatch,
                                                                          IQueryable <TSource> query, Expression <Func <IQueryable <TSource>, TResult> > selector)
 {
     return(AddAsFutureValue(shardedBatch, new ShardedQueryBatchItem <TResult>(ToShardedQuery(query, selector))));
 }
 /// <summary>
 /// Adds a query to the sharded batch.
 /// </summary>
 /// <param name="shardedBatch">The batch.</param>
 /// <param name="key">A key for retrieval of the query result.</param>
 /// <param name="query">The query.</param>
 /// <param name="selector">An aggregation function to apply to <paramref name="query"/>.</param>
 /// <typeparam name="TSource">The type of the query elements before aggregation.</typeparam>
 /// <typeparam name="TResult">The type resulting of the query result aggregation.</typeparam>
 /// <exception cref="InvalidOperationException">Thrown if the batch has already been executed.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="query"/> is <see langword="null"/>.</exception>
 /// <returns>The batch instance for method chain.</returns>
 public static IShardedQueryBatch Add <TSource, TResult>(this IShardedQueryBatch shardedBatch, string key,
                                                         IQueryable <TSource> query, Expression <Func <IQueryable <TSource>, TResult> > selector)
 {
     shardedBatch.Add(new ShardedQueryBatchItem <TResult>(ToShardedQuery(query, selector)));
     return(shardedBatch);
 }