| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for dumping service registration information for debugging. |
| | | 9 | | /// </summary> |
| | | 10 | | public 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 | | { |
| | 8 | 22 | | ArgumentNullException.ThrowIfNull(services); |
| | | 23 | | |
| | 7 | 24 | | options ??= DumpOptions.Default; |
| | 7 | 25 | | var sb = new StringBuilder(); |
| | | 26 | | |
| | 7 | 27 | | var filteredDescriptors = services.AsEnumerable(); |
| | | 28 | | |
| | | 29 | | // Apply lifetime filter |
| | 7 | 30 | | if (options.LifetimeFilter.HasValue) |
| | | 31 | | { |
| | 3 | 32 | | filteredDescriptors = filteredDescriptors.Where(d => d.Lifetime == options.LifetimeFilter.Value); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // Apply service type filter |
| | 7 | 36 | | if (options.ServiceTypeFilter is not null) |
| | | 37 | | { |
| | 3 | 38 | | filteredDescriptors = filteredDescriptors.Where(d => options.ServiceTypeFilter(d.ServiceType)); |
| | | 39 | | } |
| | | 40 | | |
| | 7 | 41 | | var descriptorList = filteredDescriptors.ToList(); |
| | | 42 | | |
| | 7 | 43 | | sb.AppendLine($"═══ Service Registrations ({descriptorList.Count} registrations) ═══"); |
| | 7 | 44 | | sb.AppendLine(); |
| | | 45 | | |
| | 7 | 46 | | if (descriptorList.Count == 0) |
| | | 47 | | { |
| | 1 | 48 | | sb.AppendLine(" (no registrations match the filter)"); |
| | 1 | 49 | | return sb.ToString(); |
| | | 50 | | } |
| | | 51 | | |
| | 6 | 52 | | if (options.GroupByLifetime) |
| | | 53 | | { |
| | 1 | 54 | | DumpGroupedByLifetime(sb, descriptorList); |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | 5 | 58 | | DumpFlat(sb, descriptorList); |
| | | 59 | | } |
| | | 60 | | |
| | 6 | 61 | | 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 | | { |
| | 2 | 75 | | ArgumentNullException.ThrowIfNull(serviceProvider); |
| | | 76 | | |
| | 1 | 77 | | var serviceCollection = serviceProvider.GetServiceCollection(); |
| | 1 | 78 | | return serviceCollection.Dump(options); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static void DumpGroupedByLifetime(StringBuilder sb, List<ServiceDescriptor> descriptors) |
| | | 82 | | { |
| | 1 | 83 | | var groups = descriptors |
| | 3 | 84 | | .GroupBy(d => d.Lifetime) |
| | 4 | 85 | | .OrderBy(g => g.Key); |
| | | 86 | | |
| | 8 | 87 | | foreach (var group in groups) |
| | | 88 | | { |
| | 3 | 89 | | sb.AppendLine($"── {group.Key} ──"); |
| | 3 | 90 | | sb.AppendLine(); |
| | | 91 | | |
| | 15 | 92 | | foreach (var descriptor in group.OrderBy(d => d.ServiceType.Name)) |
| | | 93 | | { |
| | 3 | 94 | | var info = new ServiceRegistrationInfo(descriptor); |
| | 3 | 95 | | sb.AppendLine(info.ToDetailedString()); |
| | 3 | 96 | | sb.AppendLine(); |
| | | 97 | | } |
| | | 98 | | } |
| | 1 | 99 | | } |
| | | 100 | | |
| | | 101 | | private static void DumpFlat(StringBuilder sb, List<ServiceDescriptor> descriptors) |
| | | 102 | | { |
| | 34 | 103 | | foreach (var descriptor in descriptors.OrderBy(d => d.ServiceType.Name)) |
| | | 104 | | { |
| | 8 | 105 | | var info = new ServiceRegistrationInfo(descriptor); |
| | 8 | 106 | | sb.AppendLine(info.ToDetailedString()); |
| | 8 | 107 | | sb.AppendLine(); |
| | | 108 | | } |
| | 5 | 109 | | } |
| | | 110 | | } |