< Summary

Information
Class: NexusLabs.Needlr.DecoratorForAttribute<T>
Assembly: NexusLabs.Needlr
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr/DecoratorForAttribute.cs
Line coverage
50%
Covered lines: 1
Uncovered lines: 1
Coverable lines: 2
Total lines: 91
Line coverage: 50%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Order()100%11100%
get_ServiceType()100%210%

File(s)

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

#LineLine coverage
 1namespace NexusLabs.Needlr;
 2
 3/// <summary>
 4/// Marks a class as a decorator for the specified service type. When discovered
 5/// by Needlr (via source generation or reflection), the decorator will be
 6/// automatically wired using <see cref="ServiceCollectionExtensions.AddDecorator{TService, TDecorator}"/>.
 7/// </summary>
 8/// <typeparam name="TService">The service type (interface or class) that this class decorates.</typeparam>
 9/// <remarks>
 10/// <para>
 11/// The decorated class must implement <typeparamref name="TService"/> and accept
 12/// an instance of <typeparamref name="TService"/> in its constructor.
 13/// </para>
 14/// <para>
 15/// When multiple decorators exist for the same service, use the <see cref="Order"/>
 16/// property to control the decoration order. Lower values are applied first (closer
 17/// to the original service), higher values wrap outer layers.
 18/// </para>
 19/// <para>
 20/// Using this attribute implicitly excludes the type from normal interface
 21/// registration (equivalent to applying <see cref="DoNotAutoRegisterAttribute"/>
 22/// for the decorated interface).
 23/// </para>
 24/// </remarks>
 25/// <example>
 26/// <code>
 27/// public interface IMyService
 28/// {
 29///     string GetValue();
 30/// }
 31///
 32/// public class MyService : IMyService
 33/// {
 34///     public string GetValue() => "Original";
 35/// }
 36///
 37/// [DecoratorFor&lt;IMyService&gt;(Order = 1)]
 38/// public class LoggingDecorator : IMyService
 39/// {
 40///     private readonly IMyService _inner;
 41///
 42///     public LoggingDecorator(IMyService inner) => _inner = inner;
 43///
 44///     public string GetValue()
 45///     {
 46///         Console.WriteLine("Before");
 47///         var result = _inner.GetValue();
 48///         Console.WriteLine("After");
 49///         return result;
 50///     }
 51/// }
 52///
 53/// [DecoratorFor&lt;IMyService&gt;(Order = 2)]
 54/// public class CachingDecorator : IMyService
 55/// {
 56///     private readonly IMyService _inner;
 57///     private string? _cached;
 58///
 59///     public CachingDecorator(IMyService inner) => _inner = inner;
 60///
 61///     public string GetValue() => _cached ??= _inner.GetValue();
 62/// }
 63/// </code>
 64/// </example>
 65[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
 66public sealed class DecoratorForAttribute<TService> : Attribute
 67    where TService : class
 68{
 69    /// <summary>
 70    /// Gets or sets the order in which this decorator is applied relative to
 71    /// other decorators for the same service. Lower values are applied first
 72    /// (closer to the original implementation). Default is 0.
 73    /// </summary>
 74    /// <remarks>
 75    /// <para>
 76    /// Example with Order values:
 77    /// <list type="bullet">
 78    /// <item>Original service: MyService</item>
 79    /// <item>Order = 1: LoggingDecorator wraps MyService</item>
 80    /// <item>Order = 2: CachingDecorator wraps LoggingDecorator</item>
 81    /// </list>
 82    /// Resolving IMyService returns: CachingDecorator → LoggingDecorator → MyService
 83    /// </para>
 84    /// </remarks>
 492085    public int Order { get; set; } = 0;
 86
 87    /// <summary>
 88    /// Gets the service type that this decorator wraps.
 89    /// </summary>
 090    public Type ServiceType => typeof(TService);
 91}

Methods/Properties

get_Order()
get_ServiceType()