-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query expression interceptor (#28351)
- Loading branch information
1 parent
9952893
commit e26e7cf
Showing
21 changed files
with
703 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
/// <summary> | ||
/// Allows interception of query expression trees and resulting compiled delegates. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// Use <see cref="DbContextOptionsBuilder.AddInterceptors(Microsoft.EntityFrameworkCore.Diagnostics.IInterceptor[])" /> | ||
/// to register application interceptors. | ||
/// </para> | ||
/// <para> | ||
/// Extensions can also register interceptors in the internal service provider. | ||
/// If both injected and application interceptors are found, then the injected interceptors are run in the | ||
/// order that they are resolved from the service provider, and then the application interceptors are run last. | ||
/// </para> | ||
/// <para> | ||
/// See <see href="https://aka.ms/efcore-docs-interceptors">EF Core interceptors</see> for more information and examples. | ||
/// </para> | ||
/// </remarks> | ||
public interface IQueryExpressionInterceptor : IInterceptor | ||
{ | ||
/// <summary> | ||
/// Called with the LINQ expression tree for a query before it is compiled. | ||
/// </summary> | ||
/// <param name="queryExpression">The query expression.</param> | ||
/// <param name="eventData">Contextual information about the query environment.</param> | ||
/// <returns>The query expression tree to continue with, which may have been changed by the interceptor.</returns> | ||
Expression ProcessingQuery( | ||
Expression queryExpression, | ||
QueryExpressionEventData eventData) | ||
=> queryExpression; | ||
|
||
/// <summary> | ||
/// Called when EF is about to compile the query delegate that will be used to execute the query. | ||
/// </summary> | ||
/// <param name="queryExpression">The query expression.</param> | ||
/// <param name="queryExecutorExpression">The expression that will be compiled into the execution delegate.</param> | ||
/// <param name="eventData">Contextual information about the query environment.</param> | ||
/// <typeparam name="TResult">The return type of the execution delegate.</typeparam> | ||
/// <returns>The expression that will be compiled into the execution delegate, which may have been changed by the interceptor.</returns> | ||
Expression<Func<QueryContext, TResult>> CompilingQuery<TResult>( | ||
Expression queryExpression, | ||
Expression<Func<QueryContext, TResult>> queryExecutorExpression, | ||
QueryExpressionEventData eventData) | ||
=> queryExecutorExpression; | ||
|
||
/// <summary> | ||
/// Called when EF is about to compile the query delegate that will be used to execute the query. | ||
/// </summary> | ||
/// <param name="queryExpression">The query expression.</param> | ||
/// <param name="eventData">Contextual information about the query environment.</param> | ||
/// <param name="queryExecutor">The delegate that will be used to execute the query.</param> | ||
/// <typeparam name="TResult">The return type of the execution delegate.</typeparam> | ||
/// <returns>The delegate that will be used to execute the query, which may have been changed by the interceptor.</returns> | ||
Func<QueryContext, TResult> CompiledQuery<TResult>( | ||
Expression queryExpression, | ||
QueryExpressionEventData eventData, | ||
Func<QueryContext, TResult> queryExecutor) | ||
=> queryExecutor; | ||
} |
65 changes: 65 additions & 0 deletions
65
src/EFCore/Diagnostics/Internal/QueryExpressionInterceptorAggregator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Diagnostics.Internal; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class QueryExpressionInterceptorAggregator : InterceptorAggregator<IQueryExpressionInterceptor> | ||
{ | ||
/// <inheritdoc /> | ||
protected override IQueryExpressionInterceptor CreateChain(IEnumerable<IQueryExpressionInterceptor> interceptors) | ||
=> new CompositeQueryExpressionInterceptor(interceptors); | ||
|
||
private sealed class CompositeQueryExpressionInterceptor : IQueryExpressionInterceptor | ||
{ | ||
private readonly IQueryExpressionInterceptor[] _interceptors; | ||
|
||
public CompositeQueryExpressionInterceptor(IEnumerable<IQueryExpressionInterceptor> interceptors) | ||
{ | ||
_interceptors = interceptors.ToArray(); | ||
} | ||
|
||
public Expression ProcessingQuery( | ||
Expression queryExpression, | ||
QueryExpressionEventData eventData) | ||
{ | ||
for (var i = 0; i < _interceptors.Length; i++) | ||
{ | ||
queryExpression = _interceptors[i].ProcessingQuery(queryExpression, eventData); | ||
} | ||
|
||
return queryExpression; | ||
} | ||
|
||
public Expression<Func<QueryContext, TResult>> CompilingQuery<TResult>( | ||
Expression queryExpression, | ||
Expression<Func<QueryContext, TResult>> queryExecutorExpression, | ||
QueryExpressionEventData eventData) | ||
{ | ||
for (var i = 0; i < _interceptors.Length; i++) | ||
{ | ||
queryExecutorExpression = _interceptors[i].CompilingQuery(queryExpression, queryExecutorExpression, eventData); | ||
} | ||
|
||
return queryExecutorExpression; | ||
} | ||
|
||
public Func<QueryContext, TResult> CompiledQuery<TResult>( | ||
Expression queryExpression, | ||
QueryExpressionEventData eventData, | ||
Func<QueryContext, TResult> queryExecutor) | ||
{ | ||
for (var i = 0; i < _interceptors.Length; i++) | ||
{ | ||
queryExecutor = _interceptors[i].CompiledQuery(queryExpression, eventData, queryExecutor); | ||
} | ||
|
||
return queryExecutor; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
test/EFCore.Cosmos.FunctionalTests/QueryExpressionInterceptionWithDiagnosticsCosmosTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos; | ||
|
||
public class QueryExpressionInterceptionWithDiagnosticsCosmosTest | ||
: QueryExpressionInterceptionTestBase, | ||
IClassFixture<QueryExpressionInterceptionWithDiagnosticsCosmosTest.InterceptionCosmosFixture> | ||
{ | ||
public QueryExpressionInterceptionWithDiagnosticsCosmosTest(InterceptionCosmosFixture fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
public class InterceptionCosmosFixture : InterceptionFixtureBase | ||
{ | ||
protected override ITestStoreFactory TestStoreFactory | ||
=> CosmosTestStoreFactory.Instance; | ||
|
||
protected override IServiceCollection InjectInterceptors( | ||
IServiceCollection serviceCollection, | ||
IEnumerable<IInterceptor> injectedInterceptors) | ||
=> base.InjectInterceptors(serviceCollection.AddEntityFrameworkCosmos(), injectedInterceptors); | ||
|
||
protected override string StoreName | ||
=> "QueryExpressionInterceptionWithDiagnostics"; | ||
|
||
protected override bool ShouldSubscribeToDiagnosticListener | ||
=> true; | ||
} | ||
} |
Oops, something went wrong.