Skip to content

Commit

Permalink
Improved class diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Dec 2, 2024
1 parent 07efd2c commit 4c4c332
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 36 deletions.
8 changes: 3 additions & 5 deletions readme/ArrayDetails.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ classDiagram
Service3v2 *-- "2 " Service4 : IService4
Service3v3 *-- "2 " Service4 : IService4
Service3v4 *-- "2 " Service4 : IService4
class ArrayᐸIService3ᐳ {
<<array>>
}
namespace Pure.DI.Benchmarks.Benchmarks {
class Array {
<<partial>>
Expand Down Expand Up @@ -79,11 +82,6 @@ classDiagram
+Service4()
}
}
namespace System {
class ArrayᐸIService3ᐳ {
<<array>>
}
}
```

### Generated code
Expand Down
8 changes: 3 additions & 5 deletions readme/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ classDiagram
Service *-- ArrayᐸIDependencyᐳ : ArrayᐸIDependencyᐳ
ArrayᐸIDependencyᐳ *-- AbcDependency : IDependency
ArrayᐸIDependencyᐳ *-- XyzDependency : 2 IDependency
class ArrayᐸIDependencyᐳ {
<<array>>
}
namespace Pure.DI.UsageTests.BCL.ArrayScenario {
class AbcDependency {
+AbcDependency()
Expand All @@ -247,10 +250,5 @@ classDiagram
+XyzDependency()
}
}
namespace System {
class ArrayᐸIDependencyᐳ {
<<array>>
}
}
```

