| | | 1 | | using NexusLabs.Needlr.Generators; |
| | | 2 | | using NexusLabs.Needlr.Injection.Reflection; |
| | | 3 | | using NexusLabs.Needlr.Injection.SourceGen; |
| | | 4 | | |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.Injection.Bundle; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Extension methods that provide automatic fallback between source-generated and reflection-based components. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <para> |
| | | 14 | | /// When using the Bundle package, Needlr will automatically: |
| | | 15 | | /// <list type="number"> |
| | | 16 | | /// <item>Try to use source-generated components if available (via NeedlrSourceGenBootstrap)</item> |
| | | 17 | | /// <item>Fall back to reflection-based components if source generation is not configured</item> |
| | | 18 | | /// </list> |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// Use <see cref="WithFallbackBehavior"/> to configure the fallback behavior, or |
| | | 22 | | /// <see cref="WithFastFailOnReflection"/> to throw if reflection is used. |
| | | 23 | | /// </para> |
| | | 24 | | /// </remarks> |
| | | 25 | | public static class SyringeBundleExtensions |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// Configures the syringe with automatic fallback from source-gen to reflection. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <remarks> |
| | | 31 | | /// <para> |
| | | 32 | | /// This method automatically detects whether source generation is available |
| | | 33 | | /// and configures the appropriate components. If source-generated providers |
| | | 34 | | /// are registered via NeedlrSourceGenBootstrap, they will be used. Otherwise, |
| | | 35 | | /// reflection-based components are used as fallback. |
| | | 36 | | /// </para> |
| | | 37 | | /// </remarks> |
| | | 38 | | /// <param name="syringe">The syringe to configure.</param> |
| | | 39 | | /// <returns>A configured syringe ready for further configuration and building.</returns> |
| | | 40 | | [UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", |
| | | 41 | | Justification = "Reflection components are only used when source-gen is not available.")] |
| | | 42 | | public static ConfiguredSyringe UsingAutoConfiguration(this Syringe syringe) |
| | | 43 | | { |
| | 3 | 44 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 45 | | |
| | 2 | 46 | | if (NeedlrSourceGenBootstrap.TryGetProviders(out var injectableTypeProvider, out var pluginTypeProvider)) |
| | | 47 | | { |
| | 1 | 48 | | var configured = new ConfiguredSyringe(syringe) with |
| | 1 | 49 | | { |
| | 1 | 50 | | ServiceProviderBuilderFactory = (populator, assemblyProvider, additionalAssemblies) => |
| | 0 | 51 | | new ServiceProviderBuilder(populator, assemblyProvider, additionalAssemblies) |
| | 1 | 52 | | }; |
| | 1 | 53 | | return configured.UsingGeneratedComponents(injectableTypeProvider, pluginTypeProvider); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // Fallback to reflection |
| | 1 | 57 | | var reflectionSyringe = syringe.UsingReflection(); |
| | 1 | 58 | | return reflectionSyringe with |
| | 1 | 59 | | { |
| | 1 | 60 | | ServiceProviderBuilderFactory = (populator, assemblyProvider, additionalAssemblies) => |
| | 0 | 61 | | new ServiceProviderBuilder(populator, assemblyProvider, additionalAssemblies) |
| | 1 | 62 | | }; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Configures the syringe with automatic fallback and a custom fallback handler. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="syringe">The syringe to configure.</param> |
| | | 69 | | /// <param name="onFallback">Called when reflection fallback occurs. Can be used for logging or to throw.</param> |
| | | 70 | | /// <returns>A configured syringe ready for further configuration and building.</returns> |
| | | 71 | | [UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", |
| | | 72 | | Justification = "Reflection components are only used when source-gen is not available.")] |
| | | 73 | | public static ConfiguredSyringe WithFallbackBehavior( |
| | | 74 | | this Syringe syringe, |
| | | 75 | | Action<ReflectionFallbackContext>? onFallback) |
| | | 76 | | { |
| | 5 | 77 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 78 | | |
| | 4 | 79 | | if (NeedlrSourceGenBootstrap.TryGetProviders(out var injectableTypeProvider, out var pluginTypeProvider)) |
| | | 80 | | { |
| | 3 | 81 | | var configured = new ConfiguredSyringe(syringe) with |
| | 3 | 82 | | { |
| | 3 | 83 | | ServiceProviderBuilderFactory = (populator, assemblyProvider, additionalAssemblies) => |
| | 0 | 84 | | new ServiceProviderBuilder(populator, assemblyProvider, additionalAssemblies) |
| | 3 | 85 | | }; |
| | 3 | 86 | | return configured.UsingGeneratedComponents(injectableTypeProvider, pluginTypeProvider); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | // Invoke fallback handler if provided |
| | 1 | 90 | | if (onFallback is not null) |
| | | 91 | | { |
| | 1 | 92 | | onFallback(ReflectionFallbackHandlers.CreateTypeRegistrarContext()); |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | var reflectionSyringe = syringe.UsingReflection(); |
| | 1 | 96 | | return reflectionSyringe with |
| | 1 | 97 | | { |
| | 1 | 98 | | ServiceProviderBuilderFactory = (populator, assemblyProvider, additionalAssemblies) => |
| | 0 | 99 | | new ServiceProviderBuilder(populator, assemblyProvider, additionalAssemblies) |
| | 1 | 100 | | }; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Configures the syringe to throw an exception if reflection fallback would occur. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <remarks> |
| | | 107 | | /// Use this in AOT/trimming scenarios to ensure that source-generated components are always used. |
| | | 108 | | /// If no source-generated providers are registered, an <see cref="InvalidOperationException"/> is thrown. |
| | | 109 | | /// </remarks> |
| | | 110 | | /// <param name="syringe">The syringe to configure.</param> |
| | | 111 | | /// <returns>A configured syringe ready for further configuration and building.</returns> |
| | | 112 | | public static ConfiguredSyringe WithFastFailOnReflection(this Syringe syringe) |
| | | 113 | | { |
| | 2 | 114 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 115 | | |
| | 1 | 116 | | return syringe.WithFallbackBehavior(ReflectionFallbackHandlers.ThrowException); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Configures the syringe to log warnings when reflection fallback occurs. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="syringe">The syringe to configure.</param> |
| | | 123 | | /// <returns>A configured syringe ready for further configuration and building.</returns> |
| | | 124 | | [UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", |
| | | 125 | | Justification = "Reflection components are only used when source-gen is not available.")] |
| | | 126 | | public static ConfiguredSyringe WithFallbackLogging(this Syringe syringe) |
| | | 127 | | { |
| | 2 | 128 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 129 | | |
| | 1 | 130 | | return syringe.WithFallbackBehavior(ReflectionFallbackHandlers.LogWarning); |
| | | 131 | | } |
| | | 132 | | } |