| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Options for controlling container verification behavior. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed record VerificationOptions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets or sets the behavior when lifetime mismatches are detected. |
| | | 12 | | /// Default is <see cref="VerificationBehavior.Warn"/>. |
| | | 13 | | /// </summary> |
| | 1030 | 14 | | public VerificationBehavior LifetimeMismatchBehavior { get; init; } = VerificationBehavior.Warn; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets or sets the behavior when circular dependencies are detected. |
| | | 18 | | /// Default is <see cref="VerificationBehavior.Throw"/>. |
| | | 19 | | /// </summary> |
| | 512 | 20 | | public VerificationBehavior CircularDependencyBehavior { get; init; } = VerificationBehavior.Throw; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets or sets a callback for reporting verification issues. |
| | | 24 | | /// If null, issues are written to Console.Error. |
| | | 25 | | /// </summary> |
| | 19 | 26 | | public Action<VerificationIssue>? IssueReporter { get; init; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Default verification options - warns on lifetime mismatches, throws on circular dependencies. |
| | | 30 | | /// </summary> |
| | 489 | 31 | | public static VerificationOptions Default => new(); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Strict verification options - throws on any verification issue. |
| | | 35 | | /// </summary> |
| | 6 | 36 | | public static VerificationOptions Strict => new() |
| | 6 | 37 | | { |
| | 6 | 38 | | LifetimeMismatchBehavior = VerificationBehavior.Throw, |
| | 6 | 39 | | CircularDependencyBehavior = VerificationBehavior.Throw |
| | 6 | 40 | | }; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Disabled verification - no checks performed. |
| | | 44 | | /// </summary> |
| | 1 | 45 | | public static VerificationOptions Disabled => new() |
| | 1 | 46 | | { |
| | 1 | 47 | | LifetimeMismatchBehavior = VerificationBehavior.Silent, |
| | 1 | 48 | | CircularDependencyBehavior = VerificationBehavior.Silent |
| | 1 | 49 | | }; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Specifies the behavior when a verification issue is detected. |
| | | 54 | | /// </summary> |
| | | 55 | | public enum VerificationBehavior |
| | | 56 | | { |
| | | 57 | | /// <summary> |
| | | 58 | | /// Ignore the issue silently. |
| | | 59 | | /// </summary> |
| | | 60 | | Silent = 0, |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Log a warning but continue. |
| | | 64 | | /// </summary> |
| | | 65 | | Warn = 1, |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Throw an exception to prevent the application from starting. |
| | | 69 | | /// </summary> |
| | | 70 | | Throw = 2 |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Represents a verification issue detected during container analysis. |
| | | 75 | | /// </summary> |
| | | 76 | | public sealed record VerificationIssue( |
| | | 77 | | VerificationIssueType Type, |
| | | 78 | | string Message, |
| | | 79 | | string DetailedMessage, |
| | | 80 | | VerificationBehavior ConfiguredBehavior) |
| | | 81 | | { |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the service types involved in this issue. |
| | | 84 | | /// </summary> |
| | | 85 | | public IReadOnlyList<Type> InvolvedTypes { get; init; } = []; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// The type of verification issue detected. |
| | | 90 | | /// </summary> |
| | | 91 | | public enum VerificationIssueType |
| | | 92 | | { |
| | | 93 | | /// <summary> |
| | | 94 | | /// A longer-lived service depends on a shorter-lived service. |
| | | 95 | | /// </summary> |
| | | 96 | | LifetimeMismatch, |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// A circular dependency was detected in the dependency graph. |
| | | 100 | | /// </summary> |
| | | 101 | | CircularDependency |
| | | 102 | | } |