Skip to content

Commit

Permalink
Query: Don't expose ICurrentDbContext on QCCD (#18973)
Browse files Browse the repository at this point in the history
Resolves #17845
  • Loading branch information
smitpatel committed Nov 19, 2019
1 parent cb6bb76 commit 9b6d5c1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
6 changes: 2 additions & 4 deletions src/EFCore/Query/QueryCompilationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ public QueryCompilationContext(
QueryCompilationContextDependencies dependencies,
bool async)
{
var context = dependencies.CurrentContext.Context;

IsAsync = async;
IsTracking = context.ChangeTracker.QueryTrackingBehavior == QueryTrackingBehavior.TrackAll;
IsTracking = dependencies.IsTracking;
IsBuffering = dependencies.IsRetryingExecutionStrategy;
Model = dependencies.Model;
ContextOptions = dependencies.ContextOptions;
ContextType = context.GetType();
ContextType = dependencies.ContextType;
Logger = dependencies.Logger;

_queryTranslationPreprocessorFactory = dependencies.QueryTranslationPreprocessorFactory;
Expand Down
29 changes: 18 additions & 11 deletions src/EFCore/Query/QueryCompilationContextDependencies.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -37,6 +38,7 @@ namespace Microsoft.EntityFrameworkCore.Query
public sealed class QueryCompilationContextDependencies
{
private readonly IExecutionStrategyFactory _executionStrategyFactory;
private readonly ICurrentDbContext _currentContext;

/// <summary>
/// <para>
Expand Down Expand Up @@ -79,7 +81,7 @@ public QueryCompilationContextDependencies(
Check.NotNull(contextOptions, nameof(contextOptions));
Check.NotNull(logger, nameof(logger));

CurrentContext = currentContext;
_currentContext = currentContext;
Model = model;
QueryTranslationPreprocessorFactory = queryTranslationPreprocessorFactory;
QueryableMethodTranslatingExpressionVisitorFactory = queryableMethodTranslatingExpressionVisitorFactory;
Expand All @@ -92,9 +94,14 @@ public QueryCompilationContextDependencies(
}

/// <summary>
/// The cache being used to store value generator instances.
/// The CLR type of DbContext.
/// </summary>
public ICurrentDbContext CurrentContext { get; }
public Type ContextType => _currentContext.Context.GetType();

/// <summary>
/// The flag indicating if default query tracking behavior is tracking.
/// </summary>
public bool IsTracking => _currentContext.Context.ChangeTracker.QueryTrackingBehavior == QueryTrackingBehavior.TrackAll;

/// <summary>
/// The model.
Expand Down Expand Up @@ -149,7 +156,7 @@ public QueryCompilationContextDependencies With([NotNull] IModel model)
QueryTranslationPostprocessorFactory,
ShapedQueryCompilingExpressionVisitorFactory,
_executionStrategyFactory,
CurrentContext,
_currentContext,
ContextOptions,
Logger);

Expand All @@ -166,7 +173,7 @@ public QueryCompilationContextDependencies With([NotNull] IQueryTranslationPrepr
QueryTranslationPostprocessorFactory,
ShapedQueryCompilingExpressionVisitorFactory,
_executionStrategyFactory,
CurrentContext,
_currentContext,
ContextOptions,
Logger);

Expand All @@ -184,7 +191,7 @@ public QueryCompilationContextDependencies With(
QueryTranslationPostprocessorFactory,
ShapedQueryCompilingExpressionVisitorFactory,
_executionStrategyFactory,
CurrentContext,
_currentContext,
ContextOptions,
Logger);

Expand All @@ -202,7 +209,7 @@ public QueryCompilationContextDependencies With(
queryTranslationPostprocessorFactory,
ShapedQueryCompilingExpressionVisitorFactory,
_executionStrategyFactory,
CurrentContext,
_currentContext,
ContextOptions,
Logger);

Expand All @@ -220,7 +227,7 @@ public QueryCompilationContextDependencies With(
QueryTranslationPostprocessorFactory,
shapedQueryCompilingExpressionVisitorFactory,
_executionStrategyFactory,
CurrentContext,
_currentContext,
ContextOptions,
Logger);

Expand All @@ -237,7 +244,7 @@ public QueryCompilationContextDependencies With([NotNull] IExecutionStrategyFact
QueryTranslationPostprocessorFactory,
ShapedQueryCompilingExpressionVisitorFactory,
executionStrategyFactory,
CurrentContext,
_currentContext,
ContextOptions,
Logger);

Expand Down Expand Up @@ -271,7 +278,7 @@ public QueryCompilationContextDependencies With([NotNull] IDbContextOptions cont
QueryTranslationPostprocessorFactory,
ShapedQueryCompilingExpressionVisitorFactory,
_executionStrategyFactory,
CurrentContext,
_currentContext,
contextOptions,
Logger);

Expand All @@ -288,7 +295,7 @@ public QueryCompilationContextDependencies With([NotNull] IDiagnosticsLogger<DbL
QueryTranslationPostprocessorFactory,
ShapedQueryCompilingExpressionVisitorFactory,
_executionStrategyFactory,
CurrentContext,
_currentContext,
ContextOptions,
logger);
}
Expand Down
4 changes: 1 addition & 3 deletions test/EFCore.Specification.Tests/TestUtilities/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ public void TestDependenciesClone<TDependencies>(params string[] ignorePropertie
.First();
var constructorParameters = constructor.GetParameters().Where(p => !obsoleteTypes.Contains(p.ParameterType)).ToList();

Assert.Equal(constructorParameters.Count, serviceProperties.Count);

foreach (var serviceType in constructorParameters.Where(p => !ignoreProperties.Contains(p.Name)).Select(p => p.ParameterType))
foreach (var serviceType in constructorParameters.Select(p => p.ParameterType))
{
var withMethod = typeof(TDependencies).GetTypeInfo().DeclaredMethods
.Single(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class QueryCompilationContextDependenciesTest
[ConditionalFact]
public void Can_use_With_methods_to_clone_and_replace_service()
{
InMemoryTestHelpers.Instance.TestDependenciesClone<QueryCompilationContextDependencies>();
InMemoryTestHelpers.Instance.TestDependenciesClone<QueryCompilationContextDependencies>(
nameof(QueryCompilationContextDependencies.IsTracking),
nameof(QueryCompilationContextDependencies.ContextType));
}
}
}

0 comments on commit 9b6d5c1

Please sign in to comment.