@@ -1064,4 +1064,235 @@ public static void Main()
10641064 result . Success . ShouldBeTrue ( result ) ;
10651065 result . StdOut . ShouldBe ( [ "True" ] , result ) ;
10661066 }
1067+
1068+ [ Fact ]
1069+ public async Task ShouldSupportBuilderWithArgs ( )
1070+ {
1071+ // Given
1072+
1073+ // When
1074+ var result = await """
1075+ using System;
1076+ using Pure.DI;
1077+ #pragma warning disable CS8618
1078+
1079+ namespace Sample
1080+ {
1081+ interface IDependency { string Name { get; } }
1082+ class Dependency: IDependency
1083+ {
1084+ public Dependency(string name) => Name = name;
1085+ public string Name { get; }
1086+ }
1087+
1088+ class Service
1089+ {
1090+ [Ordinal(0)]
1091+ public IDependency Dep { get; set; }
1092+ }
1093+
1094+ static class Setup
1095+ {
1096+ private static void SetupComposition()
1097+ {
1098+ DI.Setup("Composition")
1099+ .Arg<string>("depName")
1100+ .Bind<IDependency>().To<Dependency>()
1101+ .Builder<Service>("BuildUpService");
1102+ }
1103+ }
1104+
1105+ public class Program
1106+ {
1107+ public static void Main()
1108+ {
1109+ var composition = new Composition(depName: "ArgValue");
1110+ var service = new Service();
1111+ composition.BuildUpService(service);
1112+ Console.WriteLine(service.Dep.Name);
1113+ }
1114+ }
1115+ }
1116+ """ . RunAsync ( ) ;
1117+
1118+ // Then
1119+ result . Success . ShouldBeTrue ( result ) ;
1120+ result . StdOut . ShouldBe ( [ "ArgValue" ] , result ) ;
1121+ }
1122+
1123+ [ Fact ]
1124+ public async Task ShouldSupportBuilderWithLifetimes ( )
1125+ {
1126+ // Given
1127+
1128+ // When
1129+ var result = await """
1130+ using System;
1131+ using Pure.DI;
1132+ #pragma warning disable CS8618
1133+
1134+ namespace Sample
1135+ {
1136+ interface IDependency {}
1137+ class Dependency: IDependency {}
1138+
1139+ class Service
1140+ {
1141+ [Ordinal(0)]
1142+ public IDependency Dep1 { get; set; }
1143+
1144+ [Ordinal(1)]
1145+ public IDependency Dep2 { get; set; }
1146+ }
1147+
1148+ static class Setup
1149+ {
1150+ private static void SetupComposition()
1151+ {
1152+ DI.Setup("Composition")
1153+ .Bind<IDependency>().As(Lifetime.Singleton).To<Dependency>()
1154+ .Builder<Service>("BuildUpService");
1155+ }
1156+ }
1157+
1158+ public class Program
1159+ {
1160+ public static void Main()
1161+ {
1162+ var composition = new Composition();
1163+ var service1 = new Service();
1164+ composition.BuildUpService(service1);
1165+
1166+ var service2 = new Service();
1167+ composition.BuildUpService(service2);
1168+
1169+ Console.WriteLine(ReferenceEquals(service1.Dep1, service1.Dep2));
1170+ Console.WriteLine(ReferenceEquals(service1.Dep1, service2.Dep1));
1171+ }
1172+ }
1173+ }
1174+ """ . RunAsync ( ) ;
1175+
1176+ // Then
1177+ result . Success . ShouldBeTrue ( result ) ;
1178+ result . StdOut . ShouldBe ( [ "True" , "True" ] , result ) ;
1179+ }
1180+
1181+ [ Fact ]
1182+ public async Task ShouldSupportBuilderWithPartialClasses ( )
1183+ {
1184+ // Given
1185+
1186+ // When
1187+ var result = await """
1188+ using System;
1189+ using Pure.DI;
1190+ #pragma warning disable CS8618
1191+
1192+ namespace Sample
1193+ {
1194+ interface IDependency {}
1195+ class Dependency: IDependency {}
1196+
1197+ partial class Service
1198+ {
1199+ [Ordinal(0)]
1200+ public IDependency Dep1 { get; set; }
1201+ }
1202+
1203+ partial class Service
1204+ {
1205+ [Ordinal(1)]
1206+ public IDependency Dep2 { get; set; }
1207+ }
1208+
1209+ static class Setup
1210+ {
1211+ private static void SetupComposition()
1212+ {
1213+ DI.Setup("Composition")
1214+ .Bind<IDependency>().To<Dependency>()
1215+ .Builder<Service>("BuildUpService");
1216+ }
1217+ }
1218+
1219+ public class Program
1220+ {
1221+ public static void Main()
1222+ {
1223+ var composition = new Composition();
1224+ var service = new Service();
1225+ composition.BuildUpService(service);
1226+
1227+ Console.WriteLine(service.Dep1 != null);
1228+ Console.WriteLine(service.Dep2 != null);
1229+ }
1230+ }
1231+ }
1232+ """ . RunAsync ( ) ;
1233+
1234+ // Then
1235+ result . Success . ShouldBeTrue ( result ) ;
1236+ result . StdOut . ShouldBe ( [ "True" , "True" ] , result ) ;
1237+ }
1238+
1239+ [ Fact ]
1240+ public async Task ShouldSupportBuilderWithDeepHierarchy ( )
1241+ {
1242+ // Given
1243+
1244+ // When
1245+ var result = await """
1246+ using System;
1247+ using Pure.DI;
1248+ #pragma warning disable CS8618
1249+
1250+ namespace Sample
1251+ {
1252+ interface IDependency1 {}
1253+ class Dependency1: IDependency1 {}
1254+
1255+ interface IDependency2 { IDependency1 Dep1 { get; } }
1256+ class Dependency2: IDependency2
1257+ {
1258+ public Dependency2(IDependency1 dep1) => Dep1 = dep1;
1259+ public IDependency1 Dep1 { get; }
1260+ }
1261+
1262+ class Service
1263+ {
1264+ [Ordinal(0)]
1265+ public IDependency2 Dep2 { get; set; }
1266+ }
1267+
1268+ static class Setup
1269+ {
1270+ private static void SetupComposition()
1271+ {
1272+ DI.Setup("Composition")
1273+ .Bind<IDependency1>().To<Dependency1>()
1274+ .Bind<IDependency2>().To<Dependency2>()
1275+ .Builder<Service>("BuildUpService");
1276+ }
1277+ }
1278+
1279+ public class Program
1280+ {
1281+ public static void Main()
1282+ {
1283+ var composition = new Composition();
1284+ var service = new Service();
1285+ composition.BuildUpService(service);
1286+
1287+ Console.WriteLine(service.Dep2 != null);
1288+ Console.WriteLine(service.Dep2!.Dep1 != null);
1289+ }
1290+ }
1291+ }
1292+ """ . RunAsync ( ) ;
1293+
1294+ // Then
1295+ result . Success . ShouldBeTrue ( result ) ;
1296+ result . StdOut . ShouldBe ( [ "True" , "True" ] , result ) ;
1297+ }
10671298}
0 commit comments