From 26b14537c2b64220e10577590ade911c01b13f02 Mon Sep 17 00:00:00 2001 From: medkg15 Date: Wed, 7 Aug 2024 09:19:19 -0400 Subject: [PATCH 1/2] Add extension points before and after a Future query is run. --- .../Z.EF.Plus.QueryFuture.Shared/QueryFutureBatch.cs | 2 ++ .../Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureBatch.cs b/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureBatch.cs index f3a689e5..045a9b1b 100644 --- a/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureBatch.cs +++ b/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureBatch.cs @@ -183,6 +183,7 @@ public void ExecuteQueries() using (command) { + QueryFutureManager.OnBatchExecuting?.Invoke(command); #if EF5 using (var reader = command.ExecuteReader()) { @@ -213,6 +214,7 @@ public void ExecuteQueries() } } #endif + QueryFutureManager.OnBatchExecuted?.Invoke(command); } } finally diff --git a/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs b/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs index aa5dfebc..ffce9a84 100644 --- a/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs +++ b/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs @@ -7,6 +7,8 @@ using System.Runtime.CompilerServices; using Z.EntityFramework.Extensions; +using System; +using System.Data.Common; #if NET45 || EFCORE @@ -53,6 +55,12 @@ static QueryFutureManager() /// True if allow query batch, false if not. public static bool AllowQueryBatch { get; set; } = true; + /// Gets or sets a delegate to be invoked directly before executing the batch DbCommand. + public static Action OnBatchExecuting { get; set; } = cmd => { }; + + /// Gets or sets a delegate to be invoked directly after executing the batch DbCommand. + public static Action OnBatchExecuted { get; set; } = cmd => { }; + /// Gets or sets the weak table used to cache future batch associated to a context. /// The weak table used to cache future batch associated to a context. #if EF5 || EF6 From f0222ac8753d22f7edff36564cb3f922e322f045 Mon Sep 17 00:00:00 2001 From: medkg15 Date: Wed, 7 Aug 2024 12:09:23 -0400 Subject: [PATCH 2/2] Skip action invocation by default. Clarify documentation. --- .../QueryFutureManager.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs b/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs index ffce9a84..da51d2d2 100644 --- a/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs +++ b/src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureManager.cs @@ -56,10 +56,18 @@ static QueryFutureManager() public static bool AllowQueryBatch { get; set; } = true; /// Gets or sets a delegate to be invoked directly before executing the batch DbCommand. - public static Action OnBatchExecuting { get; set; } = cmd => { }; + /// + /// 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. + /// + public static Action OnBatchExecuting { get; set; } = null; /// Gets or sets a delegate to be invoked directly after executing the batch DbCommand. - public static Action OnBatchExecuted { get; set; } = cmd => { }; + /// + /// 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. + /// + public static Action OnBatchExecuted { get; set; } = null; /// Gets or sets the weak table used to cache future batch associated to a context. /// The weak table used to cache future batch associated to a context.