| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.Hosting; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.Injection; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.Hosting; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Extension methods for configuring <see cref="HostSyringe"/> instances. |
| | | 10 | | /// Provides only host-specific configuration methods. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Remember to use base <see cref="Syringe"/> extension methods BEFORE transitioning to host mode. |
| | | 14 | | /// </remarks> |
| | | 15 | | /// <example> |
| | | 16 | | /// Complete host configuration: |
| | | 17 | | /// <code> |
| | | 18 | | /// var host = new Syringe() |
| | | 19 | | /// // Transition to host mode |
| | | 20 | | /// .ForHost() |
| | | 21 | | /// // Configure host-specific options |
| | | 22 | | /// .UsingOptions(() => CreateHostOptions.Default |
| | | 23 | | /// .UsingArgs(args) |
| | | 24 | | /// .UsingApplicationName("My Worker Service") |
| | | 25 | | /// .UsingStartupConsoleLogger()) |
| | | 26 | | /// .UsingConfigurationCallback((builder, options) => |
| | | 27 | | /// { |
| | | 28 | | /// // Configure the HostApplicationBuilder |
| | | 29 | | /// builder.Configuration.AddJsonFile("custom-settings.json", optional: true); |
| | | 30 | | /// builder.Services.AddSingleton<IMyCustomService, MyCustomService>(); |
| | | 31 | | /// }) |
| | | 32 | | /// .BuildHost(); |
| | | 33 | | /// |
| | | 34 | | /// await host.RunAsync(); |
| | | 35 | | /// </code> |
| | | 36 | | /// |
| | | 37 | | /// Minimal host: |
| | | 38 | | /// <code> |
| | | 39 | | /// var host = new Syringe() |
| | | 40 | | /// .ForHost() |
| | | 41 | | /// .BuildHost(); |
| | | 42 | | /// </code> |
| | | 43 | | /// </example> |
| | | 44 | | public static class HostSyringeExtensions |
| | | 45 | | { |
| | | 46 | | /// <summary> |
| | | 47 | | /// Configures the <see cref="HostSyringe"/> to use the specified host options factory. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="syringe">The <see cref="HostSyringe"/> to configure.</param> |
| | | 50 | | /// <param name="optionsFactory">The factory function for creating host options.</param> |
| | | 51 | | /// <returns>A new configured <see cref="HostSyringe"/> instance.</returns> |
| | | 52 | | /// <example> |
| | | 53 | | /// <code> |
| | | 54 | | /// var hostSyringe = syringe |
| | | 55 | | /// .ForHost() |
| | | 56 | | /// .UsingOptions(() => CreateHostOptions.Default |
| | | 57 | | /// .UsingArgs(args) |
| | | 58 | | /// .UsingApplicationName("My App") |
| | | 59 | | /// .UsingStartupConsoleLogger()); |
| | | 60 | | /// </code> |
| | | 61 | | /// </example> |
| | | 62 | | public static HostSyringe UsingOptions( |
| | | 63 | | this HostSyringe syringe, |
| | | 64 | | Func<CreateHostOptions> optionsFactory) |
| | | 65 | | { |
| | 7 | 66 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 7 | 67 | | ArgumentNullException.ThrowIfNull(optionsFactory); |
| | | 68 | | |
| | 6 | 69 | | return syringe with { OptionsFactory = optionsFactory }; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Configures the <see cref="HostSyringe"/> to use the specified host factory. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="syringe">The <see cref="HostSyringe"/> to configure.</param> |
| | | 76 | | /// <param name="factory">The factory function for creating host factories.</param> |
| | | 77 | | /// <returns>A new configured <see cref="HostSyringe"/> instance.</returns> |
| | | 78 | | /// <example> |
| | | 79 | | /// <code> |
| | | 80 | | /// var hostSyringe = syringe |
| | | 81 | | /// .ForHost() |
| | | 82 | | /// .UsingHostFactory((serviceProviderBuilder, serviceCollectionPopulator) => |
| | | 83 | | /// { |
| | | 84 | | /// // Custom factory logic here |
| | | 85 | | /// return new CustomHostFactory(serviceProviderBuilder, serviceCollectionPopulator); |
| | | 86 | | /// }); |
| | | 87 | | /// </code> |
| | | 88 | | /// </example> |
| | | 89 | | public static HostSyringe UsingHostFactory( |
| | | 90 | | this HostSyringe syringe, |
| | | 91 | | Func<IServiceProviderBuilder, IServiceCollectionPopulator, IHostFactory> factory) |
| | | 92 | | { |
| | 1 | 93 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 1 | 94 | | ArgumentNullException.ThrowIfNull(factory); |
| | | 95 | | |
| | 0 | 96 | | return syringe with { HostFactoryCreator = factory }; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Configures the <see cref="HostSyringe"/> to use a callback for configuring the <see cref="HostApplicationBuilder |
| | | 101 | | /// This allows for custom configuration of the builder, such as modifying the <see cref="ConfigurationBuilder"/> |
| | | 102 | | /// or adding additional services before the host is built. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="syringe">The <see cref="HostSyringe"/> to configure.</param> |
| | | 105 | | /// <param name="configureCallback">The callback to configure the <see cref="HostApplicationBuilder"/>.</param> |
| | | 106 | | /// <returns>A new configured <see cref="HostSyringe"/> instance.</returns> |
| | | 107 | | /// <example> |
| | | 108 | | /// <code> |
| | | 109 | | /// var hostSyringe = syringe |
| | | 110 | | /// .ForHost() |
| | | 111 | | /// .UsingConfigurationCallback((builder, options) => |
| | | 112 | | /// { |
| | | 113 | | /// // Add custom configuration sources |
| | | 114 | | /// builder.Configuration.AddJsonFile("appsettings.local.json", optional: true); |
| | | 115 | | /// builder.Configuration.AddEnvironmentVariables("MYAPP_"); |
| | | 116 | | /// |
| | | 117 | | /// // Register additional services that need to be available before plugin registration |
| | | 118 | | /// builder.Services.AddSingleton<ICustomConfigurationService, CustomConfigurationService>(); |
| | | 119 | | /// |
| | | 120 | | /// // Configure logging |
| | | 121 | | /// builder.Services.AddLogging(logging => |
| | | 122 | | /// { |
| | | 123 | | /// logging.AddConsole(); |
| | | 124 | | /// logging.SetMinimumLevel(LogLevel.Debug); |
| | | 125 | | /// }); |
| | | 126 | | /// }); |
| | | 127 | | /// </code> |
| | | 128 | | /// </example> |
| | | 129 | | public static HostSyringe UsingConfigurationCallback( |
| | | 130 | | this HostSyringe syringe, |
| | | 131 | | Action<HostApplicationBuilder, CreateHostOptions> configureCallback) |
| | | 132 | | { |
| | 4 | 133 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 4 | 134 | | ArgumentNullException.ThrowIfNull(configureCallback); |
| | | 135 | | |
| | 3 | 136 | | return syringe with { ConfigureCallback = configureCallback }; |
| | | 137 | | } |
| | | 138 | | } |