-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#32 Lazy cyclic dependencies are no longer possible in v2.0
- Loading branch information
Nikolay Pyanikov
committed
Aug 11, 2023
1 parent
a4d29f2
commit 35af751
Showing
10 changed files
with
193 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Pure.DI.Core; | ||
|
||
internal class GraphPath : IGraphPath | ||
{ | ||
private readonly List<DependencyNode> _parts = new(); | ||
private readonly HashSet<MdBinding> _bindings = new(); | ||
private HashSet<MdBinding> _prevBindings = new(); | ||
|
||
public bool TryAddPart(in DependencyNode node) | ||
{ | ||
_parts.Add(node); | ||
|
||
// ReSharper disable once InvertIf | ||
if (node.Type is INamedTypeSymbol { IsGenericType: true, Name: "Func" } namedType | ||
&& namedType.ContainingNamespace.Name == "System") | ||
{ | ||
LazyBarrier(); | ||
} | ||
|
||
return _bindings.Add(node.Binding); | ||
} | ||
|
||
public bool IsCompleted(in DependencyNode node) => | ||
_prevBindings.Contains(node.Binding); | ||
|
||
private void LazyBarrier() | ||
{ | ||
_prevBindings = new HashSet<MdBinding>(_bindings); | ||
_bindings.Clear(); | ||
} | ||
|
||
public override string ToString() => string.Join(" <-- ", _parts.Select(i => i.Type)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Pure.DI.Core; | ||
|
||
internal interface IGraphPath | ||
{ | ||
bool TryAddPart(in DependencyNode node); | ||
bool IsCompleted(in DependencyNode node); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Pure.DI.Core; | ||
|
||
internal interface IPathfinder | ||
{ | ||
IEnumerable<(int pathId, Dependency dependency)> GetPaths( | ||
IGraph<DependencyNode, Dependency> graph, | ||
DependencyNode rootNode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace Pure.DI.Core; | ||
|
||
internal class Pathfinder : IPathfinder | ||
{ | ||
private readonly CancellationToken _cancellationToken; | ||
|
||
public Pathfinder(CancellationToken cancellationToken) | ||
{ | ||
_cancellationToken = cancellationToken; | ||
} | ||
|
||
public IEnumerable<(int pathId, Dependency dependency)> GetPaths( | ||
IGraph<DependencyNode, Dependency> graph, | ||
DependencyNode rootNode) | ||
{ | ||
if (!graph.TryGetInEdges(rootNode, out var dependencies)) | ||
{ | ||
yield break; | ||
} | ||
|
||
var id = 0; | ||
var enumerators = new Stack<(int Id, IEnumerator<Dependency> Enumerator)>(); | ||
enumerators.Push((rootNode.Binding.Id, dependencies.GetEnumerator())); | ||
while (enumerators.TryPop(out var enumerator)) | ||
{ | ||
_cancellationToken.ThrowIfCancellationRequested(); | ||
if (!enumerator.Enumerator.MoveNext()) | ||
{ | ||
id++; | ||
continue; | ||
} | ||
|
||
var dependency = enumerator.Enumerator.Current; | ||
yield return (id, dependency); | ||
enumerators.Push(enumerator); | ||
if (!graph.TryGetInEdges(dependency.Source, out var nestedDependencies)) | ||
{ | ||
continue; | ||
} | ||
|
||
enumerators.Push((dependency.Source.Binding.Id, nestedDependencies.GetEnumerator())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters