| | | 1 | | namespace 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<IMyService>(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<IMyService>(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)] |
| | | 66 | | public 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> |
| | 4920 | 85 | | public int Order { get; set; } = 0; |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the service type that this decorator wraps. |
| | | 89 | | /// </summary> |
| | 0 | 90 | | public Type ServiceType => typeof(TService); |
| | | 91 | | } |