-
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.
Plugin support for EvaluatableExpressionFilter
Closes #13454
- Loading branch information
Showing
5 changed files
with
130 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Represents a filter for evaluatable expressions. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Singleton" />. This means a single instance | ||
/// is used by many <see cref="DbContext" /> instances. The implementation must be thread-safe. | ||
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped" />. | ||
/// </para> | ||
/// </summary> | ||
public class EvaluatableExpressionFilterBase : IEvaluatableExpressionFilter | ||
{ | ||
/// <summary> | ||
/// Parameter object containing dependencies for this service. | ||
/// </summary> | ||
protected virtual EvaluatableExpressionFilterDependencies Dependencies { get; } | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Creates a new <see cref="EvaluatableExpressionFilter" /> instance. | ||
/// </para> | ||
/// <para> | ||
/// This type is typically used by database providers (and other extensions). It is generally | ||
/// not used in application code. | ||
/// </para> | ||
/// </summary> | ||
/// <param name="dependencies"> The dependencies to use. </param> | ||
public EvaluatableExpressionFilterBase( | ||
[NotNull] EvaluatableExpressionFilterDependencies dependencies) | ||
{ | ||
Check.NotNull(dependencies, nameof(dependencies)); | ||
|
||
Dependencies = dependencies; | ||
} | ||
|
||
/// <summary> | ||
/// Checks whether the given expression can be evaluated. | ||
/// </summary> | ||
/// <param name="expression"> The expression. </param> | ||
/// <param name="model"> The model. </param> | ||
/// <returns> True if the expression can be evaluated; false otherwise. </returns> | ||
public virtual bool IsEvaluatableExpression(Expression expression, IModel model) | ||
{ | ||
Check.NotNull(expression, nameof(expression)); | ||
Check.NotNull(model, nameof(model)); | ||
|
||
foreach (var plugin in Dependencies.Plugins) | ||
{ | ||
if (!plugin.IsEvaluatableExpression(expression, model)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
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,33 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Represents a plugin evaluatable expression filter. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Singleton" /> and multiple registrations | ||
/// are allowed. This means a single instance of each service is used by many <see cref="DbContext" /> | ||
/// instances. The implementation must be thread-safe. | ||
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped" />. | ||
/// </para> | ||
/// </summary> | ||
public interface IEvaluatableExpressionFilterPlugin | ||
{ | ||
/// <summary> | ||
/// Checks whether the given expression can be evaluated. | ||
/// </summary> | ||
/// <param name="expression"> The expression. </param> | ||
/// <param name="model"> The model. </param> | ||
/// <returns> True if the expression can be evaluated; false otherwise. </returns> | ||
bool IsEvaluatableExpression([NotNull] Expression expression, [NotNull] IModel model); | ||
} | ||
} |