Skip to content

Commit

Permalink
Create a read-optimized implementation of IModel
Browse files Browse the repository at this point in the history
Part of #8258
  • Loading branch information
AndriySvyryd authored Mar 11, 2021
1 parent fd171db commit f1a0d8c
Show file tree
Hide file tree
Showing 24 changed files with 3,570 additions and 496 deletions.
429 changes: 4 additions & 425 deletions src/EFCore/Infrastructure/Annotatable.cs

Large diffs are not rendered by default.

484 changes: 484 additions & 0 deletions src/EFCore/Infrastructure/AnnotatableBase.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/EFCore/Metadata/IMutableAnnotatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface IMutableAnnotatable : IReadOnlyAnnotatable
/// </summary>
/// <param name="annotations"> The annotations to be added. </param>
void AddAnnotations([NotNull] IEnumerable<IAnnotation> annotations)
=> Annotatable.AddAnnotations(this, annotations);
=> AnnotatableBase.AddAnnotations((AnnotatableBase)this, annotations);

/// <summary>
/// Sets the annotation stored under the given name. Overwrites the existing annotation if an
Expand Down
10 changes: 5 additions & 5 deletions src/EFCore/Metadata/Internal/EntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public static PropertyCounts GetCounts([NotNull] this IEntityType entityType)
/// 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 PropertyCounts CalculateCounts([NotNull] this EntityType entityType)
public static PropertyCounts CalculateCounts([NotNull] this IRuntimeEntityType entityType)
{
var index = 0;
var navigationIndex = 0;
Expand Down Expand Up @@ -243,13 +243,13 @@ public static PropertyCounts CalculateCounts([NotNull] this EntityType entityTyp
relationshipIndex: property.IsKey() || property.IsForeignKey() ? relationshipIndex++ : -1,
storeGenerationIndex: property.MayBeStoreGenerated() ? storeGenerationIndex++ : -1);

property.PropertyIndexes = indexes;
((IRuntimePropertyBase)property).PropertyIndexes = indexes;
}

var isNotifying = entityType.GetChangeTrackingStrategy() != ChangeTrackingStrategy.Snapshot;

foreach (var navigation in entityType.GetDeclaredNavigations()
.Union<PropertyBase>(entityType.GetDeclaredSkipNavigations()))
.Union<IPropertyBase>(entityType.GetDeclaredSkipNavigations()))
{
var indexes = new PropertyIndexes(
index: navigationIndex++,
Expand All @@ -258,7 +258,7 @@ public static PropertyCounts CalculateCounts([NotNull] this EntityType entityTyp
relationshipIndex: ((IReadOnlyNavigationBase)navigation).IsCollection && isNotifying ? -1 : relationshipIndex++,
storeGenerationIndex: -1);

navigation.PropertyIndexes = indexes;
((IRuntimePropertyBase)navigation).PropertyIndexes = indexes;
}

foreach (var serviceProperty in entityType.GetDeclaredServiceProperties())
Expand All @@ -270,7 +270,7 @@ public static PropertyCounts CalculateCounts([NotNull] this EntityType entityTyp
relationshipIndex: -1,
storeGenerationIndex: -1);

serviceProperty.PropertyIndexes = indexes;
((IRuntimePropertyBase)serviceProperty).PropertyIndexes = indexes;
}

return new PropertyCounts(
Expand Down
10 changes: 8 additions & 2 deletions src/EFCore/Metadata/Internal/ForeignKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,14 @@ public virtual DeleteBehavior DeleteBehavior
return DeleteBehavior;
}

private static DeleteBehavior DefaultDeleteBehavior
=> DeleteBehavior.ClientSetNull;
/// <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 const DeleteBehavior DefaultDeleteBehavior
= DeleteBehavior.ClientSetNull;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Metadata/Internal/IRuntimeEntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,13 @@ public interface IRuntimeEntityType : IEntityType
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
Func<MaterializationContext, object> InstanceFactory { 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
/// 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>
InstantiationBinding? ServiceOnlyConstructorBinding { get; }
}
}
4 changes: 3 additions & 1 deletion src/EFCore/Metadata/Internal/IRuntimePropertyBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Metadata.Internal
Expand Down Expand Up @@ -43,7 +45,7 @@ public interface IRuntimePropertyBase : IPropertyBase
/// 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>
PropertyIndexes PropertyIndexes { get; }
PropertyIndexes PropertyIndexes { get; [param: NotNull] set; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
41 changes: 0 additions & 41 deletions src/EFCore/Metadata/Internal/MetadataExtensions.cs

This file was deleted.

9 changes: 8 additions & 1 deletion src/EFCore/Metadata/Internal/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -743,6 +742,14 @@ public virtual PropertyAccessMode GetPropertyAccessMode()
=> (PropertyAccessMode?)this[CoreAnnotationNames.PropertyAccessMode]
?? PropertyAccessMode.PreferField;

/// <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 const PropertyAccessMode DefaultPropertyAccessMode = PropertyAccessMode.PreferField;

/// <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
17 changes: 1 addition & 16 deletions src/EFCore/Metadata/Internal/PropertyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ public static bool IsCompatible(
/// 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>
[CA.AllowNull]
public virtual PropertyIndexes PropertyIndexes
{
get
Expand All @@ -324,21 +323,7 @@ public virtual PropertyIndexes PropertyIndexes
var _ = (property.DeclaringType as EntityType)?.Counts;
});

[param: CanBeNull]
set
{
if (value == null)
{
EnsureMutable();
// This path should only kick in when the model is still mutable and therefore access does not need
// to be thread-safe.
_indexes = null;
}
else
{
NonCapturingLazyInitializer.EnsureInitialized(ref _indexes, value);
}
}
set => NonCapturingLazyInitializer.EnsureInitialized(ref _indexes, value);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/Metadata/Internal/PropertyNameComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal
/// </summary>
public sealed class PropertyNameComparer : IComparer<string>
{
private readonly EntityType _entityType;
private readonly IReadOnlyEntityType _entityType;

/// <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 PropertyNameComparer([NotNull] EntityType entityType)
public PropertyNameComparer([NotNull] IReadOnlyEntityType entityType)
{
_entityType = entityType;
}
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/Metadata/Internal/SkipNavigationComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal
/// 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 sealed class SkipNavigationComparer : IComparer<SkipNavigation>
public sealed class SkipNavigationComparer : IComparer<IReadOnlySkipNavigation>
{
private SkipNavigationComparer()
{
Expand All @@ -34,7 +34,7 @@ private SkipNavigationComparer()
/// 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 int Compare(SkipNavigation? x, SkipNavigation? y)
public int Compare(IReadOnlySkipNavigation? x, IReadOnlySkipNavigation? y)
=> (x, y) switch
{
(not null, null) => 1,
Expand Down
Loading

0 comments on commit f1a0d8c

Please sign in to comment.