| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extension methods for verifying service collection configuration. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class ServiceCollectionVerificationExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Verifies the service collection configuration and throws if issues are configured to throw. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="services">The service collection to verify.</param> |
| | | 14 | | /// <param name="options">The verification options. Defaults to <see cref="VerificationOptions.Default"/>.</param> |
| | | 15 | | /// <returns>The same service collection for chaining.</returns> |
| | | 16 | | /// <exception cref="ContainerVerificationException"> |
| | | 17 | | /// Thrown if verification issues are detected and the configured behavior is <see cref="VerificationBehavior.Throw" |
| | | 18 | | /// </exception> |
| | | 19 | | public static IServiceCollection Verify( |
| | | 20 | | this IServiceCollection services, |
| | | 21 | | VerificationOptions? options = null) |
| | | 22 | | { |
| | 8 | 23 | | ArgumentNullException.ThrowIfNull(services); |
| | 8 | 24 | | options ??= VerificationOptions.Default; |
| | | 25 | | |
| | 8 | 26 | | var issues = new List<VerificationIssue>(); |
| | | 27 | | |
| | | 28 | | // Check for lifetime mismatches |
| | 8 | 29 | | if (options.LifetimeMismatchBehavior != VerificationBehavior.Silent) |
| | | 30 | | { |
| | 7 | 31 | | var mismatches = services.DetectLifetimeMismatches(); |
| | 24 | 32 | | foreach (var mismatch in mismatches) |
| | | 33 | | { |
| | 5 | 34 | | issues.Add(new VerificationIssue( |
| | 5 | 35 | | Type: VerificationIssueType.LifetimeMismatch, |
| | 5 | 36 | | Message: $"Lifetime mismatch: {mismatch.ConsumerServiceType.Name} ({mismatch.ConsumerLifetime}) depe |
| | 5 | 37 | | DetailedMessage: mismatch.ToDetailedString(), |
| | 5 | 38 | | ConfiguredBehavior: options.LifetimeMismatchBehavior) |
| | 5 | 39 | | { |
| | 5 | 40 | | InvolvedTypes = [mismatch.ConsumerServiceType, mismatch.DependencyServiceType] |
| | 5 | 41 | | }); |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | // Process issues based on configured behavior |
| | 8 | 46 | | ProcessVerificationIssues(issues, options); |
| | | 47 | | |
| | 7 | 48 | | return services; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Verifies the service collection and returns detailed diagnostic results. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="services">The service collection to verify.</param> |
| | | 55 | | /// <param name="options">The verification options. Defaults to <see cref="VerificationOptions.Default"/>.</param> |
| | | 56 | | /// <returns>A <see cref="VerificationResult"/> containing all detected issues.</returns> |
| | | 57 | | public static VerificationResult VerifyWithDiagnostics( |
| | | 58 | | this IServiceCollection services, |
| | | 59 | | VerificationOptions? options = null) |
| | | 60 | | { |
| | 2 | 61 | | ArgumentNullException.ThrowIfNull(services); |
| | 2 | 62 | | options ??= VerificationOptions.Default; |
| | | 63 | | |
| | 2 | 64 | | var issues = new List<VerificationIssue>(); |
| | | 65 | | |
| | | 66 | | // Check for lifetime mismatches |
| | 2 | 67 | | if (options.LifetimeMismatchBehavior != VerificationBehavior.Silent) |
| | | 68 | | { |
| | 2 | 69 | | var mismatches = services.DetectLifetimeMismatches(); |
| | 8 | 70 | | foreach (var mismatch in mismatches) |
| | | 71 | | { |
| | 2 | 72 | | issues.Add(new VerificationIssue( |
| | 2 | 73 | | Type: VerificationIssueType.LifetimeMismatch, |
| | 2 | 74 | | Message: $"Lifetime mismatch: {mismatch.ConsumerServiceType.Name} ({mismatch.ConsumerLifetime}) depe |
| | 2 | 75 | | DetailedMessage: mismatch.ToDetailedString(), |
| | 2 | 76 | | ConfiguredBehavior: options.LifetimeMismatchBehavior) |
| | 2 | 77 | | { |
| | 2 | 78 | | InvolvedTypes = [mismatch.ConsumerServiceType, mismatch.DependencyServiceType] |
| | 2 | 79 | | }); |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | 2 | 83 | | return new VerificationResult(issues); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static void ProcessVerificationIssues(List<VerificationIssue> issues, VerificationOptions options) |
| | | 87 | | { |
| | 13 | 88 | | var issuesByBehavior = issues.GroupBy(i => i.ConfiguredBehavior); |
| | | 89 | | |
| | 23 | 90 | | foreach (var group in issuesByBehavior) |
| | | 91 | | { |
| | 4 | 92 | | switch (group.Key) |
| | | 93 | | { |
| | | 94 | | case VerificationBehavior.Warn: |
| | 14 | 95 | | foreach (var issue in group) |
| | | 96 | | { |
| | 4 | 97 | | if (options.IssueReporter is not null) |
| | | 98 | | { |
| | 4 | 99 | | options.IssueReporter(issue); |
| | | 100 | | } |
| | | 101 | | else |
| | | 102 | | { |
| | 0 | 103 | | Console.Error.WriteLine($"[Needlr Warning] {issue.Message}"); |
| | 0 | 104 | | Console.Error.WriteLine(issue.DetailedMessage); |
| | 0 | 105 | | Console.Error.WriteLine(); |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | break; |
| | | 109 | | |
| | | 110 | | case VerificationBehavior.Throw: |
| | 1 | 111 | | var throwableIssues = group.ToList(); |
| | 1 | 112 | | if (throwableIssues.Count > 0) |
| | | 113 | | { |
| | 1 | 114 | | throw new ContainerVerificationException(throwableIssues); |
| | | 115 | | } |
| | | 116 | | break; |
| | | 117 | | } |
| | | 118 | | } |
| | 7 | 119 | | } |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Result of container verification containing all detected issues. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="Issues">The list of verification issues detected.</param> |
| | | 126 | | public sealed record VerificationResult(IReadOnlyList<VerificationIssue> Issues) |
| | | 127 | | { |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets whether the container configuration is valid (no errors). |
| | | 130 | | /// </summary> |
| | | 131 | | public bool IsValid => !Issues.Any(i => i.ConfiguredBehavior == VerificationBehavior.Throw); |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Throws <see cref="ContainerVerificationException"/> if any issues are present. |
| | | 135 | | /// </summary> |
| | | 136 | | public void ThrowIfInvalid() |
| | | 137 | | { |
| | | 138 | | if (Issues.Count > 0) |
| | | 139 | | { |
| | | 140 | | throw new ContainerVerificationException(Issues.ToList()); |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Generates a detailed diagnostic report of all issues. |
| | | 146 | | /// </summary> |
| | | 147 | | public string ToDetailedReport() |
| | | 148 | | { |
| | | 149 | | if (Issues.Count == 0) |
| | | 150 | | { |
| | | 151 | | return "✅ Container verification passed. No issues detected."; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | var sb = new System.Text.StringBuilder(); |
| | | 155 | | sb.AppendLine($"❌ Container verification found {Issues.Count} issue(s):"); |
| | | 156 | | sb.AppendLine(); |
| | | 157 | | |
| | | 158 | | foreach (var issue in Issues) |
| | | 159 | | { |
| | | 160 | | sb.AppendLine($"[{issue.Type}] {issue.Message}"); |
| | | 161 | | if (!string.IsNullOrWhiteSpace(issue.DetailedMessage)) |
| | | 162 | | { |
| | | 163 | | sb.AppendLine(issue.DetailedMessage); |
| | | 164 | | } |
| | | 165 | | sb.AppendLine(); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | return sb.ToString(); |
| | | 169 | | } |
| | | 170 | | } |