diff --git a/readme/auto-scoped.md b/readme/auto-scoped.md index bc5cf8e7..bebafa75 100644 --- a/readme/auto-scoped.md +++ b/readme/auto-scoped.md @@ -153,7 +153,7 @@ classDiagram class Composition { <> +Program ProgramRoot - +Service SessionRoot + -Service SessionRoot } class Dependency { +Dependency() diff --git a/readme/composition-root-kinds.md b/readme/composition-root-kinds.md index 0cf21f3b..c333b3c4 100644 --- a/readme/composition-root-kinds.md +++ b/readme/composition-root-kinds.md @@ -247,9 +247,9 @@ classDiagram namespace Pure.DI.UsageTests.Advanced.CompositionRootKindsScenario { class Composition { <> - +IDependency Dependency + ~IDependency Dependency +IService GetOtherService() - +IService GetRoot() + -IService GetRoot() + T ResolveᐸTᐳ() + T ResolveᐸTᐳ(object? tag) + object Resolve(Type type) diff --git a/readme/partial-class.md b/readme/partial-class.md index 8592232d..acd172ed 100644 --- a/readme/partial-class.md +++ b/readme/partial-class.md @@ -229,7 +229,7 @@ classDiagram namespace Pure.DI.UsageTests.Advanced.PartialClassScenario { class Composition { <> - +Service Root + ~Service Root + T ResolveᐸTᐳ() + T ResolveᐸTᐳ(object? tag) + object Resolve(Type type) diff --git a/src/Pure.DI.Core/Components/Api.g.cs b/src/Pure.DI.Core/Components/Api.g.cs index 9498444b..f9f31b94 100644 --- a/src/Pure.DI.Core/Components/Api.g.cs +++ b/src/Pure.DI.Core/Components/Api.g.cs @@ -1093,37 +1093,42 @@ internal enum RootKinds /// /// Specifies to use a internal access modifier for the root of the composition. /// - Internal = 2, + Internal = 1 << 1, /// /// Specifies to use a private access modifier for the root of the composition. /// - Private = 4, + Private = 1 << 2, /// /// Specifies to create a composition root as a property. /// - Property = 8, + Property = 1 << 3, /// /// Specifies to create a composition root as a method. /// - Method = 16, + Method = 1 << 4, /// /// Specifies to create a static root of the composition. /// - Static = 32, + Static = 1 << 5, /// /// Specifies to create a partial root of the composition. /// - Partial = 64, + Partial = 1 << 6, /// /// Specifies to create a exposed root of the composition. /// - Exposed = 128 + Exposed = 1 << 7, + + /// + /// Specifies to use a protected access modifier for the root of the composition. + /// + Protected = 1 << 8, } /// diff --git a/src/Pure.DI.Core/Core/Code/ClassDiagramBuilder.cs b/src/Pure.DI.Core/Core/Code/ClassDiagramBuilder.cs index df1b91fc..0232e7fc 100644 --- a/src/Pure.DI.Core/Core/Code/ClassDiagramBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/ClassDiagramBuilder.cs @@ -8,6 +8,7 @@ internal sealed class ClassDiagramBuilder( IBuilder> injectionsBuilder, IMarker marker, ITypeResolver typeResolver, + IRootAccessModifierResolver rootAccessModifierResolver, CancellationToken cancellationToken) : IBuilder { @@ -38,7 +39,7 @@ public LinesBuilder Build(CompositionCode composition) compositionLines.AppendLine("<>"); foreach (var root in composition.Roots.OrderByDescending(i => i.IsPublic).ThenBy(i => i.Name)) { - compositionLines.AppendLine($"{(root.IsPublic ? "+" : "-")}{FormatRoot(setup, root)}"); + compositionLines.AppendLine($"{Format(rootAccessModifierResolver.Resolve(root))}{FormatRoot(setup, root)}"); } if (hasResolveMethods) @@ -320,7 +321,6 @@ private string ResolveTypeName(MdSetup setup, ITypeSymbol typeSymbol) private static string Format(Accessibility accessibility) => accessibility switch { - Accessibility.NotApplicable => "", Accessibility.Private => "-", Accessibility.ProtectedAndInternal => "#", Accessibility.Protected => "#", diff --git a/src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs b/src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs index b021d1f3..68f14b55 100644 --- a/src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs @@ -7,6 +7,7 @@ internal sealed class RootMethodsBuilder( ITypeResolver typeResolver, [Tag(typeof(RootMethodsCommenter))] ICommenter rootCommenter, IMarker marker, + IRootAccessModifierResolver rootAccessModifierResolver, CancellationToken cancellationToken) : IBuilder { @@ -51,15 +52,20 @@ private void BuildRoot(CompositionCode composition, Root root) rootArgsStr = $"({string.Join(", ", root.Args.Select(arg => $"{typeResolver.Resolve(composition.Source.Source, arg.InstanceType)} {arg.VariableDeclarationName}"))})"; buildTools.AddPureHeader(code); } - - var modifier = !root.IsPublic || (root.Kind & RootKinds.Private) == RootKinds.Private - ? "private" - : (root.Kind & RootKinds.Internal) == RootKinds.Internal - ? "internal" - : "public"; - + var name = new StringBuilder(); - name.Append(modifier); + var accessModifier = rootAccessModifierResolver.Resolve(root) switch + { + Accessibility.Private => "private", + Accessibility.ProtectedAndInternal => "protected", + Accessibility.Protected => "protected", + Accessibility.Internal => "internal", + Accessibility.ProtectedOrInternal => "internal", + Accessibility.Public => "public", + _ => "" + }; + + name.Append(accessModifier); if ((root.Kind & RootKinds.Static) == RootKinds.Static) { name.Append(" static"); diff --git a/src/Pure.DI.Core/Core/IRootAccessModifierResolver.cs b/src/Pure.DI.Core/Core/IRootAccessModifierResolver.cs new file mode 100644 index 00000000..e41af7f9 --- /dev/null +++ b/src/Pure.DI.Core/Core/IRootAccessModifierResolver.cs @@ -0,0 +1,6 @@ +namespace Pure.DI.Core; + +internal interface IRootAccessModifierResolver +{ + Accessibility Resolve(Root root); +} \ No newline at end of file diff --git a/src/Pure.DI.Core/Core/RootAccessModifierResolver.cs b/src/Pure.DI.Core/Core/RootAccessModifierResolver.cs new file mode 100644 index 00000000..0bb07a7a --- /dev/null +++ b/src/Pure.DI.Core/Core/RootAccessModifierResolver.cs @@ -0,0 +1,15 @@ +namespace Pure.DI.Core; + +internal class RootAccessModifierResolver : IRootAccessModifierResolver +{ + public Accessibility Resolve(Root root) => + root.IsPublic switch + { + true when (root.Kind & RootKinds.Public) != 0 => Accessibility.Public, + true when (root.Kind & RootKinds.Internal) != 0 => Accessibility.Internal, + true when (root.Kind & RootKinds.Protected) != 0 => Accessibility.Protected, + true when (root.Kind & RootKinds.Private) != 0 => Accessibility.Private, + false => Accessibility.Private, + _ => Accessibility.Public + }; +} \ No newline at end of file diff --git a/src/Pure.DI.Core/Generator.cs b/src/Pure.DI.Core/Generator.cs index 8d164688..96c24a99 100644 --- a/src/Pure.DI.Core/Generator.cs +++ b/src/Pure.DI.Core/Generator.cs @@ -145,5 +145,6 @@ private void Setup() => DI.Setup() .Bind("Injection").To() .Bind().To() .Bind().To>() - .Bind().To(); + .Bind().To() + .Bind().To(); } \ No newline at end of file