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

Move IIndex, INavigation and related extension methods to the interfaces #24192

Merged
merged 3 commits into from
Feb 19, 2021
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
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/CollectionEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ protected virtual InternalEntityEntry GetInternalTargetEntry([NotNull] object en
: InternalEntry.StateManager.GetOrCreateEntry(entity, Metadata.TargetEntityType);

private ICollectionLoader TargetLoader
=> _loader ??= Metadata is ISkipNavigation skipNavigation
=> _loader ??= Metadata is IRuntimeSkipNavigation skipNavigation
? skipNavigation.GetManyToManyLoader()
: new EntityFinderCollectionLoaderAdapter(
InternalEntry.StateManager.CreateEntityFinder(Metadata.TargetEntityType),
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/Internal/IdentityMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public virtual IDependentsMap GetDependentsMap(IForeignKey foreignKey)

if (!_dependentMaps.TryGetValue(foreignKey, out var map))
{
map = foreignKey.CreateDependentsMapFactory();
map = ((IRuntimeForeignKey)foreignKey).DependentsMapFactory();

foreach (var value in _identityMap.Values)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static CompositePrincipalKeyValueFactory CreateCompositeFactory(IKey key
object dependentKeyValueFactory,
Func<IDependentsMap> dependentsMapFactory)
{
var concreteForeignKey = foreignKey.AsForeignKey();
var concreteForeignKey = (IRuntimeForeignKey)foreignKey;

concreteForeignKey.DependentKeyValueFactory = dependentKeyValueFactory;
concreteForeignKey.DependentsMapFactory = dependentsMapFactory;
Expand Down
6 changes: 3 additions & 3 deletions src/EFCore/ChangeTracking/Internal/StateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ private IIdentityMap GetOrCreateIdentityMap(IKey key)
{
if (_identityMap0 == null)
{
_identityMap0 = key.GetIdentityMapFactory()(SensitiveLoggingEnabled);
_identityMap0 = ((IRuntimeKey)key).GetIdentityMapFactory()(SensitiveLoggingEnabled);
return _identityMap0;
}

Expand All @@ -444,7 +444,7 @@ private IIdentityMap GetOrCreateIdentityMap(IKey key)

if (_identityMap1 == null)
{
_identityMap1 = key.GetIdentityMapFactory()(SensitiveLoggingEnabled);
_identityMap1 = ((IRuntimeKey)key).GetIdentityMapFactory()(SensitiveLoggingEnabled);
return _identityMap1;
}

Expand All @@ -460,7 +460,7 @@ private IIdentityMap GetOrCreateIdentityMap(IKey key)

if (!_identityMaps.TryGetValue(key, out var identityMap))
{
identityMap = key.GetIdentityMapFactory()(SensitiveLoggingEnabled);
identityMap = ((IRuntimeKey)key).GetIdentityMapFactory()(SensitiveLoggingEnabled);
_identityMaps[key] = identityMap;
}

Expand Down
26 changes: 2 additions & 24 deletions src/EFCore/Extensions/ConventionForeignKeyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +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 JetBrains.Annotations;
using System;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable enable
Expand All @@ -12,30 +12,8 @@ namespace Microsoft.EntityFrameworkCore
/// <summary>
/// Extension methods for <see cref="IConventionForeignKey" />.
/// </summary>
[Obsolete("Use IConventionForeignKey")]
public static class ConventionForeignKeyExtensions
{
/// <summary>
/// Gets the entity type related to the given one.
/// </summary>
/// <param name="foreignKey"> The foreign key. </param>
/// <param name="entityType"> One of the entity types related by the foreign key. </param>
/// <returns> The entity type related to the given one. </returns>
public static IConventionEntityType GetRelatedEntityType(
[NotNull] this IConventionForeignKey foreignKey,
[NotNull] IConventionEntityType entityType)
=> (IConventionEntityType)((IReadOnlyForeignKey)foreignKey).GetRelatedEntityType(entityType);

/// <summary>
/// Returns a navigation associated with this foreign key.
/// </summary>
/// <param name="foreignKey"> The foreign key. </param>
/// <param name="pointsToPrincipal">
/// A value indicating whether the navigation is on the dependent type pointing to the principal type.
/// </param>
/// <returns>
/// A navigation associated with this foreign key or <see langword="null" />.
/// </returns>
public static IConventionNavigation? GetNavigation([NotNull] this IConventionForeignKey foreignKey, bool pointsToPrincipal)
=> pointsToPrincipal ? foreignKey.DependentToPrincipal : foreignKey.PrincipalToDependent;
}
}
12 changes: 2 additions & 10 deletions src/EFCore/Extensions/ConventionKeyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +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.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using System;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable enable
Expand All @@ -14,14 +12,8 @@ namespace Microsoft.EntityFrameworkCore
/// <summary>
/// Extension methods for <see cref="IConventionKey" />.
/// </summary>
[Obsolete("Use IConventionForeignKey")]
public static class ConventionKeyExtensions
{
/// <summary>
/// Gets all foreign keys that target a given primary or alternate key.
/// </summary>
/// <param name="key"> The key to find the foreign keys for. </param>
/// <returns> The foreign keys that reference the given key. </returns>
public static IEnumerable<IConventionForeignKey> GetReferencingForeignKeys([NotNull] this IConventionKey key)
=> ((IReadOnlyKey)key).GetReferencingForeignKeys().Cast<IConventionForeignKey>();
}
}
1 change: 1 addition & 0 deletions src/EFCore/Extensions/ConventionNavigationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.EntityFrameworkCore
/// <summary>
/// Extension methods for <see cref="IConventionNavigation" />.
/// </summary>
[Obsolete("Use IConventionNavigation")]
public static class ConventionNavigationExtensions
{
/// <summary>
Expand Down
146 changes: 1 addition & 145 deletions src/EFCore/Extensions/ForeignKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
// 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.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

#nullable enable

Expand All @@ -19,145 +12,8 @@ namespace Microsoft.EntityFrameworkCore
/// <summary>
/// Extension methods for <see cref="IReadOnlyForeignKey" />.
/// </summary>
[Obsolete("Use IReadOnlyForeignKey")]
public static class ForeignKeyExtensions
{
/// <summary>
/// <para>
/// Creates a factory for key values based on the foreign key values taken
/// from various forms of entity data.
/// </para>
/// <para>
/// This method is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// </summary>
/// <param name="foreignKey"> The <see cref="IReadOnlyForeignKey" /> for which a factory is needed. </param>
/// <typeparam name="TKey"> The type of key instanceas. </typeparam>
/// <returns> A new factory. </returns>
public static IDependentKeyValueFactory<TKey>? GetDependentKeyValueFactory<TKey>(
[NotNull] this IReadOnlyForeignKey foreignKey)
=> (IDependentKeyValueFactory<TKey>?)foreignKey.AsForeignKey().DependentKeyValueFactory;

/// <summary>
/// Gets the entity type related to the given one.
/// </summary>
/// <param name="foreignKey"> The foreign key. </param>
/// <param name="entityType"> One of the entity types related by the foreign key. </param>
/// <returns> The entity type related to the given one. </returns>
public static IReadOnlyEntityType GetRelatedEntityType(
[NotNull] this IReadOnlyForeignKey foreignKey, [NotNull] IReadOnlyEntityType entityType)
{
if (foreignKey.DeclaringEntityType != entityType
&& foreignKey.PrincipalEntityType != entityType)
{
throw new InvalidOperationException(
CoreStrings.EntityTypeNotInRelationshipStrict(
entityType.DisplayName(),
foreignKey.DeclaringEntityType.DisplayName(),
foreignKey.PrincipalEntityType.DisplayName()));
}

return foreignKey.DeclaringEntityType == entityType
? foreignKey.PrincipalEntityType
: foreignKey.DeclaringEntityType;
}

/// <summary>
/// Returns a navigation associated with this foreign key.
/// </summary>
/// <param name="foreignKey"> The foreign key. </param>
/// <param name="pointsToPrincipal">
/// A value indicating whether the navigation is on the dependent type pointing to the principal type.
/// </param>
/// <returns>
/// A navigation associated with this foreign key or <see langword="null" />.
/// </returns>
public static IReadOnlyNavigation? GetNavigation([NotNull] this IReadOnlyForeignKey foreignKey, bool pointsToPrincipal)
=> pointsToPrincipal ? foreignKey.DependentToPrincipal : foreignKey.PrincipalToDependent;

/// <summary>
/// Returns a value indicating whether the foreign key is defined on the primary key and pointing to the same primary key.
/// </summary>
/// <param name="foreignKey"> The foreign key. </param>
/// <returns> A value indicating whether the foreign key is defined on the primary key and pointing to the same primary key. </returns>
public static bool IsBaseLinking([NotNull] this IReadOnlyForeignKey foreignKey)
{
var primaryKey = foreignKey.DeclaringEntityType.FindPrimaryKey();
return primaryKey == foreignKey.PrincipalKey
&& foreignKey.Properties.SequenceEqual(primaryKey.Properties);
}

/// <summary>
/// <para>
/// Creates a human-readable representation of the given metadata.
/// </para>
/// <para>
/// Warning: Do not rely on the format of the returned string.
/// It is designed for debugging only and may change arbitrarily between releases.
/// </para>
/// </summary>
/// <param name="foreignKey"> The metadata item. </param>
/// <param name="options"> Options for generating the string. </param>
/// <param name="indent"> The number of indent spaces to use before each new line. </param>
/// <returns> A human-readable representation. </returns>
public static string ToDebugString(
[NotNull] this IReadOnlyForeignKey foreignKey,
MetadataDebugStringOptions options,
int indent = 0)
{
var builder = new StringBuilder();
var indentString = new string(' ', indent);

builder.Append(indentString);

var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0;
if (singleLine)
{
builder.Append("ForeignKey: ");
}

builder
.Append(foreignKey.DeclaringEntityType.DisplayName())
.Append(" ")
.Append(foreignKey.Properties.Format())
.Append(" -> ")
.Append(foreignKey.PrincipalEntityType.DisplayName())
.Append(" ")
.Append(foreignKey.PrincipalKey.Properties.Format());

if (foreignKey.IsUnique)
{
builder.Append(" Unique");
}

if (foreignKey.IsOwnership)
{
builder.Append(" Ownership");
}

if (foreignKey.PrincipalToDependent != null)
{
builder.Append(" ToDependent: ").Append(foreignKey.PrincipalToDependent.Name);
}

if (foreignKey.DependentToPrincipal != null)
{
builder.Append(" ToPrincipal: ").Append(foreignKey.DependentToPrincipal.Name);
}

if (foreignKey.DeleteBehavior != DeleteBehavior.NoAction)
{
builder
.Append(" ")
.Append(foreignKey.DeleteBehavior);
}

if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0)
{
builder.Append(foreignKey.AnnotationsToDebugString(indent + 2));
}

return builder.ToString();
}
}
}
90 changes: 0 additions & 90 deletions src/EFCore/Extensions/IndexExtensions.cs

This file was deleted.

Loading