< Summary

Information
Class: NexusLabs.Needlr.SourceGenRegistry
Assembly: NexusLabs.Needlr
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr/SourceGenRegistry.cs
Line coverage
52%
Covered lines: 19
Uncovered lines: 17
Coverable lines: 36
Total lines: 119
Line coverage: 52.7%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
RegisterOptionsRegistrar(...)50%3245.45%
RegisterExtension(...)50%3245.45%
TryGetOptionsRegistrar(...)100%11100%
TryGetExtensionRegistrar(...)100%11100%
Clear()100%210%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr/SourceGenRegistry.cs

#LineLine coverage
 1// Copyright (c) NexusLabs. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5
 6namespace NexusLabs.Needlr;
 7
 8/// <summary>
 9/// Static registry for source-generated registrations.
 10/// </summary>
 11/// <remarks>
 12/// <para>
 13/// This class provides a decoupling layer between the core Needlr runtime and the
 14/// source-generated bootstrap code. The source generator emits a module initializer
 15/// that registers callbacks here, and the <c>ConfiguredSyringe</c> calls these
 16/// callbacks during <c>BuildServiceProvider</c>.
 17/// </para>
 18/// <para>
 19/// This allows <c>NexusLabs.Needlr.Injection</c> to have no dependency on
 20/// <c>NexusLabs.Needlr.Generators.Attributes</c>.
 21/// </para>
 22/// </remarks>
 23public static class SourceGenRegistry
 24{
 825    private static readonly object _gate = new();
 26
 27    private static Action<object, object>? _optionsRegistrar;
 28    private static Action<object, object>? _extensionRegistrar;
 29
 30    /// <summary>
 31    /// Registers the options registrar from source-generated code.
 32    /// </summary>
 33    /// <param name="registrar">
 34    /// Action that registers options. Parameters are (IServiceCollection, IConfiguration),
 35    /// typed as object to avoid package dependencies.
 36    /// </param>
 37    public static void RegisterOptionsRegistrar(Action<object, object> registrar)
 38    {
 639        lock (_gate)
 40        {
 641            var prior = _optionsRegistrar;
 642            if (prior == null)
 43            {
 644                _optionsRegistrar = registrar;
 45            }
 46            else
 47            {
 048                _optionsRegistrar = (services, config) =>
 049                {
 050                    prior(services, config);
 051                    registrar(services, config);
 052                };
 53            }
 054        }
 655    }
 56
 57    /// <summary>
 58    /// Registers an extension registrar from extension packages.
 59    /// </summary>
 60    /// <param name="registrar">
 61    /// Action that registers extension services. Parameters are (IServiceCollection, IConfiguration),
 62    /// typed as object to avoid package dependencies.
 63    /// </param>
 64    public static void RegisterExtension(Action<object, object> registrar)
 65    {
 366        lock (_gate)
 67        {
 368            var prior = _extensionRegistrar;
 369            if (prior == null)
 70            {
 371                _extensionRegistrar = registrar;
 72            }
 73            else
 74            {
 075                _extensionRegistrar = (services, config) =>
 076                {
 077                    prior(services, config);
 078                    registrar(services, config);
 079                };
 80            }
 081        }
 382    }
 83
 84    /// <summary>
 85    /// Gets the options registrar if one is registered.
 86    /// </summary>
 87    public static bool TryGetOptionsRegistrar(out Action<object, object>? registrar)
 88    {
 49289        lock (_gate)
 90        {
 49291            registrar = _optionsRegistrar;
 49292            return registrar != null;
 93        }
 49294    }
 95
 96    /// <summary>
 97    /// Gets the extension registrar if one is registered.
 98    /// </summary>
 99    public static bool TryGetExtensionRegistrar(out Action<object, object>? registrar)
 100    {
 492101        lock (_gate)
 102        {
 492103            registrar = _extensionRegistrar;
 492104            return registrar != null;
 105        }
 492106    }
 107
 108    /// <summary>
 109    /// Clears all registrations. For testing purposes only.
 110    /// </summary>
 111    internal static void Clear()
 112    {
 0113        lock (_gate)
 114        {
 0115            _optionsRegistrar = null;
 0116            _extensionRegistrar = null;
 0117        }
 0118    }
 119}