< Summary

Information
Class: NexusLabs.Needlr.Injection.SourceGen.TypeFilterers.GeneratedTypeFilterer
Assembly: NexusLabs.Needlr.Injection.SourceGen
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.SourceGen/TypeFilterers/GeneratedTypeFilterer.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 104
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%44100%
IsInjectableSingletonType(...)100%44100%
IsInjectableTransientType(...)100%44100%
IsInjectableScopedType(...)100%44100%

File(s)

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

#LineLine coverage
 1using NexusLabs.Needlr.Generators;
 2
 3namespace NexusLabs.Needlr.Injection.SourceGen.TypeFilterers;
 4
 5/// <summary>
 6/// A type filterer that uses compile-time generated lifetime information
 7/// instead of runtime reflection for constructor analysis.
 8/// </summary>
 9/// <remarks>
 10/// <para>
 11/// When using the generated type registrar with pre-computed lifetimes,
 12/// this filterer is effectively a no-op since all lifetime decisions are
 13/// made at compile time. It exists for API compatibility and as a safety
 14/// fallback that returns false for all checks.
 15/// </para>
 16/// <para>
 17/// For zero-reflection scenarios, use this filterer together with
 18/// <see cref="TypeRegistrars.GeneratedTypeRegistrar"/> and explicitly
 19/// provide the type provider function.
 20/// </para>
 21/// </remarks>
 22/// <example>
 23/// <code>
 24/// // Zero-reflection configuration:
 25/// var syringe = new Syringe()
 26///     .UsingGeneratedTypeRegistrar(TypeRegistry.GetInjectableTypes)
 27///     .UsingGeneratedTypeFilterer()
 28///     .UsingGeneratedPluginFactory(TypeRegistry.GetPluginTypes);
 29/// </code>
 30/// </example>
 31[DoNotAutoRegister]
 32public sealed class GeneratedTypeFilterer : ITypeFilterer
 33{
 34    private readonly IReadOnlyDictionary<Type, InjectableLifetime>? _lifetimeLookup;
 35
 36    /// <summary>
 37    /// Initializes a new instance that returns false for all type checks.
 38    /// </summary>
 39    /// <remarks>
 40    /// Use this when all lifetime information is pre-computed by the source generator.
 41    /// The <see cref="TypeRegistrars.GeneratedTypeRegistrar"/> will use pre-computed
 42    /// lifetimes directly and will not need to call this filterer.
 43    /// </remarks>
 1444    public GeneratedTypeFilterer()
 45    {
 1446        _lifetimeLookup = null;
 1447    }
 48
 49    /// <summary>
 50    /// Initializes a new instance with pre-computed lifetime information.
 51    /// </summary>
 52    /// <param name="typeProvider">A function that returns the injectable types with pre-computed lifetimes.</param>
 53    /// <remarks>
 54    /// This constructor builds a lookup table from the provided type information,
 55    /// enabling reflection-free lifetime checks.
 56    /// </remarks>
 32757    public GeneratedTypeFilterer(Func<IReadOnlyList<InjectableTypeInfo>> typeProvider)
 58    {
 32759        ArgumentNullException.ThrowIfNull(typeProvider);
 60
 32761        var types = typeProvider();
 32762        var lookup = new Dictionary<Type, InjectableLifetime>(types.Count);
 63
 10023064        foreach (var info in types)
 65        {
 4978866            if (info.Lifetime.HasValue)
 67            {
 4978868                lookup[info.Type] = info.Lifetime.Value;
 69            }
 70        }
 71
 32772        _lifetimeLookup = lookup;
 32773    }
 74
 75    /// <inheritdoc />
 76    public bool IsInjectableSingletonType(Type type)
 77    {
 4499178        if (_lifetimeLookup is null)
 224479            return false;
 80
 4274781        return _lifetimeLookup.TryGetValue(type, out var lifetime) &&
 4274782               lifetime == InjectableLifetime.Singleton;
 83    }
 84
 85    /// <inheritdoc />
 86    public bool IsInjectableTransientType(Type type)
 87    {
 4640388        if (_lifetimeLookup is null)
 225489            return false;
 90
 4414991        return _lifetimeLookup.TryGetValue(type, out var lifetime) &&
 4414992               lifetime == InjectableLifetime.Transient;
 93    }
 94
 95    /// <inheritdoc />
 96    public bool IsInjectableScopedType(Type type)
 97    {
 4561698        if (_lifetimeLookup is null)
 224499            return false;
 100
 43372101        return _lifetimeLookup.TryGetValue(type, out var lifetime) &&
 43372102               lifetime == InjectableLifetime.Scoped;
 103    }
 104}