-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathQueryFutureManager.cs
123 lines (103 loc) · 4.6 KB
/
QueryFutureManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
using System.Runtime.CompilerServices;
using Z.EntityFramework.Extensions;
using System;
using System.Data.Common;
#if NET45 || EFCORE
using System.Threading;
using System.Threading.Tasks;
#endif
#if EF5
using System.Data.Objects;
using System.Data.Entity;
#elif EF6
using System.Data.Entity.Core.Objects;
using System.Data.Entity;
#elif EFCORE
using Microsoft.EntityFrameworkCore;
#endif
namespace Z.EntityFramework.Plus
{
/// <summary>Manage EF+ Query Future Configuration.</summary>
#if QUERY_INCLUDEOPTIMIZED
internal static class QueryFutureManager
#else
public static class QueryFutureManager
#endif
{
/// <summary>Static constructor.</summary>
static QueryFutureManager()
{
EntityFrameworkManager.IsEntityFrameworkPlus = true;
#if EF5 || EF6
CacheWeakFutureBatch = new ConditionalWeakTable<ObjectContext, QueryFutureBatch>();
#elif EFCORE
CacheWeakFutureBatch = new System.Runtime.CompilerServices.ConditionalWeakTable<DbContext, QueryFutureBatch>();
#endif
}
/// <summary>Gets or sets a value indicating whether we allow query batch.</summary>
/// <value>True if allow query batch, false if not.</value>
public static bool AllowQueryBatch { get; set; } = true;
/// <summary>Gets or sets a delegate to be invoked directly before executing the batch DbCommand.</summary>
/// <remarks>
/// This delegate is only invoked when queries are actually executed as a batch containing multiple queries.
/// i.e. When AllowQueryBatch=false or only a single query is pending, this delegate is not invoked.
/// </remarks>
public static Action<DbCommand> OnBatchExecuting { get; set; } = null;
/// <summary>Gets or sets a delegate to be invoked directly after executing the batch DbCommand.</summary>
/// <remarks>
/// This delegate is only invoked when queries are actually executed as a batch containing multiple queries.
/// i.e. When AllowQueryBatch=false or only a single query is pending, this delegate is not invoked.
/// </remarks>
public static Action<DbCommand> OnBatchExecuted { get; set; } = null;
/// <summary>Gets or sets the weak table used to cache future batch associated to a context.</summary>
/// <value>The weak table used to cache future batch associated to a context.</value>
#if EF5 || EF6
public static ConditionalWeakTable<ObjectContext, QueryFutureBatch> CacheWeakFutureBatch { get; set; }
#elif EFCORE
public static System.Runtime.CompilerServices.ConditionalWeakTable<DbContext, QueryFutureBatch> CacheWeakFutureBatch { get; set; }
#endif
/// <summary>Adds or gets the future batch associated to the context.</summary>
/// <param name="context">The context used to cache the future batch.</param>
/// <returns>The future batch associated to the context.</returns>
#if EF5 || EF6
public static QueryFutureBatch AddOrGetBatch(ObjectContext context)
#elif EFCORE
public static QueryFutureBatch AddOrGetBatch(DbContext context)
#endif
{
QueryFutureBatch futureBatch;
if (!CacheWeakFutureBatch.TryGetValue(context, out futureBatch))
{
futureBatch = new QueryFutureBatch(context);
CacheWeakFutureBatch.Add(context, futureBatch);
}
return futureBatch;
}
public static void ExecuteBatch(DbContext context)
{
#if EF5 || EF6
var batch = AddOrGetBatch(context.GetObjectContext());
#elif EFCORE
var batch = AddOrGetBatch(context);
#endif
batch.ExecuteQueries();
}
#if NET45 || EFCORE
public static async Task ExecuteBatchAsync(DbContext context, CancellationToken cancellationToken = default(CancellationToken))
{
#if EF5 || EF6
var batch = AddOrGetBatch(context.GetObjectContext());
#elif EFCORE
var batch = AddOrGetBatch(context);
#endif
await batch.ExecuteQueriesAsync(cancellationToken).ConfigureAwait(false);
}
#endif
}
}