< Summary

Information
Class: NexusLabs.Needlr.DumpExtensions
Assembly: NexusLabs.Needlr
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr/DumpExtensions.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 110
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
Dump(...)100%1010100%
Dump(...)100%11100%
DumpGroupedByLifetime(...)100%44100%
DumpFlat(...)100%22100%

File(s)

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

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2
 3using System.Text;
 4
 5namespace NexusLabs.Needlr;
 6
 7/// <summary>
 8/// Extension methods for dumping service registration information for debugging.
 9/// </summary>
 10public static class DumpExtensions
 11{
 12    /// <summary>
 13    /// Returns a formatted string representation of all service registrations
 14    /// in the service collection for debugging purposes.
 15    /// </summary>
 16    /// <param name="services">The service collection to dump.</param>
 17    /// <param name="options">Optional dump options for filtering and formatting.</param>
 18    /// <returns>A formatted string with registration details.</returns>
 19    /// <exception cref="ArgumentNullException">Thrown when services is null.</exception>
 20    public static string Dump(this IServiceCollection services, DumpOptions? options = null)
 21    {
 822        ArgumentNullException.ThrowIfNull(services);
 23
 724        options ??= DumpOptions.Default;
 725        var sb = new StringBuilder();
 26
 727        var filteredDescriptors = services.AsEnumerable();
 28
 29        // Apply lifetime filter
 730        if (options.LifetimeFilter.HasValue)
 31        {
 332            filteredDescriptors = filteredDescriptors.Where(d => d.Lifetime == options.LifetimeFilter.Value);
 33        }
 34
 35        // Apply service type filter
 736        if (options.ServiceTypeFilter is not null)
 37        {
 338            filteredDescriptors = filteredDescriptors.Where(d => options.ServiceTypeFilter(d.ServiceType));
 39        }
 40
 741        var descriptorList = filteredDescriptors.ToList();
 42
 743        sb.AppendLine($"═══ Service Registrations ({descriptorList.Count} registrations) ═══");
 744        sb.AppendLine();
 45
 746        if (descriptorList.Count == 0)
 47        {
 148            sb.AppendLine("  (no registrations match the filter)");
 149            return sb.ToString();
 50        }
 51
 652        if (options.GroupByLifetime)
 53        {
 154            DumpGroupedByLifetime(sb, descriptorList);
 55        }
 56        else
 57        {
 558            DumpFlat(sb, descriptorList);
 59        }
 60
 661        return sb.ToString();
 62    }
 63
 64    /// <summary>
 65    /// Returns a formatted string representation of all service registrations
 66    /// in the service provider for debugging purposes.
 67    /// </summary>
 68    /// <param name="serviceProvider">The service provider to dump.</param>
 69    /// <param name="options">Optional dump options for filtering and formatting.</param>
 70    /// <returns>A formatted string with registration details.</returns>
 71    /// <exception cref="ArgumentNullException">Thrown when serviceProvider is null.</exception>
 72    /// <exception cref="InvalidOperationException">Thrown when the service collection is not accessible.</exception>
 73    public static string Dump(this IServiceProvider serviceProvider, DumpOptions? options = null)
 74    {
 275        ArgumentNullException.ThrowIfNull(serviceProvider);
 76
 177        var serviceCollection = serviceProvider.GetServiceCollection();
 178        return serviceCollection.Dump(options);
 79    }
 80
 81    private static void DumpGroupedByLifetime(StringBuilder sb, List<ServiceDescriptor> descriptors)
 82    {
 183        var groups = descriptors
 384            .GroupBy(d => d.Lifetime)
 485            .OrderBy(g => g.Key);
 86
 887        foreach (var group in groups)
 88        {
 389            sb.AppendLine($"── {group.Key} ──");
 390            sb.AppendLine();
 91
 1592            foreach (var descriptor in group.OrderBy(d => d.ServiceType.Name))
 93            {
 394                var info = new ServiceRegistrationInfo(descriptor);
 395                sb.AppendLine(info.ToDetailedString());
 396                sb.AppendLine();
 97            }
 98        }
 199    }
 100
 101    private static void DumpFlat(StringBuilder sb, List<ServiceDescriptor> descriptors)
 102    {
 34103        foreach (var descriptor in descriptors.OrderBy(d => d.ServiceType.Name))
 104        {
 8105            var info = new ServiceRegistrationInfo(descriptor);
 8106            sb.AppendLine(info.ToDetailedString());
 8107            sb.AppendLine();
 108        }
 5109    }
 110}