| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Marks an interface or partial class as a Provider - a strongly-typed service locator. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// <para> |
| | | 10 | | /// A Provider is a compile-time generated class that provides strongly-typed access to |
| | | 11 | | /// services registered in the dependency injection container. Unlike using |
| | | 12 | | /// <c>IServiceProvider.GetService<T>()</c> directly, Providers offer: |
| | | 13 | | /// </para> |
| | | 14 | | /// <list type="bullet"> |
| | | 15 | | /// <item><description>Compile-time verification that required services are registered</description></item> |
| | | 16 | | /// <item><description>IntelliSense and IDE support for available services</description></item> |
| | | 17 | | /// <item><description>Easy mocking in unit tests</description></item> |
| | | 18 | | /// </list> |
| | | 19 | | /// <para> |
| | | 20 | | /// <b>Providers are always registered as Singletons.</b> All service properties are resolved |
| | | 21 | | /// via constructor injection at Provider construction time (fail-fast). For creating new |
| | | 22 | | /// instances on demand, use the <see cref="Factories"/> parameter to generate factory properties. |
| | | 23 | | /// </para> |
| | | 24 | | /// <para> |
| | | 25 | | /// <b>Usage modes:</b> |
| | | 26 | | /// </para> |
| | | 27 | | /// <list type="number"> |
| | | 28 | | /// <item> |
| | | 29 | | /// <description> |
| | | 30 | | /// <b>Interface definition:</b> Apply to an interface with get-only properties. |
| | | 31 | | /// The generator creates an implementing class. |
| | | 32 | | /// <code> |
| | | 33 | | /// [Provider] |
| | | 34 | | /// public interface IOrderServicesProvider |
| | | 35 | | /// { |
| | | 36 | | /// IOrderRepository Repository { get; } |
| | | 37 | | /// IOrderValidator Validator { get; } |
| | | 38 | | /// } |
| | | 39 | | /// // Generates: OrderServicesProvider class |
| | | 40 | | /// </code> |
| | | 41 | | /// </description> |
| | | 42 | | /// </item> |
| | | 43 | | /// <item> |
| | | 44 | | /// <description> |
| | | 45 | | /// <b>Shorthand class:</b> Apply to a partial class with type parameters. |
| | | 46 | | /// The generator creates both an interface and the implementation. |
| | | 47 | | /// <code> |
| | | 48 | | /// [Provider(typeof(IOrderRepository), typeof(IOrderValidator))] |
| | | 49 | | /// public partial class OrderDependenciesProvider { } |
| | | 50 | | /// // Generates: IOrderDependenciesProvider interface + implementation |
| | | 51 | | /// </code> |
| | | 52 | | /// </description> |
| | | 53 | | /// </item> |
| | | 54 | | /// </list> |
| | | 55 | | /// </remarks> |
| | | 56 | | [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = false, Inherited = false)] |
| | | 57 | | public sealed class ProviderAttribute : Attribute |
| | | 58 | | { |
| | | 59 | | /// <summary> |
| | | 60 | | /// Creates a Provider attribute for an interface definition. |
| | | 61 | | /// Define service properties directly on the interface. |
| | | 62 | | /// </summary> |
| | 1845 | 63 | | public ProviderAttribute() |
| | | 64 | | { |
| | 1845 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Creates a Provider with the specified required service types. |
| | | 69 | | /// Use on a partial class to auto-generate interface and implementation. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="requiredServices"> |
| | | 72 | | /// Service types that must be registered in the container. |
| | | 73 | | /// Property names are derived from type names (e.g., IOrderRepository → OrderRepository). |
| | | 74 | | /// </param> |
| | 1230 | 75 | | public ProviderAttribute(params Type[] requiredServices) |
| | | 76 | | { |
| | 1230 | 77 | | Required = requiredServices; |
| | 1230 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Service types that must be registered. Resolution failure throws at startup. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <remarks> |
| | | 84 | | /// Properties for these services use <c>GetRequiredService<T>()</c>. |
| | | 85 | | /// If any service is not registered, the application fails to start. |
| | | 86 | | /// </remarks> |
| | 1230 | 87 | | public Type[]? Required { get; set; } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Service types that may not be registered. Properties are nullable. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <remarks> |
| | | 93 | | /// Properties for these services use <c>GetService<T>()</c> and return null |
| | | 94 | | /// if the service is not registered. |
| | | 95 | | /// </remarks> |
| | 615 | 96 | | public Type[]? Optional { get; set; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Service types to resolve as <c>IEnumerable<T></c>. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <remarks> |
| | | 102 | | /// Properties for these services use <c>GetServices<T>()</c> and return |
| | | 103 | | /// all registered implementations. |
| | | 104 | | /// </remarks> |
| | 615 | 105 | | public Type[]? Collections { get; set; } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Types to generate factories for, enabling creation of new instances. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <remarks> |
| | | 111 | | /// <para> |
| | | 112 | | /// For each type specified: |
| | | 113 | | /// </para> |
| | | 114 | | /// <list type="bullet"> |
| | | 115 | | /// <item><description> |
| | | 116 | | /// If the type has <c>[GenerateFactory]</c>, the existing factory is used. |
| | | 117 | | /// </description></item> |
| | | 118 | | /// <item><description> |
| | | 119 | | /// Otherwise, a factory (<c>IXxxFactory</c>) is generated automatically. |
| | | 120 | | /// </description></item> |
| | | 121 | | /// </list> |
| | | 122 | | /// <para> |
| | | 123 | | /// This allows the Provider to create new instances while remaining a Singleton. |
| | | 124 | | /// </para> |
| | | 125 | | /// </remarks> |
| | 615 | 126 | | public Type[]? Factories { get; set; } |
| | | 127 | | } |