Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make EvaluableExpressionFilter public #16776

Merged
merged 1 commit into from
Jul 26, 2019
Merged
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 @@ -192,7 +192,8 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
.AddDependencyScoped<HistoryRepositoryDependencies>()
.AddDependencyScoped<RelationalCompiledQueryCacheKeyGeneratorDependencies>()
.AddDependencyScoped<RelationalConnectionDependencies>()
.AddDependencyScoped<RelationalDatabaseDependencies>();
.AddDependencyScoped<RelationalDatabaseDependencies>()
.AddDependencyScoped<RelationalEvaluatableExpressionFilterDependencies>();

return base.TryAddCoreServices();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.Linq.Expressions;
Expand All @@ -7,7 +7,7 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Query.Internal
namespace Microsoft.EntityFrameworkCore.Query
{
/// <summary>
/// <para>
Expand All @@ -33,13 +33,22 @@ public class RelationalEvaluatableExpressionFilter : EvaluatableExpressionFilter
/// 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 RelationalEvaluatableExpressionFilter([NotNull] IModel model)
public RelationalEvaluatableExpressionFilter(
[NotNull] EvaluatableExpressionFilterDependencies dependencies,
[NotNull] RelationalEvaluatableExpressionFilterDependencies relationalDependencies)
: base(dependencies)
{
Check.NotNull(model, nameof(model));
Check.NotNull(relationalDependencies, nameof(relationalDependencies));

_model = model;
RelationalDependencies = relationalDependencies;
_model = relationalDependencies.Model;
}

/// <summary>
/// Dependencies used to create a <see cref="RelationalEvaluatableExpressionFilter" />
/// </summary>
protected virtual RelationalEvaluatableExpressionFilterDependencies RelationalDependencies { get; }

/// <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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Query
{
/// <summary>
/// <para>
/// Service dependencies parameter class for <see cref="RelationalEvaluatableExpressionFilter" />
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// <para>
/// Do not construct instances of this class directly from either provider or application code as the
/// constructor signature may change as new dependencies are added. Instead, use this type in
/// your constructor so that an instance will be created and injected automatically by the
/// dependency injection container. To create an instance with some dependent services replaced,
/// first resolve the object from the dependency injection container, then replace selected
/// services using the 'With...' methods. Do not call the constructor at any point in this process.
/// </para>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Scoped"/>. This means that each
/// <see cref="DbContext"/> instance will use its own instance of this service.
/// The implementation may depend on other services registered with any lifetime.
/// The implementation does not need to be thread-safe.
/// </para>
/// </summary>
public sealed class RelationalEvaluatableExpressionFilterDependencies
{
/// <summary>
/// <para>
/// Creates the service dependencies parameter object for a <see cref="RelationalEvaluatableExpressionFilter" />.
/// </para>
/// <para>
/// Do not call this constructor directly from either provider or application code as it may change
/// as new dependencies are added. Instead, use this type in your constructor so that an instance
/// will be created and injected automatically by the dependency injection container. To create
/// an instance with some dependent services replaced, first resolve the object from the dependency
/// injection container, then replace selected services using the 'With...' methods. Do not call
/// the constructor at any point in this process.
/// </para>
/// <para>
/// 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.
/// </para>
/// </summary>
[EntityFrameworkInternal]
public RelationalEvaluatableExpressionFilterDependencies([NotNull] IModel model)
{
Check.NotNull(model, nameof(model));

Model = model;
}

/// <summary>
/// The model used with this <see cref="RelationalEvaluatableExpressionFilter"/>.
/// </summary>
public IModel Model { get; }

/// <summary>
/// Clones this dependency parameter object with one service replaced.
/// </summary>
/// <param name="model"> A replacement for the current dependency of this type. </param>
/// <returns> A new parameter object with the given service replaced. </returns>
public RelationalEvaluatableExpressionFilterDependencies With([NotNull] IModel model)
=> new RelationalEvaluatableExpressionFilterDependencies(model);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roji Follow the same pattern as for all existing dependency objects and add validation test--see https://github.com/aspnet/EntityFrameworkCore/blob/master/test/EFCore.Tests/ModelSourceDependenciesTest.cs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added With method for Model and tests for both the core and relational dependencies (sorry, it's the first time I'm doing this).

Note that I added these because the API consistency tests told me too. I think there are various other cases of services without dependency objects (e.g. QueryContext accepts QueryContextDependencies, but RelationalQueryContext accepts additional parameters directly). We may want to do an extra pass for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roji Dependency objects are needed primarily when "outsiders" need to call constructors, possible from a derived class. This is expected anytime we have a public service implementation since, as you know from API review, we typically make these public when we expect people to inherit. So that's why there is a API consistency rule for those.

There are other places where we have added similar objects, but sometimes with a lighter-weight pattern (although maybe all our dependency objects could be read-only structs?). If there are places where you can see there will be problems evolving the API without dependency objects, then we should review those.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for the explanation.

It seems a bit odd for a core base class (e.g. QueryContext) to have dependencies but not for its subclass in relational - if no outsider is expected to extend, then we don't need the deps in core, and if they do, then we do need them on relational, no?

Apart from that I don't yet have a good-enough feel for which services are expected to be extended (again, QueryContext?). It's probably not a big deal in any case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roji Agreed that feels wrong. File an issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #16788

3 changes: 2 additions & 1 deletion src/EFCore/Infrastructure/EntityFrameworkServicesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ public virtual EntityFrameworkServicesBuilder TryAddCoreServices()
.AddDependencyScoped<CompiledQueryCacheKeyGeneratorDependencies>()
.AddDependencyScoped<QueryContextDependencies>()
.AddDependencyScoped<ValueGeneratorSelectorDependencies>()
.AddDependencyScoped<DatabaseDependencies>();
.AddDependencyScoped<DatabaseDependencies>()
.AddDependencyScoped<EvaluatableExpressionFilterDependencies>();

ServiceCollectionMap.TryAddSingleton<IRegisteredServices>(
new RegisteredServices(ServiceCollectionMap.ServiceCollection.Select(s => s.ServiceType)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Query.Internal
namespace Microsoft.EntityFrameworkCore.Query
{
/// <summary>
/// <para>
Expand All @@ -22,7 +25,7 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal
/// The implementation does not need to be thread-safe.
/// </para>
/// </summary>
public class EvaluatableExpressionFilter : EvaluatableExpressionFilterBase
public class EvaluatableExpressionFilter : IEvaluatableExpressionFilter
{
// This methods are non-deterministic and result varies based on time of running the query.
// Hence we don't evaluate them. See issue#2069
Expand Down Expand Up @@ -54,13 +57,36 @@ private static readonly MethodInfo _randomNextOneArg
private static readonly MethodInfo _randomNextTwoArgs
= typeof(Random).GetRuntimeMethod(nameof(Random.Next), new[] { typeof(int), typeof(int) });

/// <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 EvaluatableExpressionFilter(
[NotNull] EvaluatableExpressionFilterDependencies dependencies)
{
Check.NotNull(dependencies, nameof(dependencies));

Dependencies = dependencies;
}

/// <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 override bool IsEvaluatableExpression(Expression expression)
public virtual bool IsEvaluatableExpression(Expression expression)
{
switch (expression)
{
Expand Down Expand Up @@ -91,7 +117,7 @@ public override bool IsEvaluatableExpression(Expression expression)
break;
}

return base.IsEvaluatableExpression(expression);
return true;
}
}
}
58 changes: 58 additions & 0 deletions src/EFCore/Query/EvaluatableExpressionFilterDependencies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Query
{
/// <summary>
/// <para>
/// Service dependencies parameter class for <see cref="EvaluatableExpressionFilter" />
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// <para>
/// Do not construct instances of this class directly from either provider or application code as the
/// constructor signature may change as new dependencies are added. Instead, use this type in
/// your constructor so that an instance will be created and injected automatically by the
/// dependency injection container. To create an instance with some dependent services replaced,
/// first resolve the object from the dependency injection container, then replace selected
/// services using the 'With...' methods. Do not call the constructor at any point in this process.
/// </para>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Scoped"/>. This means that each
/// <see cref="DbContext"/> instance will use its own instance of this service.
/// The implementation may depend on other services registered with any lifetime.
/// The implementation does not need to be thread-safe.
/// </para>
/// </summary>
public sealed class EvaluatableExpressionFilterDependencies
{
/// <summary>
/// <para>
/// Creates the service dependencies parameter object for a <see cref="EvaluatableExpressionFilter" />.
/// </para>
/// <para>
/// Do not call this constructor directly from either provider or application code as it may change
/// as new dependencies are added. Instead, use this type in your constructor so that an instance
/// will be created and injected automatically by the dependency injection container. To create
/// an instance with some dependent services replaced, first resolve the object from the dependency
/// injection container, then replace selected services using the 'With...' methods. Do not call
/// the constructor at any point in this process.
/// </para>
/// <para>
/// 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.
/// </para>
/// </summary>
[EntityFrameworkInternal]
public EvaluatableExpressionFilterDependencies()
{
}
}
}
33 changes: 0 additions & 33 deletions src/EFCore/Query/Internal/EvaluatableExpressionFilterBase.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Query
{
public class RelationalEvaluatableExpressionFilterDependenciesTest
{
[ConditionalFact]
public void Can_use_With_methods_to_clone_and_replace_service()
{
RelationalTestHelpers.Instance.TestDependenciesClone<RelationalEvaluatableExpressionFilterDependencies>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Query
{
public class EvaluatableExpressionFilterDependenciesTest
{
[ConditionalFact]
public void Can_use_With_methods_to_clone_and_replace_service()
{
InMemoryTestHelpers.Instance.TestDependenciesClone<EvaluatableExpressionFilterDependencies>();
}
}
}