@@ -257,6 +257,78 @@ public static void Register(IServiceCollection services)
257
257
. ScrubLinesContaining ( "GeneratedCodeAttribute" ) ;
258
258
}
259
259
260
+ [ Fact ]
261
+ public Task GenerateRegisterServicesInvalidMethod ( )
262
+ {
263
+ var source = @"
264
+ using Injectio.Attributes;
265
+ using Microsoft.Extensions.DependencyInjection;
266
+ using Microsoft.Extensions.DependencyInjection.Extensions;
267
+
268
+ namespace Injectio.Sample;
269
+
270
+ public interface IModuleService
271
+ {
272
+ }
273
+
274
+ public class ModuleService : IModuleService
275
+ {
276
+ }
277
+
278
+ public static class RegistrationModule
279
+ {
280
+ [RegisterServices]
281
+ public static void Register(IServiceCollection services, string test)
282
+ {
283
+ services.TryAddTransient<IModuleService, ModuleService>();
284
+ }
285
+ }
286
+ " ;
287
+
288
+ var ( diagnostics , output ) = GetGeneratedOutput < ServiceRegistrationGenerator > ( source ) ;
289
+
290
+ diagnostics . Should ( ) . NotBeEmpty ( ) ;
291
+ diagnostics [ 0 ] . Id . Should ( ) . Be ( "SD0010" ) ;
292
+
293
+ return Task . CompletedTask ;
294
+ }
295
+
296
+ [ Fact ]
297
+ public Task GenerateRegisterServicesInvalidService ( )
298
+ {
299
+ var source = @"
300
+ using Injectio.Attributes;
301
+ using Microsoft.Extensions.DependencyInjection;
302
+ using Microsoft.Extensions.DependencyInjection.Extensions;
303
+
304
+ namespace Injectio.Sample;
305
+
306
+ public interface IModuleService
307
+ {
308
+ }
309
+
310
+ public class ModuleService : IModuleService
311
+ {
312
+ }
313
+
314
+ public static class RegistrationModule
315
+ {
316
+ [RegisterServices]
317
+ public static void Register(string test)
318
+ {
319
+ services.TryAddTransient<IModuleService, ModuleService>();
320
+ }
321
+ }
322
+ " ;
323
+
324
+ var ( diagnostics , output ) = GetGeneratedOutput < ServiceRegistrationGenerator > ( source ) ;
325
+
326
+ diagnostics . Should ( ) . NotBeEmpty ( ) ;
327
+ diagnostics [ 0 ] . Id . Should ( ) . Be ( "SD0011" ) ;
328
+
329
+ return Task . CompletedTask ;
330
+ }
331
+
260
332
[ Fact ]
261
333
public Task GenerateRegisterSingletonFactory ( )
262
334
{
@@ -351,6 +423,33 @@ public class ServiceTag : IServiceTag
351
423
. ScrubLinesContaining ( "GeneratedCodeAttribute" ) ;
352
424
}
353
425
426
+ #if NET7_0_OR_GREATER
427
+ [ Fact ]
428
+ public Task GenerateRegisterSingletonGeneric ( )
429
+ {
430
+ var source = @"
431
+ using Injectio.Attributes;
432
+
433
+ namespace Injectio.Sample;
434
+
435
+ public interface IService { }
436
+
437
+ [RegisterSingleton<IService, SingletonService>(Duplicate = DuplicateStrategy.Replace)]
438
+ public class SingletonService : IService
439
+ { }
440
+ " ;
441
+
442
+ var ( diagnostics , output ) = GetGeneratedOutput < ServiceRegistrationGenerator > ( source ) ;
443
+
444
+ diagnostics . Should ( ) . BeEmpty ( ) ;
445
+
446
+ return Verifier
447
+ . Verify ( output )
448
+ . UseDirectory ( "Snapshots" )
449
+ . ScrubLinesContaining ( "GeneratedCodeAttribute" ) ;
450
+ }
451
+ #endif
452
+
354
453
[ Fact ]
355
454
public Task GenerateRegisterSingletonServiceKeys ( )
356
455
{
@@ -361,24 +460,24 @@ public Task GenerateRegisterSingletonServiceKeys()
361
460
362
461
namespace Injectio.Sample;
363
462
364
- [RegisterSingleton<IServiceKeyed>( ServiceKey = ""Alpha"")]
463
+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Alpha"")]
365
464
public class ServiceAlphaKeyed : IServiceKeyed
366
465
{ }
367
466
368
- [RegisterSingleton<IServiceKeyed>( ServiceKey = ""Beta"")]
467
+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Beta"")]
369
468
public class ServiceBetaKeyed : IServiceKeyed
370
469
{ }
371
470
372
- [RegisterSingleton<IServiceKeyed>( ServiceKey = ServiceType.Alpha)]
471
+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ServiceType.Alpha)]
373
472
public class ServiceAlphaTypeKeyed : IServiceKeyed
374
473
{ }
375
474
376
- [RegisterSingleton<IServiceKeyed>( ServiceKey = ServiceType.Beta)]
475
+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ServiceType.Beta)]
377
476
public class ServiceBetaTypeKeyed : IServiceKeyed
378
477
{ }
379
478
380
- [RegisterSingleton<IServiceKeyed>( ServiceKey = ""Charlie"", Factory = nameof(ServiceFactory))]
381
- [RegisterSingleton<IServiceKeyed>( ServiceKey = ""Delta"", Factory = nameof(ServiceFactory))]
479
+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Charlie"", Factory = nameof(ServiceFactory))]
480
+ [RegisterSingleton(ServiceType = typeof(IServiceKeyed), ServiceKey = ""Delta"", Factory = nameof(ServiceFactory))]
382
481
public class ServiceFactoryKeyed : IServiceKeyed
383
482
{
384
483
public ServiceFactoryKeyed(object? serviceKey)
@@ -415,7 +514,6 @@ public enum ServiceType
415
514
}
416
515
417
516
418
-
419
517
private static ( ImmutableArray < Diagnostic > Diagnostics , string Output ) GetGeneratedOutput < T > ( string source )
420
518
where T : IIncrementalGenerator , new ( )
421
519
{
0 commit comments