Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static FutureQuery<TEntity> Future<TEntity>(this IQueryable<TEntity> sour
throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");

var futureContext = GetFutureContext(sourceQuery);
var future = new FutureQuery<TEntity>(sourceQuery, futureContext.ExecuteFutureQueries);
var future = new FutureQuery<TEntity>(sourceQuery, futureContext.ExecuteFutureQueriesAsync);
futureContext.AddQuery(future);

return future;
Expand Down Expand Up @@ -69,7 +69,7 @@ public static FutureCount FutureCount<TEntity>(this IQueryable<TEntity> source)
throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");

var futureContext = GetFutureContext(sourceQuery);
var future = new FutureCount(countQuery, futureContext.ExecuteFutureQueries);
var future = new FutureCount(countQuery, futureContext.ExecuteFutureQueriesAsync);
futureContext.AddQuery(future);
return future;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public static FutureValue<TResult> FutureValue<TEntity, TResult>(this IQueryable
throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");

var futureContext = GetFutureContext(sourceQuery);
var future = new FutureValue<TResult>(valueQuery, futureContext.ExecuteFutureQueries);
var future = new FutureValue<TResult>(valueQuery, futureContext.ExecuteFutureQueriesAsync);
futureContext.AddQuery(future);
return future;
}
Expand Down Expand Up @@ -141,7 +141,7 @@ public static FutureValue<TEntity> FutureFirstOrDefault<TEntity>(this IQueryable
throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");

var futureContext = GetFutureContext(sourceQuery);
var future = new FutureValue<TEntity>(objectQuery, futureContext.ExecuteFutureQueries);
var future = new FutureValue<TEntity>(objectQuery, futureContext.ExecuteFutureQueriesAsync);
futureContext.AddQuery(future);
return future;
}
Expand Down
7 changes: 5 additions & 2 deletions Source/EntityFramework.Extended/Future/FutureContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Threading.Tasks;

namespace EntityFramework.Future
{
Expand Down Expand Up @@ -69,10 +70,12 @@ public bool IsAlive
get { return _objectContext.IsAlive; }
}



/// <summary>
/// Executes the future queries as a single batch.
/// </summary>
public void ExecuteFutureQueries()
public async Task ExecuteFutureQueriesAsync()
{
ObjectContext context = ObjectContext;
if (context == null)
Expand All @@ -82,7 +85,7 @@ public void ExecuteFutureQueries()
if (runner == null)
throw new InvalidOperationException("Could not resolve the IFutureRunner. Make sure IFutureRunner is registered in the Locator.Current container.");

runner.ExecuteFutureQueries(context, FutureQueries);
await runner.ExecuteFutureQueriesAsync(context, FutureQueries);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Source/EntityFramework.Extended/Future/FutureCount.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace EntityFramework.Future
{
Expand Down Expand Up @@ -31,7 +32,7 @@ public class FutureCount : FutureValue<int>
/// </summary>
/// <param name="query">The query source to use when materializing.</param>
/// <param name="loadAction">The action to execute when the query is accessed.</param>
internal FutureCount(IQueryable query, Action loadAction)
internal FutureCount(IQueryable query, Func<Task> loadAction)
: base(query, loadAction)
{ }

Expand Down
14 changes: 12 additions & 2 deletions Source/EntityFramework.Extended/Future/FutureQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace EntityFramework.Future
{
Expand Down Expand Up @@ -30,7 +31,7 @@ public class FutureQuery<T> : FutureQueryBase<T>, IEnumerable<T>
/// </summary>
/// <param name="query">The query source to use when materializing.</param>
/// <param name="loadAction">The action to execute when the query is accessed.</param>
internal FutureQuery(IQueryable query, Action loadAction)
internal FutureQuery(IQueryable query, Func<Task> loadAction)
: base(query, loadAction)
{ }

Expand All @@ -43,14 +44,23 @@ internal FutureQuery(IQueryable query, Action loadAction)
public IEnumerator<T> GetEnumerator()
{
// triggers loading future queries
var result = GetResult() ?? Enumerable.Empty<T>();
var result = GetResultAsync().Result?? Enumerable.Empty<T>();

if (Exception != null)
throw new FutureException("An error occurred executing the future query.", Exception);

return result.GetEnumerator();
}

public async Task<List<T>> ToListAsync()
{
var result = await GetResultAsync();

return result.ToList();

}


/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
Expand Down
11 changes: 6 additions & 5 deletions Source/EntityFramework.Extended/Future/FutureQueryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Linq;
using EntityFramework.Reflection;
using System.Threading.Tasks;

namespace EntityFramework.Future
{
Expand All @@ -15,7 +16,7 @@ namespace EntityFramework.Future
[DebuggerDisplay("IsLoaded={IsLoaded}")]
public abstract class FutureQueryBase<T> : IFutureQuery
{
private readonly Action _loadAction;
private readonly Func<Task> _loadAction;
private readonly IQueryable _query;
private IEnumerable<T> _result;
private bool _isLoaded;
Expand All @@ -25,7 +26,7 @@ public abstract class FutureQueryBase<T> : IFutureQuery
/// </summary>
/// <param name="query">The query source to use when materializing.</param>
/// <param name="loadAction">The action to execute when the query is accessed.</param>
protected FutureQueryBase(IQueryable query, Action loadAction)
protected FutureQueryBase(IQueryable query, Func<Task> loadAction)
{
_query = query;
_loadAction = loadAction;
Expand All @@ -36,7 +37,7 @@ protected FutureQueryBase(IQueryable query, Action loadAction)
/// Gets the action to execute when the query is accessed.
/// </summary>
/// <value>The load action.</value>
protected Action LoadAction
protected Func<Task> LoadAction
{
get { return _loadAction; }
}
Expand Down Expand Up @@ -71,7 +72,7 @@ IQueryable IFutureQuery.Query
/// <returns>
/// An <see cref="T:System.Collections.Generic.IEnumerable`1"/> that can be used to iterate through the collection.
/// </returns>
protected virtual IEnumerable<T> GetResult()
protected virtual async Task<IEnumerable<T>> GetResultAsync()
{
if (IsLoaded)
return _result;
Expand All @@ -86,7 +87,7 @@ protected virtual IEnumerable<T> GetResult()

// invoke the load action on the datacontext
// result will be set with a callback to SetResult
LoadAction.Invoke();
await LoadAction.Invoke();
return _result ?? Enumerable.Empty<T>();
}

Expand Down
21 changes: 19 additions & 2 deletions Source/EntityFramework.Extended/Future/FutureRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Data.Entity.Core.Objects;
using System.Text;
using EntityFramework.Reflection;
using System.Threading.Tasks;
using System.Diagnostics;

namespace EntityFramework.Future
{
Expand All @@ -13,12 +15,13 @@ namespace EntityFramework.Future
/// </summary>
public class FutureRunner : IFutureRunner
{

/// <summary>
/// Executes the future queries.
/// </summary>
/// <param name="context">The <see cref="ObjectContext"/> to run the queries against.</param>
/// <param name="futureQueries">The future queries list.</param>
public void ExecuteFutureQueries(ObjectContext context, IList<IFutureQuery> futureQueries)
public async Task ExecuteFutureQueriesAsync(ObjectContext context, IList<IFutureQuery> futureQueries)
{
if (context == null)
throw new ArgumentNullException("context");
Expand All @@ -31,9 +34,20 @@ public void ExecuteFutureQueries(ObjectContext context, IList<IFutureQuery> futu

try
{
#if DEBUG
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
#endif
using (var command = CreateFutureCommand(context, futureQueries))
using (var reader = command.ExecuteReader())
using (var reader = await command.ExecuteReaderAsync())
{
#if DEBUG
Debug.WriteLine("Executing Query: ");
Debug.WriteLine(command.CommandText);
Debug.WriteLine("Time Elapsed: " + sw.ElapsedMilliseconds.ToString("f2") + "ms");

#endif

foreach (var futureQuery in futureQueries)
{
futureQuery.SetResult(context, reader);
Expand Down Expand Up @@ -110,5 +124,8 @@ private static DbCommand CreateFutureCommand(ObjectContext context, IEnumerable<

return command;
}



}
}
11 changes: 9 additions & 2 deletions Source/EntityFramework.Extended/Future/FutureValue.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace EntityFramework.Future
{
Expand Down Expand Up @@ -30,7 +31,7 @@ public class FutureValue<T> : FutureQueryBase<T>
/// </summary>
/// <param name="query">The query source to use when materializing.</param>
/// <param name="loadAction">The action to execute when the query is accessed.</param>
internal FutureValue(IQueryable query, Action loadAction)
internal FutureValue(IQueryable query, Func<Task> loadAction)
: base(query, loadAction)
{ }

Expand Down Expand Up @@ -60,7 +61,7 @@ public T Value
{
_hasValue = true;

var result = GetResult() ?? Enumerable.Empty<T>();
var result = GetResultAsync().Result ?? Enumerable.Empty<T>();
UnderlyingValue = result.FirstOrDefault();
}

Expand All @@ -76,6 +77,12 @@ public T Value
}
}

public async Task<T> ValueAsync()
{
return (await GetResultAsync()).FirstOrDefault();
}


/// <summary>
/// Performs an implicit conversion from <see cref="T:EntityFramework.Future.FutureValue`1" /> to T.
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion Source/EntityFramework.Extended/Future/IFutureContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EntityFramework.Future
{
Expand All @@ -14,10 +15,12 @@ public interface IFutureContext
/// <value>The future queries.</value>
IList<IFutureQuery> FutureQueries { get; }



/// <summary>
/// Executes the future queries.
/// </summary>
void ExecuteFutureQueries();
Task ExecuteFutureQueriesAsync();

/// <summary>
/// Adds the future query to the waiting queries list on this context.
Expand Down
5 changes: 4 additions & 1 deletion Source/EntityFramework.Extended/Future/IFutureRunner.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Threading.Tasks;

namespace EntityFramework.Future
{
Expand All @@ -9,11 +10,13 @@ namespace EntityFramework.Future
/// </summary>
public interface IFutureRunner
{


/// <summary>
/// Executes the future queries.
/// </summary>
/// <param name="context">The <see cref="ObjectContext"/> to run the queries against.</param>
/// <param name="futureQueries">The future queries list.</param>
void ExecuteFutureQueries(ObjectContext context, IList<IFutureQuery> futureQueries);
Task ExecuteFutureQueriesAsync(ObjectContext context, IList<IFutureQuery> futureQueries);
}
}