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 Graph and Multigraph fully internal #20533

Merged
merged 1 commit into from
Apr 7, 2020
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
71 changes: 0 additions & 71 deletions src/EFCore/Internal/Graph.cs

This file was deleted.

29 changes: 27 additions & 2 deletions src/EFCore/Metadata/Conventions/ModelCleanupConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
Expand All @@ -30,7 +31,8 @@ public ModelCleanupConvention([NotNull] ProviderConventionSetBuilderDependencies
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; }

/// <inheritdoc />
public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
public virtual void ProcessModelFinalizing(
IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
{
RemoveEntityTypesUnreachableByNavigations(modelBuilder, context);
RemoveNavigationlessForeignKeys(modelBuilder);
Expand All @@ -44,7 +46,7 @@ private void RemoveEntityTypesUnreachableByNavigations(
var rootEntityTypes = GetRoots(model, ConfigurationSource.DataAnnotation);
using (context.DelayConventions())
{
foreach (var orphan in new ModelNavigationsGraphAdapter(model).GetUnreachableVertices(rootEntityTypes))
foreach (var orphan in new GraphAdapter(model).GetUnreachableVertices(rootEntityTypes))
{
modelBuilder.HasNoEntityType(orphan, fromDataAnnotation: true);
}
Expand Down Expand Up @@ -90,5 +92,28 @@ private void RemoveModelBuildingAnnotations(IConventionModelBuilder modelBuilder
entityType.RemoveAnnotation(CoreAnnotationNames.NavigationCandidates);
}
}

private sealed class GraphAdapter : Graph<IConventionEntityType>
{
private readonly IConventionModel _model;

public GraphAdapter([NotNull] IConventionModel model)
{
_model = model;
}

public override IEnumerable<IConventionEntityType> Vertices => _model.GetEntityTypes();

public override IEnumerable<IConventionEntityType> GetOutgoingNeighbors(IConventionEntityType from)
=> from.GetForeignKeys().Where(fk => fk.DependentToPrincipal != null).Select(fk => fk.PrincipalEntityType)
.Union(
from.GetReferencingForeignKeys().Where(fk => fk.PrincipalToDependent != null).Select(fk => fk.DeclaringEntityType));

public override IEnumerable<IConventionEntityType> GetIncomingNeighbors(IConventionEntityType to)
=> to.GetForeignKeys().Where(fk => fk.PrincipalToDependent != null).Select(fk => fk.PrincipalEntityType)
.Union(
to.GetReferencingForeignKeys().Where(fk => fk.DependentToPrincipal != null).Select(fk => fk.DeclaringEntityType));
}
}
}

60 changes: 0 additions & 60 deletions src/EFCore/Metadata/Internal/ModelNavigationsGraphAdapter.cs

This file was deleted.

41 changes: 41 additions & 0 deletions src/Shared/Graph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 JetBrains.Annotations;

namespace Microsoft.EntityFrameworkCore.Utilities
{
internal abstract class Graph<TVertex>
{
public abstract IEnumerable<TVertex> Vertices { get; }

public abstract IEnumerable<TVertex> GetOutgoingNeighbors([NotNull] TVertex from);

public abstract IEnumerable<TVertex> GetIncomingNeighbors([NotNull] TVertex to);

public virtual ISet<TVertex> GetUnreachableVertices([NotNull] IReadOnlyList<TVertex> roots)
{
var unreachableVertices = new HashSet<TVertex>(Vertices);
unreachableVertices.ExceptWith(roots);
var visitingQueue = new List<TVertex>(roots);

var currentVertexIndex = 0;
while (currentVertexIndex < visitingQueue.Count)
{
var currentVertex = visitingQueue[currentVertexIndex];
currentVertexIndex++;
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var neighbor in GetOutgoingNeighbors(currentVertex))
{
if (unreachableVertices.Remove(neighbor))
{
visitingQueue.Add(neighbor);
}
}
}

return unreachableVertices;
}
}
}
3 changes: 0 additions & 3 deletions src/Shared/Multigraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Utilities
{
#pragma warning disable EF1001 // Internal EF Core API usage.
internal class Multigraph<TVertex, TEdge> : Graph<TVertex>
#pragma warning restore EF1001 // Internal EF Core API usage.
{
private readonly HashSet<TVertex> _vertices = new HashSet<TVertex>();

Expand Down