< Summary

Information
Class: NexusLabs.Needlr.LifetimeMismatch
Assembly: NexusLabs.Needlr
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr/LifetimeMismatch.cs
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 44
Line coverage: 100%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_ConsumerServiceType()100%11100%
get_ConsumerImplementationType()100%11100%
get_ConsumerLifetime()100%11100%
get_DependencyServiceType()100%11100%
get_DependencyLifetime()100%11100%
ToDetailedString()50%44100%

File(s)

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

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2
 3using System.Text;
 4
 5namespace NexusLabs.Needlr;
 6
 7/// <summary>
 8/// Represents a lifetime mismatch where a longer-lived service depends on a shorter-lived service.
 9/// This is also known as a "captive dependency" and can lead to unexpected behavior.
 10/// </summary>
 11/// <param name="ConsumerServiceType">The service type that has the dependency.</param>
 12/// <param name="ConsumerImplementationType">The implementation type that has the dependency.</param>
 13/// <param name="ConsumerLifetime">The lifetime of the consumer service.</param>
 14/// <param name="DependencyServiceType">The service type being depended upon.</param>
 15/// <param name="DependencyLifetime">The lifetime of the dependency.</param>
 1916public sealed record LifetimeMismatch(
 4917    Type ConsumerServiceType,
 1218    Type? ConsumerImplementationType,
 4819    ServiceLifetime ConsumerLifetime,
 4920    Type DependencyServiceType,
 6721    ServiceLifetime DependencyLifetime)
 22{
 23    /// <summary>
 24    /// Returns a detailed, formatted string representation of this mismatch
 25    /// suitable for debugging and diagnostics.
 26    /// </summary>
 27    public string ToDetailedString()
 28    {
 1229        var sb = new StringBuilder();
 1230        var consumerName = ConsumerImplementationType?.Name ?? ConsumerServiceType.Name;
 31
 1232        sb.AppendLine($"┌─ Lifetime Mismatch");
 1233        sb.AppendLine($"│  {ConsumerServiceType.Name} ({ConsumerLifetime})");
 1234        sb.AppendLine($"│    └─ depends on ─▶ {DependencyServiceType.Name} ({DependencyLifetime})");
 1235        sb.AppendLine($"│");
 1236        sb.AppendLine($"│  Problem: {ConsumerLifetime} service will capture {DependencyLifetime} dependency");
 1237        sb.AppendLine($"│  Fix: Change {ConsumerServiceType.Name} to {DependencyLifetime},");
 1238        sb.AppendLine($"│       or change {DependencyServiceType.Name} to {ConsumerLifetime},");
 1239        sb.AppendLine($"│       or inject IServiceScopeFactory instead.");
 1240        sb.Append($"└─");
 41
 1242        return sb.ToString();
 43    }
 44}