5 changes: 0 additions & 5 deletions readme/complex-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ classDiagram
ServiceᐸT1ˏInt32ˏListᐸT1ᐳˏDictionaryᐸT1ˏInt32ᐳᐳ *-- DependencyᐸT1ᐳ : IDependencyᐸT1ᐳ
ServiceᐸT1ˏInt32ˏListᐸT1ᐳˏDictionaryᐸT1ˏInt32ᐳᐳ o-- "Singleton" DependencyStructᐸInt32ᐳ : "value type" IDependencyᐸInt32ᐳ
DependencyᐸT1ᐳ o-- T1 : Argument "depArg"
namespace Pure.DI {
class T1 {
<<abstract>>
}
}
namespace Pure.DI.UsageTests.Generics.ComplexGenericsScenario {
class Composition {
<<partial>>
Expand Down
5 changes: 0 additions & 5 deletions readme/generic-root-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ classDiagram
ServiceᐸT1ᐳ --|> IServiceᐸT1ᐳ
Composition ..> ServiceᐸT1ᐳ : IServiceᐸT1ᐳ GetMyServiceᐸT1ᐳ(T1 someArg)
ServiceᐸT1ᐳ o-- T1 : Argument "someArg"
namespace Pure.DI {
class T1 {
<<abstract>>
}
}
namespace Pure.DI.UsageTests.Basics.GenericRootArgScenario {
class Composition {
<<partial>>
Expand Down
8 changes: 3 additions & 5 deletions readme/overriding-the-bcl-binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ classDiagram
Service --|> IService
Composition ..> Service : IService Root
Service *-- ArrayᐸIDependencyᐳ : ArrayᐸIDependencyᐳ
class ArrayᐸIDependencyᐳ {
<<array>>
}
namespace Pure.DI.UsageTests.BCL.OverridingBclBindingScenario {
class Composition {
<<partial>>
Expand All @@ -219,10 +222,5 @@ classDiagram
+Service(ArrayᐸIDependencyᐳ dependencies)
}
}
namespace System {
class ArrayᐸIDependencyᐳ {
<<array>>
}
}
```

37 changes: 26 additions & 11 deletions src/Pure.DI.Core/Core/Code/ClassDiagramBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,40 @@ public LinesBuilder Build(CompositionCode composition)
lines.AppendLine($"{FormatType(setup, node.Type, DefaultFormatOptions)} --|> {FormatType(setup, contract.Type, DefaultFormatOptions)}{(string.IsNullOrWhiteSpace(tag) ? "" : $" : {tag}")}");
}

var classDiagramWalker = new ClassDiagramWalker(setup, this, classes, DefaultFormatOptions);
var classDiagramWalker = new ClassDiagramWalker(setup, marker, this, classes, DefaultFormatOptions);
classDiagramWalker.VisitDependencyNode(new LinesBuilder(), node);
}

// ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
foreach (var type in types)
{
classes.Add(new Class(type.ContainingNamespace?.ToDisplayString() ?? "", FormatType(setup, type, DefaultFormatOptions), "", type, new LinesBuilder()));
if (!marker.IsMarker(setup, type))
{
classes.Add(new Class(type.ContainingNamespace?.ToDisplayString() ?? "", FormatType(setup, type, DefaultFormatOptions), "", type, new LinesBuilder()));
}
}

foreach (var (dependency, count) in graph.Edges.GroupBy(i => i).Select(i => (dependency: i.First(), count: i.Count())))
{
cancellationToken.ThrowIfCancellationRequested();
var sourceType = FormatType(setup, dependency.Source.Type, DefaultFormatOptions);
classes.Add(new Class(dependency.Source.Type.ContainingNamespace?.ToDisplayString() ?? "", sourceType, "", dependency.Source.Type, new LinesBuilder()));
if (!marker.IsMarker(setup, dependency.Source.Type))
{
classes.Add(new Class(dependency.Source.Type.ContainingNamespace?.ToDisplayString() ?? "", sourceType, "", dependency.Source.Type, new LinesBuilder()));
}

if (dependency.Target.Root is not null && rootProperties.TryGetValue(dependency.Injection, out var root))
{
lines.AppendLine($"{composition.Source.Source.Name.ClassName} ..> {sourceType} : {FormatRoot(setup, root)}");
}
else
{
var targetType = FormatType(setup, dependency.Target.Type, DefaultFormatOptions);
classes.Add(new Class(dependency.Target.Type.ContainingNamespace?.ToDisplayString() ?? "", targetType, "", dependency.Target.Type, new LinesBuilder()));
if (!marker.IsMarker(setup, dependency.Target.Type))
{
classes.Add(new Class(dependency.Target.Type.ContainingNamespace?.ToDisplayString() ?? "", targetType, "", dependency.Target.Type, new LinesBuilder()));
}

if (dependency.Source.Arg is { } arg)
{
var tags = arg.Binding.Contracts.SelectMany(i => i.Tags.Select(tag => tag.Value)).ToArray();
Expand All @@ -137,13 +149,13 @@ public LinesBuilder Build(CompositionCode composition)
}
}

foreach (var classByNamespace in classes.GroupBy(i => i.ActualNamespace).OrderBy(i => i.Key))
foreach (var classByNamespace in classes.GroupBy(i => i.Namespace).OrderBy(i => i.Key))
{
var ns = classByNamespace.Key;
var hasNamespace = !string.IsNullOrWhiteSpace(ns);
var actualNamespace = classByNamespace.Key;
var hasNamespace = !string.IsNullOrWhiteSpace(actualNamespace);
if (hasNamespace)
{
lines.AppendLine($"namespace {ns} {{");
lines.AppendLine($"namespace {actualNamespace} {{");
lines.IncIndent();
}

Expand Down Expand Up @@ -325,6 +337,7 @@ private record FormatOptions(

private class ClassDiagramWalker(
MdSetup setup,
IMarker marker,
ClassDiagramBuilder builder,
IList<Class> classes,
FormatOptions options)
Expand All @@ -335,7 +348,11 @@ public override void VisitDependencyNode(in LinesBuilder ctx, DependencyNode nod
var lines = new LinesBuilder();
var name = builder.FormatType(setup, node.Type, options);
var cls = new Class(node.Type.ContainingNamespace?.ToDisplayString() ?? "", name, "", node.Type, lines);
classes.Add(cls);
if (!marker.IsMarker(setup, node.Type))
{
classes.Add(cls);
}

lines.AppendLine($"class {name} {{");
using (lines.Indent())
{
Expand Down Expand Up @@ -380,8 +397,6 @@ public override void VisitField(in LinesBuilder ctx, in DpField field)

private record Class(string Namespace, string Name, string Kind, ITypeSymbol? Type, LinesBuilder Lines)
{
public string ActualNamespace => string.IsNullOrWhiteSpace(Namespace) ? nameof(System) : Namespace;

public string ActualKind
{
get
Expand Down

0 comments on commit 4c4c332

Please sign in to comment.