| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Marks a class as an options/configuration type that should be bound to a configuration section. |
| | | 7 | | /// The source generator will automatically generate the <c>services.Configure<T>()</c> call. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// When applied to a class, the generator will emit code to bind the class to a configuration section. |
| | | 12 | | /// If no section name is specified, it is inferred from the class name by stripping common suffixes |
| | | 13 | | /// (Options, Settings, Config). |
| | | 14 | | /// </para> |
| | | 15 | | /// <para> |
| | | 16 | | /// All three options interfaces are registered automatically: |
| | | 17 | | /// <list type="bullet"> |
| | | 18 | | /// <item><description><c>IOptions<T></c> - Singleton, no reload</description></item> |
| | | 19 | | /// <item><description><c>IOptionsSnapshot<T></c> - Scoped, reloads per request</description></item> |
| | | 20 | | /// <item><description><c>IOptionsMonitor<T></c> - Singleton with change notifications</description></item> |
| | | 21 | | /// </list> |
| | | 22 | | /// </para> |
| | | 23 | | /// </remarks> |
| | | 24 | | /// <example> |
| | | 25 | | /// <code> |
| | | 26 | | /// // Section name inferred as "Database" |
| | | 27 | | /// [Options] |
| | | 28 | | /// public class DatabaseOptions |
| | | 29 | | /// { |
| | | 30 | | /// public string ConnectionString { get; set; } = ""; |
| | | 31 | | /// public int CommandTimeout { get; set; } = 30; |
| | | 32 | | /// } |
| | | 33 | | /// |
| | | 34 | | /// // Explicit section name |
| | | 35 | | /// [Options("MyApp:Database")] |
| | | 36 | | /// public class DbOptions |
| | | 37 | | /// { |
| | | 38 | | /// public string ConnectionString { get; set; } = ""; |
| | | 39 | | /// } |
| | | 40 | | /// |
| | | 41 | | /// // With validation at startup |
| | | 42 | | /// [Options(ValidateOnStart = true)] |
| | | 43 | | /// public class StripeOptions |
| | | 44 | | /// { |
| | | 45 | | /// [Required] |
| | | 46 | | /// public string ApiKey { get; set; } = ""; |
| | | 47 | | /// } |
| | | 48 | | /// |
| | | 49 | | /// // Named options for multiple instances |
| | | 50 | | /// [Options("Databases:Primary", Name = "Primary")] |
| | | 51 | | /// [Options("Databases:Replica", Name = "Replica")] |
| | | 52 | | /// public class ConnectionOptions |
| | | 53 | | /// { |
| | | 54 | | /// public string ConnectionString { get; set; } = ""; |
| | | 55 | | /// public bool ReadOnly { get; set; } |
| | | 56 | | /// } |
| | | 57 | | /// </code> |
| | | 58 | | /// </example> |
| | | 59 | | [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] |
| | | 60 | | public sealed class OptionsAttribute : Attribute |
| | | 61 | | { |
| | | 62 | | /// <summary> |
| | | 63 | | /// Initializes a new instance of the <see cref="OptionsAttribute"/> class |
| | | 64 | | /// with the section name inferred from the class name. |
| | | 65 | | /// </summary> |
| | 4 | 66 | | public OptionsAttribute() |
| | | 67 | | { |
| | 4 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Initializes a new instance of the <see cref="OptionsAttribute"/> class |
| | | 72 | | /// with an explicit section name. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="sectionName"> |
| | | 75 | | /// The configuration section name to bind to (e.g., "Database" or "MyApp:Database"). |
| | | 76 | | /// </param> |
| | 7385 | 77 | | public OptionsAttribute(string sectionName) |
| | | 78 | | { |
| | 7385 | 79 | | SectionName = sectionName; |
| | 7385 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the configuration section name to bind to. |
| | | 84 | | /// If null, the section name is inferred from the class name. |
| | | 85 | | /// </summary> |
| | 2 | 86 | | public string? SectionName { get; } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets or sets the name for named options. |
| | | 90 | | /// When set, registers as a named option that can be retrieved via |
| | | 91 | | /// <c>IOptionsSnapshot<T>.Get(name)</c> or <c>IOptionsMonitor<T>.Get(name)</c>. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <remarks> |
| | | 94 | | /// Use this when you need multiple configurations of the same options type, |
| | | 95 | | /// such as primary and replica database connections. |
| | | 96 | | /// </remarks> |
| | 1233 | 97 | | public string? Name { get; set; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets or sets a value indicating whether to validate the options at application startup. |
| | | 101 | | /// When true, validation errors will prevent the application from starting. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <remarks> |
| | | 104 | | /// <para> |
| | | 105 | | /// When enabled, the generator will emit: |
| | | 106 | | /// <list type="bullet"> |
| | | 107 | | /// <item><description><c>.ValidateDataAnnotations()</c> - Validates [Required], [Range], etc.</description></item> |
| | | 108 | | /// <item><description><c>.ValidateOnStart()</c> - Runs validation during startup</description></item> |
| | | 109 | | /// </list> |
| | | 110 | | /// </para> |
| | | 111 | | /// <para> |
| | | 112 | | /// For custom validation, implement a <c>Validate()</c> method returning <c>IEnumerable<ValidationError></c>, |
| | | 113 | | /// or specify an external validator using the <see cref="Validator"/> property. |
| | | 114 | | /// </para> |
| | | 115 | | /// </remarks> |
| | 1236 | 116 | | public bool ValidateOnStart { get; set; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets or sets the name of the validation method to use. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <remarks> |
| | | 122 | | /// <para> |
| | | 123 | | /// By default (when null), the generator looks for a method named <c>Validate</c>. |
| | | 124 | | /// Set this property to use a differently-named method. |
| | | 125 | | /// </para> |
| | | 126 | | /// <para> |
| | | 127 | | /// The method must return <c>IEnumerable<ValidationError></c> (or <c>IEnumerable<string></c> |
| | | 128 | | /// for backward compatibility) and take no parameters (for instance methods) or the options |
| | | 129 | | /// type as a parameter (for static methods). |
| | | 130 | | /// </para> |
| | | 131 | | /// </remarks> |
| | | 132 | | /// <example> |
| | | 133 | | /// <code> |
| | | 134 | | /// [Options(ValidateOnStart = true, ValidateMethod = nameof(CheckConfig))] |
| | | 135 | | /// public class MyOptions |
| | | 136 | | /// { |
| | | 137 | | /// public IEnumerable<ValidationError> CheckConfig() |
| | | 138 | | /// { |
| | | 139 | | /// if (string.IsNullOrEmpty(Value)) |
| | | 140 | | /// yield return "Value is required"; |
| | | 141 | | /// } |
| | | 142 | | /// } |
| | | 143 | | /// </code> |
| | | 144 | | /// </example> |
| | 0 | 145 | | public string? ValidateMethod { get; set; } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Gets or sets the external validator type to use. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <remarks> |
| | | 151 | | /// <para> |
| | | 152 | | /// When set, validation is delegated to the specified type instead of a method on the options class. |
| | | 153 | | /// The validator type must implement <c>IOptionsValidator<T></c> or FluentValidation's |
| | | 154 | | /// <c>AbstractValidator<T></c> (if FluentValidation is referenced). |
| | | 155 | | /// </para> |
| | | 156 | | /// <para> |
| | | 157 | | /// Can be combined with <see cref="ValidateMethod"/> to specify a custom method name on the |
| | | 158 | | /// validator type (default is <c>Validate</c>). |
| | | 159 | | /// </para> |
| | | 160 | | /// </remarks> |
| | | 161 | | /// <example> |
| | | 162 | | /// <code> |
| | | 163 | | /// [Options(ValidateOnStart = true, Validator = typeof(MyOptionsValidator))] |
| | | 164 | | /// public class MyOptions { ... } |
| | | 165 | | /// |
| | | 166 | | /// public class MyOptionsValidator : IOptionsValidator<MyOptions> |
| | | 167 | | /// { |
| | | 168 | | /// public IEnumerable<ValidationError> Validate(MyOptions options) { ... } |
| | | 169 | | /// } |
| | | 170 | | /// </code> |
| | | 171 | | /// </example> |
| | 617 | 172 | | public Type? Validator { get; set; } |
| | | 173 | | } |