Skip to content

Commit

Permalink
Remove quirks from main (#31976)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers authored Oct 6, 2023
1 parent e2a801b commit efd79b2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions;
public sealed partial class SelectExpression : TableExpressionBase
{
private const string DiscriminatorColumnAlias = "Discriminator";
private static readonly bool UseOldBehavior31107 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31107", out var enabled31107) && enabled31107;

private static readonly IdentifierComparer IdentifierComparerInstance = new();

private readonly List<ProjectionExpression> _projection = new();
Expand Down Expand Up @@ -2976,7 +2973,7 @@ static IReadOnlyDictionary<IProperty, ColumnExpression> GetPropertyExpressions(
|| (derivedType && ownerType.GetMappingStrategy() != RelationalAnnotationNames.TphMappingStrategy);
var newColumnsNullable = pkColumnsNullable
|| !navigation.ForeignKey.IsRequiredDependent
|| (derivedType && !UseOldBehavior31107);
|| derivedType;
if (derivedTpt)
{
principalMappings = principalMappings.Except(ownerType.BaseType!.GetViewOrTableMappings().Select(e => e.Table));
Expand Down
17 changes: 6 additions & 11 deletions src/EFCore/Metadata/RuntimeEntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query.Internal;

namespace Microsoft.EntityFrameworkCore.Metadata;

Expand Down Expand Up @@ -845,11 +844,9 @@ static IEnumerable<RuntimePropertyBase> Create(RuntimeEntityType type)
/// </summary>
[EntityFrameworkInternal]
public virtual Func<MaterializationContext, object> GetOrCreateMaterializer(IEntityMaterializerSource source)
=> EntityMaterializerSource.UseOldBehavior31866
? source.GetMaterializer(this)
: NonCapturingLazyInitializer.EnsureInitialized(
ref _materializer, this, source,
static (e, s) => s.GetMaterializer(e));
=> NonCapturingLazyInitializer.EnsureInitialized(
ref _materializer, this, source,
static (e, s) => s.GetMaterializer(e));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -859,11 +856,9 @@ public virtual Func<MaterializationContext, object> GetOrCreateMaterializer(IEnt
/// </summary>
[EntityFrameworkInternal]
public virtual Func<MaterializationContext, object> GetOrCreateEmptyMaterializer(IEntityMaterializerSource source)
=> EntityMaterializerSource.UseOldBehavior31866
? source.GetEmptyMaterializer(this)
: NonCapturingLazyInitializer.EnsureInitialized(
ref _emptyMaterializer, this, source,
static (e, s) => s.GetEmptyMaterializer(e));
=> NonCapturingLazyInitializer.EnsureInitialized(
ref _emptyMaterializer, this, source,
static (e, s) => s.GetEmptyMaterializer(e));

/// <summary>
/// Returns a string that represents the current object.
Expand Down
119 changes: 48 additions & 71 deletions src/EFCore/Query/Internal/EntityMaterializerSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal;
/// </summary>
public class EntityMaterializerSource : IEntityMaterializerSource
{
/// <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 static readonly bool UseOldBehavior31866 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31866", out var enabled31866) && enabled31866;

private static readonly MethodInfo InjectableServiceInjectedMethod
= typeof(IInjectableService).GetMethod(nameof(IInjectableService.Injected))!;

Expand Down Expand Up @@ -486,21 +477,14 @@ private ConcurrentDictionary<IEntityType, Func<MaterializationContext, object>>
public virtual Func<MaterializationContext, object> GetMaterializer(
IEntityType entityType)
{
return UseOldBehavior31866
? Materializers.GetOrAdd(entityType, static (e, s) => CreateMaterializer(s, e), this)
: CreateMaterializer(this, entityType);

static Func<MaterializationContext, object> CreateMaterializer(EntityMaterializerSource self, IEntityType e)
{
var materializationContextParameter
= Expression.Parameter(typeof(MaterializationContext), "materializationContext");

return Expression.Lambda<Func<MaterializationContext, object>>(
((IEntityMaterializerSource)self).CreateMaterializeExpression(
new EntityMaterializerSourceParameters(e, "instance", null), materializationContextParameter),
materializationContextParameter)
.Compile();
}
var materializationContextParameter
= Expression.Parameter(typeof(MaterializationContext), "materializationContext");

return Expression.Lambda<Func<MaterializationContext, object>>(
((IEntityMaterializerSource)this).CreateMaterializeExpression(
new EntityMaterializerSourceParameters(entityType, "instance", null), materializationContextParameter),
materializationContextParameter)
.Compile();
}

private ConcurrentDictionary<IEntityType, Func<MaterializationContext, object>> EmptyMaterializers
Expand All @@ -516,61 +500,54 @@ private ConcurrentDictionary<IEntityType, Func<MaterializationContext, object>>
/// </summary>
public virtual Func<MaterializationContext, object> GetEmptyMaterializer(IEntityType entityType)
{
return UseOldBehavior31866
? EmptyMaterializers.GetOrAdd(entityType, static (e, s) => CreateEmptyMaterializer(s, e), this)
: CreateEmptyMaterializer(this, entityType);

static Func<MaterializationContext, object> CreateEmptyMaterializer(EntityMaterializerSource self, IEntityType e)
var binding = entityType.ServiceOnlyConstructorBinding;
if (binding == null)
{
var binding = e.ServiceOnlyConstructorBinding;
if (binding == null)
{
var _ = e.ConstructorBinding;
binding = e.ServiceOnlyConstructorBinding;
if (binding == null)
{
throw new InvalidOperationException(CoreStrings.NoParameterlessConstructor(e.DisplayName()));
}
}
var _ = entityType.ConstructorBinding;
binding = entityType.ServiceOnlyConstructorBinding;
if (binding == null)
{
throw new InvalidOperationException(CoreStrings.NoParameterlessConstructor(entityType.DisplayName()));
}
}

binding = self.ModifyBindings(e, binding);
binding = ModifyBindings(entityType, binding);

var materializationContextExpression = Expression.Parameter(typeof(MaterializationContext), "mc");
var bindingInfo = new ParameterBindingInfo(
new EntityMaterializerSourceParameters(e, "instance", null), materializationContextExpression);
var materializationContextExpression = Expression.Parameter(typeof(MaterializationContext), "mc");
var bindingInfo = new ParameterBindingInfo(
new EntityMaterializerSourceParameters(entityType, "instance", null), materializationContextExpression);

var blockExpressions = new List<Expression>();
var instanceVariable = Expression.Variable(binding.RuntimeType, "instance");
var serviceProperties = e.GetServiceProperties().ToList();
bindingInfo.ServiceInstances.Add(instanceVariable);
var blockExpressions = new List<Expression>();
var instanceVariable = Expression.Variable(binding.RuntimeType, "instance");
var serviceProperties = entityType.GetServiceProperties().ToList();
bindingInfo.ServiceInstances.Add(instanceVariable);

CreateServiceInstances(binding, bindingInfo, blockExpressions, serviceProperties);
CreateServiceInstances(binding, bindingInfo, blockExpressions, serviceProperties);

var constructorExpression = binding.CreateConstructorExpression(bindingInfo);
var constructorExpression = binding.CreateConstructorExpression(bindingInfo);

var properties = new HashSet<IPropertyBase>(serviceProperties);
foreach (var consumedProperty in binding.ParameterBindings.SelectMany(p => p.ConsumedProperties))
{
properties.Remove(consumedProperty);
}

return Expression.Lambda<Func<MaterializationContext, object>>(
self._materializationInterceptor == null
? properties.Count == 0 && blockExpressions.Count == 0
? constructorExpression
: self.CreateMaterializeExpression(
blockExpressions, instanceVariable, constructorExpression, properties, bindingInfo)
: self.CreateInterceptionMaterializeExpression(
e,
new HashSet<IPropertyBase>(),
self._materializationInterceptor,
bindingInfo,
constructorExpression,
instanceVariable,
blockExpressions),
materializationContextExpression)
.Compile();
var properties = new HashSet<IPropertyBase>(serviceProperties);
foreach (var consumedProperty in binding.ParameterBindings.SelectMany(p => p.ConsumedProperties))
{
properties.Remove(consumedProperty);
}

return Expression.Lambda<Func<MaterializationContext, object>>(
_materializationInterceptor == null
? properties.Count == 0 && blockExpressions.Count == 0
? constructorExpression
: CreateMaterializeExpression(
blockExpressions, instanceVariable, constructorExpression, properties, bindingInfo)
: CreateInterceptionMaterializeExpression(
entityType,
new HashSet<IPropertyBase>(),
_materializationInterceptor,
bindingInfo,
constructorExpression,
instanceVariable,
blockExpressions),
materializationContextExpression)
.Compile();
}

private InstantiationBinding ModifyBindings(ITypeBase structuralType, InstantiationBinding binding)
Expand Down
34 changes: 5 additions & 29 deletions src/EFCore/ValueGeneration/ValueGeneratorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ namespace Microsoft.EntityFrameworkCore.ValueGeneration;
/// </remarks>
public class ValueGeneratorCache : IValueGeneratorCache
{
private static readonly bool _useOldBehavior31539 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31539", out var enabled31539) && enabled31539;

/// <summary>
/// Initializes a new instance of the <see cref="ValueGeneratorCache" /> class.
/// </summary>
Expand All @@ -54,42 +51,21 @@ public ValueGeneratorCache(ValueGeneratorCacheDependencies dependencies)

public CacheKey(IProperty property, ITypeBase typeBase)
{
if (_useOldBehavior31539)
{
_modelId = default;
_property = null;
_typeBase = null;
Property = property;
TypeBase = typeBase;
}
else
{
_modelId = typeBase.Model.ModelId;
_property = property.Name;
_typeBase = typeBase.Name;
Property = null;
TypeBase = null;
}
_modelId = typeBase.Model.ModelId;
_property = property.Name;
_typeBase = typeBase.Name;
}

public IProperty? Property { get; }

public ITypeBase? TypeBase { get; }

public bool Equals(CacheKey other)
=> _useOldBehavior31539
? Property!.Equals(other.Property) && TypeBase!.Equals(other.TypeBase)
: (_property!.Equals(other._property, StringComparison.Ordinal)
=> (_property!.Equals(other._property, StringComparison.Ordinal)
&& _typeBase!.Equals(other._typeBase, StringComparison.Ordinal)
&& _modelId.Equals(other._modelId));

public override bool Equals(object? obj)
=> obj is CacheKey cacheKey && Equals(cacheKey);

public override int GetHashCode()
=> _useOldBehavior31539
? HashCode.Combine(Property!, TypeBase!)
: HashCode.Combine(_property!, _typeBase!, _modelId);
=> HashCode.Combine(_property!, _typeBase!, _modelId);
}

/// <summary>
Expand Down

0 comments on commit efd79b2

Please sign in to comment.