| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.Hosting; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.Hosting; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents options for creating a host application with logging configuration. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <param name="Settings"> |
| | | 12 | | /// The host application builder settings to use when creating the host. |
| | | 13 | | /// </param> |
| | | 14 | | /// <param name="PostPluginRegistrationCallbacks"> |
| | | 15 | | /// The callbacks to execute after plugin registration, allowing for additional service configuration. |
| | | 16 | | /// </param> |
| | | 17 | | /// <param name="Logger"> |
| | | 18 | | /// The logger instance to use for logging during host creation. |
| | | 19 | | /// </param> |
| | 14 | 20 | | public sealed record CreateHostOptions( |
| | 68 | 21 | | HostApplicationBuilderSettings Settings, |
| | 44 | 22 | | IReadOnlyList<Action<IServiceCollection>> PostPluginRegistrationCallbacks, |
| | 76 | 23 | | ILogger Logger) |
| | | 24 | | { |
| | 1 | 25 | | private static readonly CreateHostOptions _defaultOptions = new(settings: new()); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Callbacks to execute before plugin registration to allow configuring the service collection. |
| | | 29 | | /// </summary> |
| | 42 | 30 | | public IReadOnlyList<Action<IServiceCollection>> PrePluginRegistrationCallbacks { get; init; } = Array.Empty<Action< |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="CreateHostOptions"/> |
| | | 34 | | /// record with a <see cref="NullLogger"/>. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="settings">The host application builder settings to use.</param> |
| | | 37 | | public CreateHostOptions(HostApplicationBuilderSettings? settings = null) |
| | 5 | 38 | | : this(settings ?? new(), PostPluginRegistrationCallbacks: [], NullLogger.Instance) |
| | | 39 | | { |
| | 5 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Initializes a new instance with pre-plugin registration callbacks. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="settings">The host application builder settings to use.</param> |
| | | 46 | | /// <param name="prePluginRegistrationCallbacks">Callbacks to execute before plugin registration.</param> |
| | | 47 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="prePluginRegistrationCallbacks"/> is null.</ |
| | | 48 | | public CreateHostOptions( |
| | | 49 | | HostApplicationBuilderSettings settings, |
| | | 50 | | IEnumerable<Action<IServiceCollection>> prePluginRegistrationCallbacks) |
| | 2 | 51 | | : this(settings) |
| | | 52 | | { |
| | 2 | 53 | | ArgumentNullException.ThrowIfNull(prePluginRegistrationCallbacks); |
| | 1 | 54 | | PrePluginRegistrationCallbacks = prePluginRegistrationCallbacks.ToArray(); |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Initializes a new instance with a post-plugin registration callback. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="settings">The host application builder settings to use.</param> |
| | | 61 | | /// <param name="postPluginRegistrationCallback"> |
| | | 62 | | /// The callback to execute after plugin registration, allowing for additional service configuration. |
| | | 63 | | /// </param> |
| | | 64 | | /// <exception cref="ArgumentNullException"> |
| | | 65 | | /// Thrown when <paramref name="postPluginRegistrationCallback"/> is null. |
| | | 66 | | /// </exception> |
| | | 67 | | public CreateHostOptions( |
| | | 68 | | HostApplicationBuilderSettings settings, |
| | | 69 | | Action<IServiceCollection> postPluginRegistrationCallback) |
| | 1 | 70 | | : this(settings, PostPluginRegistrationCallbacks: [postPluginRegistrationCallback], NullLogger.Instance) |
| | | 71 | | { |
| | 1 | 72 | | ArgumentNullException.ThrowIfNull(postPluginRegistrationCallback); |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Initializes a new instance with pre- and post-plugin registration callbacks. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="settings">The host application builder settings to use.</param> |
| | | 79 | | /// <param name="prePluginRegistrationCallbacks">Callbacks to execute before plugin registration.</param> |
| | | 80 | | /// <param name="postPluginRegistrationCallbacks">Callbacks to execute after plugin registration.</param> |
| | | 81 | | /// <exception cref="ArgumentNullException">Thrown when a parameter is null.</exception> |
| | | 82 | | public CreateHostOptions( |
| | | 83 | | HostApplicationBuilderSettings settings, |
| | | 84 | | IEnumerable<Action<IServiceCollection>> prePluginRegistrationCallbacks, |
| | | 85 | | IEnumerable<Action<IServiceCollection>> postPluginRegistrationCallbacks) |
| | 3 | 86 | | : this(settings, PostPluginRegistrationCallbacks: (postPluginRegistrationCallbacks ?? throw new ArgumentNullExce |
| | | 87 | | { |
| | 2 | 88 | | ArgumentNullException.ThrowIfNull(prePluginRegistrationCallbacks); |
| | 1 | 89 | | PrePluginRegistrationCallbacks = prePluginRegistrationCallbacks.ToArray(); |
| | 1 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Initializes a new instance with a logger. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="settings">The host application builder settings to use.</param> |
| | | 96 | | /// <param name="logger"> |
| | | 97 | | /// The logger instance to use for logging during host creation. |
| | | 98 | | /// </param> |
| | | 99 | | /// <exception cref="ArgumentNullException"> |
| | | 100 | | /// Thrown when <paramref name="logger"/> is null. |
| | | 101 | | /// </exception> |
| | | 102 | | public CreateHostOptions( |
| | | 103 | | HostApplicationBuilderSettings settings, |
| | | 104 | | ILogger logger) |
| | 3 | 105 | | : this(settings, PostPluginRegistrationCallbacks: [], logger) |
| | | 106 | | { |
| | 3 | 107 | | ArgumentNullException.ThrowIfNull(logger); |
| | 2 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Initializes a new instance with pre-plugin registration callbacks and logger. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="settings">The host application builder settings to use.</param> |
| | | 114 | | /// <param name="prePluginRegistrationCallbacks">Callbacks to execute before plugin registration.</param> |
| | | 115 | | /// <param name="logger">The logger instance to use for logging during host creation.</param> |
| | | 116 | | /// <exception cref="ArgumentNullException">Thrown when a parameter is null.</exception> |
| | | 117 | | public CreateHostOptions( |
| | | 118 | | HostApplicationBuilderSettings settings, |
| | | 119 | | IEnumerable<Action<IServiceCollection>> prePluginRegistrationCallbacks, |
| | | 120 | | ILogger logger) |
| | 0 | 121 | | : this(settings, PostPluginRegistrationCallbacks: [], logger) |
| | | 122 | | { |
| | 0 | 123 | | ArgumentNullException.ThrowIfNull(prePluginRegistrationCallbacks); |
| | 0 | 124 | | PrePluginRegistrationCallbacks = prePluginRegistrationCallbacks.ToArray(); |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Initializes a new instance with a post-plugin registration callback and logger. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="settings">The host application builder settings to use.</param> |
| | | 131 | | /// <param name="postPluginRegistrationCallback"> |
| | | 132 | | /// The callback to execute after plugin registration, allowing for additional service configuration. |
| | | 133 | | /// </param> |
| | | 134 | | /// <param name="logger"> |
| | | 135 | | /// The logger instance to use for logging during host creation. |
| | | 136 | | /// </param> |
| | | 137 | | /// <exception cref="ArgumentNullException"> |
| | | 138 | | /// Thrown when <paramref name="postPluginRegistrationCallback"/> or <paramref name="logger"/> is null. |
| | | 139 | | /// </exception> |
| | | 140 | | public CreateHostOptions( |
| | | 141 | | HostApplicationBuilderSettings settings, |
| | | 142 | | Action<IServiceCollection> postPluginRegistrationCallback, |
| | | 143 | | ILogger logger) |
| | 1 | 144 | | : this(settings, PostPluginRegistrationCallbacks: [postPluginRegistrationCallback], logger) |
| | | 145 | | { |
| | 1 | 146 | | ArgumentNullException.ThrowIfNull(postPluginRegistrationCallback); |
| | 1 | 147 | | ArgumentNullException.ThrowIfNull(logger); |
| | 1 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Initializes a new instance with pre- and post-plugin registration callbacks and logger. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="settings">The host application builder settings to use.</param> |
| | | 154 | | /// <param name="prePluginRegistrationCallbacks">Callbacks to execute before plugin registration.</param> |
| | | 155 | | /// <param name="postPluginRegistrationCallbacks">Callbacks to execute after plugin registration.</param> |
| | | 156 | | /// <param name="logger">The logger instance to use for logging during host creation.</param> |
| | | 157 | | /// <exception cref="ArgumentNullException">Thrown when a parameter is null.</exception> |
| | | 158 | | public CreateHostOptions( |
| | | 159 | | HostApplicationBuilderSettings settings, |
| | | 160 | | IEnumerable<Action<IServiceCollection>> prePluginRegistrationCallbacks, |
| | | 161 | | IEnumerable<Action<IServiceCollection>> postPluginRegistrationCallbacks, |
| | | 162 | | ILogger logger) |
| | 1 | 163 | | : this(settings, PostPluginRegistrationCallbacks: (postPluginRegistrationCallbacks ?? throw new ArgumentNullExce |
| | | 164 | | { |
| | 1 | 165 | | ArgumentNullException.ThrowIfNull(prePluginRegistrationCallbacks); |
| | 1 | 166 | | PrePluginRegistrationCallbacks = prePluginRegistrationCallbacks.ToArray(); |
| | 1 | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Gets the default instance of <see cref="CreateHostOptions"/> with empty |
| | | 171 | | /// settings and a <see cref="NullLogger"/>. |
| | | 172 | | /// </summary> |
| | 26 | 173 | | public static CreateHostOptions Default => _defaultOptions; |
| | | 174 | | } |