Skip to content

Commit 132bd88

Browse files
Add virtual and override root kinds
1 parent 8a5cfed commit 132bd88

4 files changed

Lines changed: 152 additions & 2 deletions

File tree

src/Pure.DI.Core/Components/Api.g.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ internal enum RootKinds
13781378
Partial = 1 << 6,
13791379

13801380
/// <summary>
1381-
/// Specifies to create a exposed root of the composition.
1381+
/// Specifies to create an exposed root of the composition.
13821382
/// </summary>
13831383
/// <seealso cref="BindAttribute"/>
13841384
Exposed = 1 << 7,
@@ -1387,6 +1387,16 @@ internal enum RootKinds
13871387
/// Specifies to use a <c>protected</c> access modifier for the root of the composition.
13881388
/// </summary>
13891389
Protected = 1 << 8,
1390+
1391+
/// <summary>
1392+
/// Specifies to use a <c>virtual</c> modifier for the root of the composition.
1393+
/// </summary>
1394+
Virtual = 1 << 9,
1395+
1396+
/// <summary>
1397+
/// Specifies to use a <c>override</c> modifier for the root of the composition.
1398+
/// </summary>
1399+
Override = 1 << 10,
13901400
}
13911401

13921402
/// <summary>

src/Pure.DI.Core/Core/Code/Parts/RootMethodsBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ private void BuildRoot(CompositionCode composition, Root root)
8383
name.Append(" partial");
8484
}
8585

86+
if ((root.Kind & RootKinds.Virtual) == RootKinds.Virtual)
87+
{
88+
name.Append(" virtual");
89+
}
90+
91+
if ((root.Kind & RootKinds.Override) == RootKinds.Override)
92+
{
93+
name.Append(" override");
94+
}
95+
8696
name.Append(' ');
8797
name.Append(root.TypeDescription.Name);
8898

src/Pure.DI.Core/Generator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private void Setup() => DI.Setup()
179179
.Bind(UniqueTag).To<IdGenerator>()
180180
.Bind(GenericType).To<IdGenerator>()
181181
.Bind(Injection).To<IdGenerator>()
182-
.Bind(Override).To<IdGenerator>()
182+
.Bind(Tag.Override).To<IdGenerator>()
183183
.Bind().To<IdGenerator>()
184184
.Bind().To<Registry<TT>>()
185185
.Bind().To<Locks>()

tests/Pure.DI.IntegrationTests/RootsTests.cs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,4 +622,134 @@ public static void Main()
622622
result.Success.ShouldBeTrue(result);
623623
result.StdOut.ShouldBe(["Initialize 1 Abc", "Initialize 1 Abc"], result);
624624
}
625+
626+
[Fact]
627+
public async Task ShouldSupportVirtualRoot()
628+
{
629+
// Given
630+
631+
// When
632+
var result = await """
633+
using System;
634+
using Pure.DI;
635+
636+
namespace Sample
637+
{
638+
interface IDependency {}
639+
640+
class Dependency: IDependency
641+
{
642+
}
643+
644+
interface IService
645+
{
646+
IDependency? Dep { get; }
647+
}
648+
649+
class Service: IService
650+
{
651+
[Ordinal(1)]
652+
internal void Initialize([Tag(374)] string depName)
653+
{
654+
Console.WriteLine($"Initialize 1 {depName}");
655+
}
656+
657+
[Ordinal(0)]
658+
public IDependency? Dep { get; set; }
659+
}
660+
661+
static class Setup
662+
{
663+
private static void SetupComposition()
664+
{
665+
DI.Setup("Composition")
666+
.Bind(374).To(_ => "Abc")
667+
.Bind().To<Dependency>()
668+
.Root<Service>("MyService", kind: RootKinds.Virtual);
669+
}
670+
}
671+
672+
public class Program
673+
{
674+
public static void Main()
675+
{
676+
var composition = new Composition();
677+
var service = composition.MyService;
678+
}
679+
}
680+
}
681+
""".RunAsync();
682+
683+
// Then
684+
result.Success.ShouldBeTrue(result);
685+
result.StdOut.ShouldBe(["Initialize 1 Abc"], result);
686+
}
687+
688+
[Fact]
689+
public async Task ShouldOverrideRoot()
690+
{
691+
// Given
692+
693+
// When
694+
var result = await """
695+
using System;
696+
using Pure.DI;
697+
698+
namespace Sample
699+
{
700+
interface IDependency {}
701+
702+
class Dependency: IDependency
703+
{
704+
}
705+
706+
interface IService
707+
{
708+
IDependency? Dep { get; }
709+
}
710+
711+
class Service: IService
712+
{
713+
[Ordinal(1)]
714+
internal void Initialize([Tag(374)] string depName)
715+
{
716+
Console.WriteLine($"Initialize 1 {depName}");
717+
}
718+
719+
[Ordinal(0)]
720+
public IDependency? Dep { get; set; }
721+
}
722+
723+
internal abstract class Composition
724+
{
725+
internal abstract IService MyService { get; }
726+
}
727+
728+
internal partial class OtherComposition: Composition
729+
{
730+
private static void Setup()
731+
{
732+
DI.Setup()
733+
.Bind(374).To(_ => "Abc")
734+
.Bind().To<Dependency>()
735+
.Bind().To<Service>()
736+
.Root<IService>("MyService", kind: RootKinds.Internal | RootKinds.Override);
737+
}
738+
}
739+
740+
public class Program
741+
{
742+
public static void Main()
743+
{
744+
var composition = new OtherComposition();
745+
var service = composition.MyService;
746+
}
747+
}
748+
}
749+
""".RunAsync();
750+
751+
// Then
752+
result.Success.ShouldBeTrue(result);
753+
result.StdOut.ShouldBe(["Initialize 1 Abc"], result);
754+
}
625755
}

0 commit comments

Comments
 (0)