| | | 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 and registry participant identities registered via |
| | | 39 | | /// <see cref="NeedlrSourceGenBootstrap"/>. |
| | | 40 | | /// The bootstrap is automatically registered by the generated module initializer when you use |
| | | 41 | | /// <c>[assembly: GenerateTypeRegistry(...)]</c>. |
| | | 42 | | /// </para> |
| | | 43 | | /// </remarks> |
| | | 44 | | /// <param name="syringe">The syringe to configure.</param> |
| | | 45 | | /// <returns>A configured syringe ready for further configuration and building.</returns> |
| | | 46 | | /// <exception cref="InvalidOperationException"> |
| | | 47 | | /// Thrown if no source-generated type providers are registered via NeedlrSourceGenBootstrap. |
| | | 48 | | /// </exception> |
| | | 49 | | public static ConfiguredSyringe UsingSourceGen(this Syringe syringe) |
| | | 50 | | { |
| | 203 | 51 | | ArgumentNullException.ThrowIfNull(syringe); |
| | | 52 | | |
| | 203 | 53 | | if (!NeedlrSourceGenBootstrap.TryGetProviders( |
| | 203 | 54 | | out var injectableTypeProvider, |
| | 203 | 55 | | out var pluginTypeProvider, |
| | 203 | 56 | | out var registryParticipantTypes)) |
| | | 57 | | { |
| | 0 | 58 | | throw new InvalidOperationException( |
| | 0 | 59 | | "No source-generated type providers found. Ensure your assembly has " + |
| | 0 | 60 | | "[assembly: GenerateTypeRegistry(...)] and references NexusLabs.Needlr.Generators."); |
| | | 61 | | } |
| | | 62 | | |
| | 203 | 63 | | return new ConfiguredSyringe(syringe).UsingGeneratedComponents( |
| | 203 | 64 | | injectableTypeProvider, |
| | 203 | 65 | | pluginTypeProvider, |
| | 203 | 66 | | registryParticipantTypes); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Configures the syringe with all generated components for zero-reflection operation. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <remarks> |
| | | 73 | | /// This is a strategy method that creates a ConfiguredSyringe. Use this when you have |
| | | 74 | | /// explicit type providers (e.g., from generated code) rather than using the bootstrap. |
| | | 75 | | /// Assemblies are derived only from the supplied injectable and plugin metadata; use the |
| | | 76 | | /// overload with registry participant types when empty registries must remain visible. |
| | | 77 | | /// </remarks> |
| | | 78 | | /// <param name="syringe">The base syringe to configure.</param> |
| | | 79 | | /// <param name="injectableTypeProvider">A function that returns the injectable types.</param> |
| | | 80 | | /// <param name="pluginTypeProvider">A function that returns the plugin types.</param> |
| | | 81 | | /// <returns>A configured syringe with all source-generated components.</returns> |
| | | 82 | | public static ConfiguredSyringe UsingGeneratedComponents( |
| | | 83 | | this Syringe syringe, |
| | | 84 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 85 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 86 | | { |
| | 121 | 87 | | return syringe.UsingGeneratedComponents( |
| | 121 | 88 | | injectableTypeProvider, |
| | 121 | 89 | | pluginTypeProvider, |
| | 121 | 90 | | Array.Empty<Type>()); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Configures the syringe with all generated components and registry participant identities. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="syringe">The base syringe to configure.</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 | | /// <param name="registryParticipantTypes"> |
| | | 100 | | /// Generated types whose assemblies identify every TypeRegistry participant. |
| | | 101 | | /// </param> |
| | | 102 | | /// <returns>A configured syringe with all source-generated components.</returns> |
| | | 103 | | public static ConfiguredSyringe UsingGeneratedComponents( |
| | | 104 | | this Syringe syringe, |
| | | 105 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 106 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | | 107 | | IReadOnlyList<Type> registryParticipantTypes) |
| | | 108 | | { |
| | 121 | 109 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 121 | 110 | | ArgumentNullException.ThrowIfNull(injectableTypeProvider); |
| | 121 | 111 | | ArgumentNullException.ThrowIfNull(pluginTypeProvider); |
| | 121 | 112 | | ArgumentNullException.ThrowIfNull(registryParticipantTypes); |
| | | 113 | | |
| | 121 | 114 | | return new ConfiguredSyringe(syringe) with |
| | 121 | 115 | | { |
| | 121 | 116 | | TypeRegistrar = new GeneratedTypeRegistrar(injectableTypeProvider), |
| | 121 | 117 | | TypeFilterer = new GeneratedTypeFilterer(injectableTypeProvider), |
| | 121 | 118 | | PluginFactory = new GeneratedPluginFactory(pluginTypeProvider), |
| | 121 | 119 | | AssemblyProvider = new GeneratedAssemblyProvider( |
| | 121 | 120 | | injectableTypeProvider, |
| | 121 | 121 | | pluginTypeProvider, |
| | 121 | 122 | | registryParticipantTypes), |
| | 121 | 123 | | ServiceProviderBuilderFactory = (populator, assemblyProvider, _) => |
| | 121 | 124 | | new GeneratedServiceProviderBuilder(populator, assemblyProvider, pluginTypeProvider) |
| | 121 | 125 | | }; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Configures the syringe with all generated components for zero-reflection operation. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <remarks> |
| | | 132 | | /// Assemblies are derived only from the supplied injectable and plugin metadata; use the |
| | | 133 | | /// overload with registry participant types when empty registries must remain visible. |
| | | 134 | | /// </remarks> |
| | | 135 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 136 | | /// <param name="injectableTypeProvider">A function that returns the injectable types.</param> |
| | | 137 | | /// <param name="pluginTypeProvider">A function that returns the plugin types.</param> |
| | | 138 | | /// <returns>A configured syringe with all source-generated components.</returns> |
| | | 139 | | public static ConfiguredSyringe UsingGeneratedComponents( |
| | | 140 | | this ConfiguredSyringe syringe, |
| | | 141 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 142 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 143 | | { |
| | 0 | 144 | | return syringe.UsingGeneratedComponents( |
| | 0 | 145 | | injectableTypeProvider, |
| | 0 | 146 | | pluginTypeProvider, |
| | 0 | 147 | | Array.Empty<Type>()); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Configures the syringe with all generated components and registry participant identities. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 154 | | /// <param name="injectableTypeProvider">A function that returns the injectable types.</param> |
| | | 155 | | /// <param name="pluginTypeProvider">A function that returns the plugin types.</param> |
| | | 156 | | /// <param name="registryParticipantTypes"> |
| | | 157 | | /// Generated types whose assemblies identify every TypeRegistry participant. |
| | | 158 | | /// </param> |
| | | 159 | | /// <returns>A configured syringe with all source-generated components.</returns> |
| | | 160 | | public static ConfiguredSyringe UsingGeneratedComponents( |
| | | 161 | | this ConfiguredSyringe syringe, |
| | | 162 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 163 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | | 164 | | IReadOnlyList<Type> registryParticipantTypes) |
| | | 165 | | { |
| | 208 | 166 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 208 | 167 | | ArgumentNullException.ThrowIfNull(injectableTypeProvider); |
| | 208 | 168 | | ArgumentNullException.ThrowIfNull(pluginTypeProvider); |
| | 208 | 169 | | ArgumentNullException.ThrowIfNull(registryParticipantTypes); |
| | | 170 | | |
| | 208 | 171 | | return syringe with |
| | 208 | 172 | | { |
| | 208 | 173 | | TypeRegistrar = new GeneratedTypeRegistrar(injectableTypeProvider), |
| | 208 | 174 | | TypeFilterer = new GeneratedTypeFilterer(injectableTypeProvider), |
| | 208 | 175 | | PluginFactory = new GeneratedPluginFactory(pluginTypeProvider), |
| | 208 | 176 | | AssemblyProvider = new GeneratedAssemblyProvider( |
| | 208 | 177 | | injectableTypeProvider, |
| | 208 | 178 | | pluginTypeProvider, |
| | 208 | 179 | | registryParticipantTypes), |
| | 208 | 180 | | ServiceProviderBuilderFactory = (populator, assemblyProvider, _) => |
| | 191 | 181 | | new GeneratedServiceProviderBuilder(populator, assemblyProvider, pluginTypeProvider) |
| | 208 | 182 | | }; |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Configures the syringe to use the generated type registrar. |
| | | 187 | | /// </summary> |
| | | 188 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 189 | | /// <param name="typeProvider">A function that returns the injectable types.</param> |
| | | 190 | | /// <returns>A new configured syringe instance.</returns> |
| | | 191 | | public static ConfiguredSyringe UsingGeneratedTypeRegistrar( |
| | | 192 | | this ConfiguredSyringe syringe, |
| | | 193 | | Func<IReadOnlyList<InjectableTypeInfo>> typeProvider) |
| | | 194 | | { |
| | 0 | 195 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 196 | | ArgumentNullException.ThrowIfNull(typeProvider); |
| | 0 | 197 | | return syringe.UsingTypeRegistrar(new GeneratedTypeRegistrar(typeProvider)); |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Configures the syringe to use the generated type filterer. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 204 | | /// <param name="typeProvider">A function that returns the injectable types.</param> |
| | | 205 | | /// <returns>A new configured syringe instance.</returns> |
| | | 206 | | public static ConfiguredSyringe UsingGeneratedTypeFilterer( |
| | | 207 | | this ConfiguredSyringe syringe, |
| | | 208 | | Func<IReadOnlyList<InjectableTypeInfo>> typeProvider) |
| | | 209 | | { |
| | 0 | 210 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 211 | | ArgumentNullException.ThrowIfNull(typeProvider); |
| | 0 | 212 | | return syringe.UsingTypeFilterer(new GeneratedTypeFilterer(typeProvider)); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Configures the syringe to use the generated plugin factory. |
| | | 217 | | /// </summary> |
| | | 218 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 219 | | /// <param name="pluginProvider">A function that returns the plugin types.</param> |
| | | 220 | | /// <returns>A new configured syringe instance.</returns> |
| | | 221 | | public static ConfiguredSyringe UsingGeneratedPluginFactory( |
| | | 222 | | this ConfiguredSyringe syringe, |
| | | 223 | | Func<IReadOnlyList<PluginTypeInfo>> pluginProvider) |
| | | 224 | | { |
| | 0 | 225 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 226 | | ArgumentNullException.ThrowIfNull(pluginProvider); |
| | 0 | 227 | | return syringe.UsingPluginFactory(new GeneratedPluginFactory(pluginProvider)); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Configures the syringe to use the generated assembly provider. |
| | | 232 | | /// </summary> |
| | | 233 | | /// <remarks> |
| | | 234 | | /// Assemblies are derived only from the supplied injectable and plugin metadata; use the |
| | | 235 | | /// overload with registry participant types when empty registries must remain visible. |
| | | 236 | | /// </remarks> |
| | | 237 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 238 | | /// <param name="injectableTypeProvider">A function that returns the injectable types.</param> |
| | | 239 | | /// <param name="pluginTypeProvider">A function that returns the plugin types.</param> |
| | | 240 | | /// <returns>A new configured syringe instance.</returns> |
| | | 241 | | public static ConfiguredSyringe UsingGeneratedAssemblyProvider( |
| | | 242 | | this ConfiguredSyringe syringe, |
| | | 243 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 244 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider) |
| | | 245 | | { |
| | 0 | 246 | | return syringe.UsingGeneratedAssemblyProvider( |
| | 0 | 247 | | injectableTypeProvider, |
| | 0 | 248 | | pluginTypeProvider, |
| | 0 | 249 | | Array.Empty<Type>()); |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Configures the syringe to use the generated assembly provider with registry participant identities. |
| | | 254 | | /// </summary> |
| | | 255 | | /// <param name="syringe">The configured syringe to update.</param> |
| | | 256 | | /// <param name="injectableTypeProvider">A function that returns the injectable types.</param> |
| | | 257 | | /// <param name="pluginTypeProvider">A function that returns the plugin types.</param> |
| | | 258 | | /// <param name="registryParticipantTypes"> |
| | | 259 | | /// Generated types whose assemblies identify every TypeRegistry participant. |
| | | 260 | | /// </param> |
| | | 261 | | /// <returns>A new configured syringe instance.</returns> |
| | | 262 | | public static ConfiguredSyringe UsingGeneratedAssemblyProvider( |
| | | 263 | | this ConfiguredSyringe syringe, |
| | | 264 | | Func<IReadOnlyList<InjectableTypeInfo>> injectableTypeProvider, |
| | | 265 | | Func<IReadOnlyList<PluginTypeInfo>> pluginTypeProvider, |
| | | 266 | | IReadOnlyList<Type> registryParticipantTypes) |
| | | 267 | | { |
| | 0 | 268 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 269 | | ArgumentNullException.ThrowIfNull(injectableTypeProvider); |
| | 0 | 270 | | ArgumentNullException.ThrowIfNull(pluginTypeProvider); |
| | 0 | 271 | | ArgumentNullException.ThrowIfNull(registryParticipantTypes); |
| | 0 | 272 | | return syringe.UsingAssemblyProvider( |
| | 0 | 273 | | new GeneratedAssemblyProvider( |
| | 0 | 274 | | injectableTypeProvider, |
| | 0 | 275 | | pluginTypeProvider, |
| | 0 | 276 | | registryParticipantTypes)); |
| | | 277 | | } |
| | | 278 | | } |