@@ -1566,4 +1566,95 @@ public static void Main()
15661566 result . Warnings . Count . ShouldBe ( 0 , result ) ;
15671567 result . StdOut . ShouldBe ( [ "41" , "True" , "41" , "False" , "True" , "True" ] , result ) ;
15681568 }
1569+
1570+ [ Fact ]
1571+ public async Task ShouldSupportSetupContextReferenceWhenCreatingScope ( )
1572+ {
1573+ // Given
1574+
1575+ // When
1576+ var result = await """
1577+ using System;
1578+ using Pure.DI;
1579+
1580+ namespace UnityEngine
1581+ {
1582+ // Lightweight test double to avoid Unity dependency
1583+ public class MonoBehaviour {}
1584+ }
1585+
1586+ namespace Sample
1587+ {
1588+ internal partial class BaseComposition
1589+ {
1590+ internal int SettingsValue { get; set; }
1591+
1592+ private void Setup()
1593+ {
1594+ DI.Setup(nameof(BaseComposition), CompositionKind.Internal)
1595+ .Bind<int>().To(_ => SettingsValue);
1596+ }
1597+ }
1598+
1599+ interface IService
1600+ {
1601+ int Value { get; }
1602+ }
1603+
1604+ sealed class Service : IService
1605+ {
1606+ public Service(int value)
1607+ {
1608+ Value = value;
1609+ }
1610+
1611+ public int Value { get; }
1612+ }
1613+
1614+ internal partial class Composition : UnityEngine.MonoBehaviour
1615+ {
1616+ private void Setup()
1617+ {
1618+ DI.Setup(nameof(Composition))
1619+ .DependsOn(nameof(BaseComposition), SetupContextKind.Reference, "baseContext")
1620+ .Bind<IService>().As(Lifetime.Scoped).To<Service>()
1621+ .Root<IService>("Service");
1622+ }
1623+
1624+ public Composition CreateScope() => new(this);
1625+ }
1626+
1627+ public class Program
1628+ {
1629+ public static void Main()
1630+ {
1631+ var baseContext = new BaseComposition
1632+ {
1633+ SettingsValue = 41
1634+ };
1635+
1636+ var composition = new Composition(baseContext);
1637+
1638+ var scope1 = composition.CreateScope();
1639+ var service11 = scope1.Service;
1640+ var service12 = scope1.Service;
1641+ Console.WriteLine(service11.Value);
1642+ Console.WriteLine(service11 == service12);
1643+
1644+ baseContext.SettingsValue = 73;
1645+ var scope2 = composition.CreateScope();
1646+ var service2 = scope2.Service;
1647+ Console.WriteLine(service2.Value);
1648+ Console.WriteLine(service11 == service2);
1649+ }
1650+ }
1651+ }
1652+ """ . RunAsync ( new Options ( LanguageVersion : LanguageVersion . CSharp9 ) ) ;
1653+
1654+ // Then
1655+ result . Success . ShouldBeTrue ( result ) ;
1656+ result . Errors . Count . ShouldBe ( 0 , result ) ;
1657+ result . Warnings . Count . ShouldBe ( 0 , result ) ;
1658+ result . StdOut . ShouldBe ( [ "41" , "True" , "73" , "False" ] , result ) ;
1659+ }
15691660}
0 commit comments