| | | 1 | | namespace NexusLabs.Needlr; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Marks a class for keyed service registration. The type will be registered |
| | | 5 | | /// with the specified service key in addition to its normal registration. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <remarks> |
| | | 8 | | /// <para> |
| | | 9 | | /// Use this attribute when you want to register multiple implementations of |
| | | 10 | | /// the same interface with different keys, allowing consumers to resolve |
| | | 11 | | /// specific implementations using <c>[FromKeyedServices("key")]</c>. |
| | | 12 | | /// </para> |
| | | 13 | | /// <para> |
| | | 14 | | /// Example: |
| | | 15 | | /// <code> |
| | | 16 | | /// [Keyed("stripe")] |
| | | 17 | | /// public class StripeProcessor : IPaymentProcessor { } |
| | | 18 | | /// |
| | | 19 | | /// [Keyed("paypal")] |
| | | 20 | | /// public class PayPalProcessor : IPaymentProcessor { } |
| | | 21 | | /// |
| | | 22 | | /// // Consumer resolves specific implementation: |
| | | 23 | | /// public class OrderService( |
| | | 24 | | /// [FromKeyedServices("stripe")] IPaymentProcessor processor) { } |
| | | 25 | | /// </code> |
| | | 26 | | /// </para> |
| | | 27 | | /// </remarks> |
| | | 28 | | [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] |
| | | 29 | | public sealed class KeyedAttribute : Attribute |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="KeyedAttribute"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="key">The service key for this registration.</param> |
| | 1230 | 35 | | public KeyedAttribute(string key) |
| | | 36 | | { |
| | 1230 | 37 | | Key = key ?? throw new ArgumentNullException(nameof(key)); |
| | 1230 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the service key for this registration. |
| | | 42 | | /// </summary> |
| | 0 | 43 | | public string Key { get; } |
| | | 44 | | } |