| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.Generators; |
| | | 5 | | using NexusLabs.Needlr.Injection.SourceGen.Loaders; |
| | | 6 | | using NexusLabs.Needlr.Injection.SourceGen.PluginFactories; |
| | | 7 | | using NexusLabs.Needlr.Injection.SourceGen.TypeFilterers; |
| | | 8 | | using NexusLabs.Needlr.Injection.SourceGen.TypeRegistrars; |
| | | 9 | | |
| | | 10 | | namespace NexusLabs.Needlr.Injection.SourceGen; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Extension methods for configuring <see cref="Syringe"/> with source-generated components. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <para> |
| | | 17 | | /// These extensions enable AOT-compatible, zero-reflection type discovery and registration |
| | | 18 | | /// using compile-time generated type registries. |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// To use these extensions, your assembly must have: |
| | | 22 | | /// <list type="bullet"> |
| | | 23 | | /// <item>A reference to <c>NexusLabs.Needlr.Generators</c></item> |
| | | 24 | | /// <item>The <c>[assembly: GenerateTypeRegistry(...)]</c> attribute</item> |
| | | 25 | | /// </list> |
| | | 26 | | /// </para> |
| | | 27 | | /// <para> |
| | | 28 | | /// For assembly ordering, use <c>SyringeExtensions.OrderAssemblies</c> after calling <c>UsingSourceGen()</c>. |
| | | 29 | | /// </para> |
| | | 30 | | /// </remarks> |
| | | 31 | | public static class SyringeSourceGenExtensions |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// Configures the syringe to use source-generated components from the module initializer bootstrap. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <remarks> |
| | | 37 | | /// <para> |
| | | 38 | | /// This method uses the type providers registered via <see cref="NeedlrSourceGenBootstrap"/>. |
| | | 39 | | /// The bootstrap is automatically registered by the generated module initializer when you use |
| | | 40 | | /// <c>[assembly: GenerateTypeRegistry(...)]</c>. |
| | | 41 | | /// </para> |
| | | 42 | | /// </remarks> |
| | | 43 | | /// <param name="syringe">The syringe to configure.</param> |
| | | 44 | | /// <returns>A configured syringe ready for further configuration and building.</returns> |
| | | 45 | | /// <exception cref="InvalidOperationException"> |
| | | 46 | | /// Thrown if no source-generated type providers are registered via NeedlrSourceGenBootstrap. |
| | | 47 | | /// </exception> |
| | | 48 | | public static ConfiguredSyringe UsingSourceGen(this Syringe syringe) |
| | | 49 | | { |
| | 173 | 50 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 51 | | |
| | 173 | 52 | | if (!NeedlrSourceGenBootstrap.TryGetProviders(out var injectableTypeProvider, out var pluginTypeProvider)) |
| | | 53 | | { |
| | 0 | 54 | | throw new InvalidOperationException( |
| | 0 | 55 | | "No source-generated type providers found. Ensure your assembly has " + |
| | 0 | 56 | | "[assembly: GenerateTypeRegistry(...)] and references NexusLabs.Needlr.Generators."); |
| | | 57 | | } |
| | | 58 | | |
| | 173 | 59 | | return new ConfiguredSyringe(syringe).UsingGeneratedComponents(injectableTypeProvider, pluginTypeProvider); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Configures the syringe with all generated components for zero-reflection operation. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <remarks> |
| | | 66 | | /// This is a strategy method that creates a ConfiguredSyringe. Use this when you have |
| | | 67 | | /// explicit type providers (e.g., from generated code) rather than using the bootstrap. |
| | | 68 | | /// </remarks> |
| | | 69 | | /// <param name="syringe">The base syringe to configure.</param> |
| | | 70 | | /// <param name="injectableTypeProvider">A function that returns the injectable types.</param> |
| | | 71 | | /// <param name="pluginTypeProvider">A function that returns the plugin types.</param> |
| | | 72 | | /// <returns>A configured syringe with all source-generated components.</returns> |
| | | 73 | | public static ConfiguredSyringe UsingGeneratedComponents( |
| | | 74 | | this Syringe syringe, |
| | | 75 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 76 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 77 | | { |
| | 84 | 78 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 84 | 79 | | ArgumentNullException.ThrowIfNull(injectableTypeProvider); |
| | 84 | 80 | | ArgumentNullException.ThrowIfNull(pluginTypeProvider); |
| | | 81 | | |
| | 84 | 82 | | return new ConfiguredSyringe(syringe) with |
| | 84 | 83 | | { |
| | 84 | 84 | | TypeRegistrar = new GeneratedTypeRegistrar(injectableTypeProvider), |
| | 84 | 85 | | TypeFilterer = new GeneratedTypeFilterer(injectableTypeProvider), |
| | 84 | 86 | | PluginFactory = new GeneratedPluginFactory(pluginTypeProvider), |
| | 84 | 87 | | AssemblyProvider = new GeneratedAssemblyProvider(injectableTypeProvider, pluginTypeProvider), |
| | 84 | 88 | | ServiceProviderBuilderFactory = (populator, assemblyProvider, _) => |
| | 84 | 89 | | new GeneratedServiceProviderBuilder(populator, assemblyProvider, pluginTypeProvider) |
| | 84 | 90 | | }; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Configures the syringe with all generated components for zero-reflection operation. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 97 | | /// <param name="injectableTypeProvider">A function that returns the injectable types.</param> |
| | | 98 | | /// <param name="pluginTypeProvider">A function that returns the plugin types.</param> |
| | | 99 | | /// <returns>A configured syringe with all source-generated components.</returns> |
| | | 100 | | public static ConfiguredSyringe UsingGeneratedComponents( |
| | | 101 | | this ConfiguredSyringe syringe, |
| | | 102 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 103 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 104 | | { |
| | 177 | 105 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 177 | 106 | | ArgumentNullException.ThrowIfNull(injectableTypeProvider); |
| | 177 | 107 | | ArgumentNullException.ThrowIfNull(pluginTypeProvider); |
| | | 108 | | |
| | 177 | 109 | | return syringe with |
| | 177 | 110 | | { |
| | 177 | 111 | | TypeRegistrar = new GeneratedTypeRegistrar(injectableTypeProvider), |
| | 177 | 112 | | TypeFilterer = new GeneratedTypeFilterer(injectableTypeProvider), |
| | 177 | 113 | | PluginFactory = new GeneratedPluginFactory(pluginTypeProvider), |
| | 177 | 114 | | AssemblyProvider = new GeneratedAssemblyProvider(injectableTypeProvider, pluginTypeProvider), |
| | 177 | 115 | | ServiceProviderBuilderFactory = (populator, assemblyProvider, _) => |
| | 161 | 116 | | new GeneratedServiceProviderBuilder(populator, assemblyProvider, pluginTypeProvider) |
| | 177 | 117 | | }; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Configures the syringe to use the generated type registrar. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 124 | | /// <param name="typeProvider">A function that returns the injectable types.</param> |
| | | 125 | | /// <returns>A new configured syringe instance.</returns> |
| | | 126 | | public static ConfiguredSyringe UsingGeneratedTypeRegistrar( |
| | | 127 | | this ConfiguredSyringe syringe, |
| | | 128 | | Func<IReadOnlyList<InjectableTypeInfo>> typeProvider) |
| | | 129 | | { |
| | 0 | 130 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 131 | | ArgumentNullException.ThrowIfNull(typeProvider); |
| | 0 | 132 | | return syringe.UsingTypeRegistrar(new GeneratedTypeRegistrar(typeProvider)); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Configures the syringe to use the generated type filterer. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 139 | | /// <param name="typeProvider">A function that returns the injectable types.</param> |
| | | 140 | | /// <returns>A new configured syringe instance.</returns> |
| | | 141 | | public static ConfiguredSyringe UsingGeneratedTypeFilterer( |
| | | 142 | | this ConfiguredSyringe syringe, |
| | | 143 | | Func<IReadOnlyList<InjectableTypeInfo>> typeProvider) |
| | | 144 | | { |
| | 0 | 145 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 146 | | ArgumentNullException.ThrowIfNull(typeProvider); |
| | 0 | 147 | | return syringe.UsingTypeFilterer(new GeneratedTypeFilterer(typeProvider)); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Configures the syringe to use the generated plugin factory. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 154 | | /// <param name="pluginProvider">A function that returns the plugin types.</param> |
| | | 155 | | /// <returns>A new configured syringe instance.</returns> |
| | | 156 | | public static ConfiguredSyringe UsingGeneratedPluginFactory( |
| | | 157 | | this ConfiguredSyringe syringe, |
| | | 158 | | Func<IReadOnlyList<PluginTypeInfo>> pluginProvider) |
| | | 159 | | { |
| | 0 | 160 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 161 | | ArgumentNullException.ThrowIfNull(pluginProvider); |
| | 0 | 162 | | return syringe.UsingPluginFactory(new GeneratedPluginFactory(pluginProvider)); |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Configures the syringe to use the generated assembly provider. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 169 | | /// <param name="injectableTypeProvider">A function that returns the injectable types.</param> |
| | | 170 | | /// <param name="pluginTypeProvider">A function that returns the plugin types.</param> |
| | | 171 | | /// <returns>A new configured syringe instance.</returns> |
| | | 172 | | public static ConfiguredSyringe UsingGeneratedAssemblyProvider( |
| | | 173 | | this ConfiguredSyringe syringe, |
| | | 174 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 175 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 176 | | { |
| | 0 | 177 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 178 | | ArgumentNullException.ThrowIfNull(injectableTypeProvider); |
| | 0 | 179 | | ArgumentNullException.ThrowIfNull(pluginTypeProvider); |
| | 0 | 180 | | return syringe.UsingAssemblyProvider( |
| | 0 | 181 | | new GeneratedAssemblyProvider(injectableTypeProvider, pluginTypeProvider)); |
| | | 182 | | } |
| | | 183 | | } |