< 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
61%
Covered lines: 27
Uncovered lines: 17
Coverable lines: 44
Total lines: 183
Line coverage: 61.3%
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 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    {
 17350        ArgumentNullException.ThrowIfNull(syringe);
 51
 17352        if (!NeedlrSourceGenBootstrap.TryGetProviders(out var injectableTypeProvider, out var pluginTypeProvider))
 53        {
 054            throw new InvalidOperationException(
 055                "No source-generated type providers found. Ensure your assembly has " +
 056                "[assembly: GenerateTypeRegistry(...)] and references NexusLabs.Needlr.Generators.");
 57        }
 58
 17359        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    {
 8478        ArgumentNullException.ThrowIfNull(syringe);
 8479        ArgumentNullException.ThrowIfNull(injectableTypeProvider);
 8480        ArgumentNullException.ThrowIfNull(pluginTypeProvider);
 81
 8482        return new ConfiguredSyringe(syringe) with
 8483        {
 8484            TypeRegistrar = new GeneratedTypeRegistrar(injectableTypeProvider),
 8485            TypeFilterer = new GeneratedTypeFilterer(injectableTypeProvider),
 8486            PluginFactory = new GeneratedPluginFactory(pluginTypeProvider),
 8487            AssemblyProvider = new GeneratedAssemblyProvider(injectableTypeProvider, pluginTypeProvider),
 8488            ServiceProviderBuilderFactory = (populator, assemblyProvider, _) =>
 8489                new GeneratedServiceProviderBuilder(populator, assemblyProvider, pluginTypeProvider)
 8490        };
 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    {
 177105        ArgumentNullException.ThrowIfNull(syringe);
 177106        ArgumentNullException.ThrowIfNull(injectableTypeProvider);
 177107        ArgumentNullException.ThrowIfNull(pluginTypeProvider);
 108
 177109        return syringe with
 177110        {
 177111            TypeRegistrar = new GeneratedTypeRegistrar(injectableTypeProvider),
 177112            TypeFilterer = new GeneratedTypeFilterer(injectableTypeProvider),
 177113            PluginFactory = new GeneratedPluginFactory(pluginTypeProvider),
 177114            AssemblyProvider = new GeneratedAssemblyProvider(injectableTypeProvider, pluginTypeProvider),
 177115            ServiceProviderBuilderFactory = (populator, assemblyProvider, _) =>
 161116                new GeneratedServiceProviderBuilder(populator, assemblyProvider, pluginTypeProvider)
 177117        };
 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    {
 0130        ArgumentNullException.ThrowIfNull(syringe);
 0131        ArgumentNullException.ThrowIfNull(typeProvider);
 0132        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    {
 0145        ArgumentNullException.ThrowIfNull(syringe);
 0146        ArgumentNullException.ThrowIfNull(typeProvider);
 0147        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    {
 0160        ArgumentNullException.ThrowIfNull(syringe);
 0161        ArgumentNullException.ThrowIfNull(pluginProvider);
 0162        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    {
 0177        ArgumentNullException.ThrowIfNull(syringe);
 0178        ArgumentNullException.ThrowIfNull(injectableTypeProvider);
 0179        ArgumentNullException.ThrowIfNull(pluginTypeProvider);
 0180        return syringe.UsingAssemblyProvider(
 0181            new GeneratedAssemblyProvider(injectableTypeProvider, pluginTypeProvider));
 182    }
 183}