Skip to content

Commit a714dab

Browse files
committed
Make Graph and Multigraph fully internal
Fixes #20382
1 parent 65204be commit a714dab

File tree

5 files changed

+68
-136
lines changed

5 files changed

+68
-136
lines changed

src/EFCore/Internal/Graph.cs

-71
This file was deleted.

src/EFCore/Metadata/Conventions/ModelCleanupConvention.cs

+27-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.EntityFrameworkCore.Metadata.Builders;
88
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
99
using Microsoft.EntityFrameworkCore.Metadata.Internal;
10+
using Microsoft.EntityFrameworkCore.Utilities;
1011

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

3233
/// <inheritdoc />
33-
public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
34+
public virtual void ProcessModelFinalizing(
35+
IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
3436
{
3537
RemoveEntityTypesUnreachableByNavigations(modelBuilder, context);
3638
RemoveNavigationlessForeignKeys(modelBuilder);
@@ -44,7 +46,7 @@ private void RemoveEntityTypesUnreachableByNavigations(
4446
var rootEntityTypes = GetRoots(model, ConfigurationSource.DataAnnotation);
4547
using (context.DelayConventions())
4648
{
47-
foreach (var orphan in new ModelNavigationsGraphAdapter(model).GetUnreachableVertices(rootEntityTypes))
49+
foreach (var orphan in new GraphAdapter(model).GetUnreachableVertices(rootEntityTypes))
4850
{
4951
modelBuilder.HasNoEntityType(orphan, fromDataAnnotation: true);
5052
}
@@ -90,5 +92,28 @@ private void RemoveModelBuildingAnnotations(IConventionModelBuilder modelBuilder
9092
entityType.RemoveAnnotation(CoreAnnotationNames.NavigationCandidates);
9193
}
9294
}
95+
96+
private sealed class GraphAdapter : Graph<IConventionEntityType>
97+
{
98+
private readonly IConventionModel _model;
99+
100+
public GraphAdapter([NotNull] IConventionModel model)
101+
{
102+
_model = model;
103+
}
104+
105+
public override IEnumerable<IConventionEntityType> Vertices => _model.GetEntityTypes();
106+
107+
public override IEnumerable<IConventionEntityType> GetOutgoingNeighbors(IConventionEntityType from)
108+
=> from.GetForeignKeys().Where(fk => fk.DependentToPrincipal != null).Select(fk => fk.PrincipalEntityType)
109+
.Union(
110+
from.GetReferencingForeignKeys().Where(fk => fk.PrincipalToDependent != null).Select(fk => fk.DeclaringEntityType));
111+
112+
public override IEnumerable<IConventionEntityType> GetIncomingNeighbors(IConventionEntityType to)
113+
=> to.GetForeignKeys().Where(fk => fk.PrincipalToDependent != null).Select(fk => fk.PrincipalEntityType)
114+
.Union(
115+
to.GetReferencingForeignKeys().Where(fk => fk.DependentToPrincipal != null).Select(fk => fk.DeclaringEntityType));
116+
}
93117
}
94118
}
119+

src/EFCore/Metadata/Internal/ModelNavigationsGraphAdapter.cs

-60
This file was deleted.

src/Shared/Graph.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using JetBrains.Annotations;
6+
7+
namespace Microsoft.EntityFrameworkCore.Utilities
8+
{
9+
internal abstract class Graph<TVertex>
10+
{
11+
public abstract IEnumerable<TVertex> Vertices { get; }
12+
13+
public abstract IEnumerable<TVertex> GetOutgoingNeighbors([NotNull] TVertex from);
14+
15+
public abstract IEnumerable<TVertex> GetIncomingNeighbors([NotNull] TVertex to);
16+
17+
public virtual ISet<TVertex> GetUnreachableVertices([NotNull] IReadOnlyList<TVertex> roots)
18+
{
19+
var unreachableVertices = new HashSet<TVertex>(Vertices);
20+
unreachableVertices.ExceptWith(roots);
21+
var visitingQueue = new List<TVertex>(roots);
22+
23+
var currentVertexIndex = 0;
24+
while (currentVertexIndex < visitingQueue.Count)
25+
{
26+
var currentVertex = visitingQueue[currentVertexIndex];
27+
currentVertexIndex++;
28+
// ReSharper disable once LoopCanBeConvertedToQuery
29+
foreach (var neighbor in GetOutgoingNeighbors(currentVertex))
30+
{
31+
if (unreachableVertices.Remove(neighbor))
32+
{
33+
visitingQueue.Add(neighbor);
34+
}
35+
}
36+
}
37+
38+
return unreachableVertices;
39+
}
40+
}
41+
}

src/Shared/Multigraph.cs

-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
using System.Linq;
77
using JetBrains.Annotations;
88
using Microsoft.EntityFrameworkCore.Diagnostics;
9-
using Microsoft.EntityFrameworkCore.Internal;
109

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

0 commit comments

Comments
 (0)