| | | 1 | | namespace NexusLabs.Needlr; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Specifies that the decorated class should only be registered as the specified interface type, |
| | | 5 | | /// rather than all interfaces it implements. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <typeparam name="TInterface"> |
| | | 8 | | /// The interface type to register as. Must be an interface implemented by the decorated class. |
| | | 9 | | /// </typeparam> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// <para> |
| | | 12 | | /// By default, Needlr registers a class as all non-system interfaces it implements. |
| | | 13 | | /// Use this attribute when you want explicit control over which interface(s) are publicly |
| | | 14 | | /// resolvable from the container. |
| | | 15 | | /// </para> |
| | | 16 | | /// <para> |
| | | 17 | | /// When this attribute is present, the class will ONLY be registered as the specified interface(s) |
| | | 18 | | /// and as itself (the concrete type). Other implemented interfaces will not be registered. |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// Multiple [RegisterAs<T>] attributes can be applied to register the class as multiple |
| | | 22 | | /// specific interfaces while still excluding others. |
| | | 23 | | /// </para> |
| | | 24 | | /// </remarks> |
| | | 25 | | /// <example> |
| | | 26 | | /// <code> |
| | | 27 | | /// public interface IReader { string Read(); } |
| | | 28 | | /// public interface IWriter { void Write(string data); } |
| | | 29 | | /// public interface ILogger { void Log(string message); } |
| | | 30 | | /// |
| | | 31 | | /// // Only registered as IReader - not as IWriter or ILogger |
| | | 32 | | /// [RegisterAs<IReader>] |
| | | 33 | | /// public class FileService : IReader, IWriter, ILogger |
| | | 34 | | /// { |
| | | 35 | | /// public string Read() => "data"; |
| | | 36 | | /// public void Write(string data) { } |
| | | 37 | | /// public void Log(string message) { } |
| | | 38 | | /// } |
| | | 39 | | /// |
| | | 40 | | /// // Register as multiple specific interfaces |
| | | 41 | | /// [RegisterAs<IReader>] |
| | | 42 | | /// [RegisterAs<IWriter>] |
| | | 43 | | /// public class DualService : IReader, IWriter, ILogger |
| | | 44 | | /// { |
| | | 45 | | /// // Registered as IReader and IWriter, but NOT as ILogger |
| | | 46 | | /// } |
| | | 47 | | /// </code> |
| | | 48 | | /// </example> |
| | | 49 | | [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] |
| | | 50 | | public sealed class RegisterAsAttribute<TInterface> : Attribute |
| | | 51 | | where TInterface : class |
| | | 52 | | { |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the interface type that the class should be registered as. |
| | | 55 | | /// </summary> |
| | 0 | 56 | | public Type InterfaceType => typeof(TInterface); |
| | | 57 | | } |