< Summary

Information
Class: NexusLabs.Needlr.Injection.SourceGen.SyringeSourceGenExtensions
Assembly: NexusLabs.Needlr.Injection.SourceGen
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.SourceGen/SyringeSourceGenExtensions.cs
Line coverage
60%
Covered lines: 45
Uncovered lines: 29
Coverable lines: 74
Total lines: 278
Line coverage: 60.8%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.SourceGen/SyringeSourceGenExtensions.cs

#LineLine coverage
 1using Microsoft.Extensions.Configuration;
 2using Microsoft.Extensions.DependencyInjection;
 3
 4using NexusLabs.Needlr.Generators;
 5using NexusLabs.Needlr.Injection.SourceGen.Loaders;
 6using NexusLabs.Needlr.Injection.SourceGen.PluginFactories;
 7using NexusLabs.Needlr.Injection.SourceGen.TypeFilterers;
 8using NexusLabs.Needlr.Injection.SourceGen.TypeRegistrars;
 9
 10namespace 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>
 31public 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    {
 20351        ArgumentNullException.ThrowIfNull(syringe);
 52
 20353        if (!NeedlrSourceGenBootstrap.TryGetProviders(
 20354            out var injectableTypeProvider,
 20355            out var pluginTypeProvider,
 20356            out var registryParticipantTypes))
 57        {
 058            throw new InvalidOperationException(
 059                "No source-generated type providers found. Ensure your assembly has " +
 060                "[assembly: GenerateTypeRegistry(...)] and references NexusLabs.Needlr.Generators.");
 61        }
 62
 20363        return new ConfiguredSyringe(syringe).UsingGeneratedComponents(
 20364            injectableTypeProvider,
 20365            pluginTypeProvider,
 20366            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    {
 12187        return syringe.UsingGeneratedComponents(
 12188            injectableTypeProvider,
 12189            pluginTypeProvider,
 12190            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    {
 121109        ArgumentNullException.ThrowIfNull(syringe);
 121110        ArgumentNullException.ThrowIfNull(injectableTypeProvider);
 121111        ArgumentNullException.ThrowIfNull(pluginTypeProvider);
 121112        ArgumentNullException.ThrowIfNull(registryParticipantTypes);
 113
 121114        return new ConfiguredSyringe(syringe) with
 121115        {
 121116            TypeRegistrar = new GeneratedTypeRegistrar(injectableTypeProvider),
 121117            TypeFilterer = new GeneratedTypeFilterer(injectableTypeProvider),
 121118            PluginFactory = new GeneratedPluginFactory(pluginTypeProvider),
 121119            AssemblyProvider = new GeneratedAssemblyProvider(
 121120                injectableTypeProvider,
 121121                pluginTypeProvider,
 121122                registryParticipantTypes),
 121123            ServiceProviderBuilderFactory = (populator, assemblyProvider, _) =>
 121124                new GeneratedServiceProviderBuilder(populator, assemblyProvider, pluginTypeProvider)
 121125        };
 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    {
 0144        return syringe.UsingGeneratedComponents(
 0145            injectableTypeProvider,
 0146            pluginTypeProvider,
 0147            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    {
 208166        ArgumentNullException.ThrowIfNull(syringe);
 208167        ArgumentNullException.ThrowIfNull(injectableTypeProvider);
 208168        ArgumentNullException.ThrowIfNull(pluginTypeProvider);
 208169        ArgumentNullException.ThrowIfNull(registryParticipantTypes);
 170
 208171        return syringe with
 208172        {
 208173            TypeRegistrar = new GeneratedTypeRegistrar(injectableTypeProvider),
 208174            TypeFilterer = new GeneratedTypeFilterer(injectableTypeProvider),
 208175            PluginFactory = new GeneratedPluginFactory(pluginTypeProvider),
 208176            AssemblyProvider = new GeneratedAssemblyProvider(
 208177                injectableTypeProvider,
 208178                pluginTypeProvider,
 208179                registryParticipantTypes),
 208180            ServiceProviderBuilderFactory = (populator, assemblyProvider, _) =>
 191181                new GeneratedServiceProviderBuilder(populator, assemblyProvider, pluginTypeProvider)
 208182        };
 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    {
 0195        ArgumentNullException.ThrowIfNull(syringe);
 0196        ArgumentNullException.ThrowIfNull(typeProvider);
 0197        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    {
 0210        ArgumentNullException.ThrowIfNull(syringe);
 0211        ArgumentNullException.ThrowIfNull(typeProvider);
 0212        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    {
 0225        ArgumentNullException.ThrowIfNull(syringe);
 0226        ArgumentNullException.ThrowIfNull(pluginProvider);
 0227        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    {
 0246        return syringe.UsingGeneratedAssemblyProvider(
 0247            injectableTypeProvider,
 0248            pluginTypeProvider,
 0249            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    {
 0268        ArgumentNullException.ThrowIfNull(syringe);
 0269        ArgumentNullException.ThrowIfNull(injectableTypeProvider);
 0270        ArgumentNullException.ThrowIfNull(pluginTypeProvider);
 0271        ArgumentNullException.ThrowIfNull(registryParticipantTypes);
 0272        return syringe.UsingAssemblyProvider(
 0273            new GeneratedAssemblyProvider(
 0274                injectableTypeProvider,
 0275                pluginTypeProvider,
 0276                registryParticipantTypes));
 277    }
 278}

Methods/Properties

UsingSourceGen(NexusLabs.Needlr.Injection.Syringe)
UsingGeneratedComponents(NexusLabs.Needlr.Injection.Syringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.InjectableTypeInfo>>,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.PluginTypeInfo>>)
UsingGeneratedComponents(NexusLabs.Needlr.Injection.Syringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.InjectableTypeInfo>>,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.PluginTypeInfo>>,System.Collections.Generic.IReadOnlyList`1<System.Type>)
UsingGeneratedComponents(NexusLabs.Needlr.Injection.ConfiguredSyringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.InjectableTypeInfo>>,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.PluginTypeInfo>>)
UsingGeneratedComponents(NexusLabs.Needlr.Injection.ConfiguredSyringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.InjectableTypeInfo>>,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.PluginTypeInfo>>,System.Collections.Generic.IReadOnlyList`1<System.Type>)
UsingGeneratedTypeRegistrar(NexusLabs.Needlr.Injection.ConfiguredSyringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.InjectableTypeInfo>>)
UsingGeneratedTypeFilterer(NexusLabs.Needlr.Injection.ConfiguredSyringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.InjectableTypeInfo>>)
UsingGeneratedPluginFactory(NexusLabs.Needlr.Injection.ConfiguredSyringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.PluginTypeInfo>>)
UsingGeneratedAssemblyProvider(NexusLabs.Needlr.Injection.ConfiguredSyringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.InjectableTypeInfo>>,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.PluginTypeInfo>>)
UsingGeneratedAssemblyProvider(NexusLabs.Needlr.Injection.ConfiguredSyringe,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.InjectableTypeInfo>>,System.Func`1<System.Collections.Generic.IReadOnlyList`1<NexusLabs.Needlr.Generators.PluginTypeInfo>>,System.Collections.Generic.IReadOnlyList`1<System.Type>)