| | | 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 | | /// Provides a fluent API for configuring and building host applications using Needlr. |
| | | 10 | | /// Wraps a ConfiguredSyringe with additional host functionality. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <example> |
| | | 13 | | /// Creating and configuring a HostSyringe: |
| | | 14 | | /// <code> |
| | | 15 | | /// // Transition from ConfiguredSyringe |
| | | 16 | | /// var hostSyringe = new Syringe() |
| | | 17 | | /// .UsingReflection() |
| | | 18 | | /// .ForHost(); |
| | | 19 | | /// |
| | | 20 | | /// // Build and run the host |
| | | 21 | | /// var host = hostSyringe |
| | | 22 | | /// .UsingOptions(() => CreateHostOptions.Default.UsingArgs(args)) |
| | | 23 | | /// .UsingConfigurationCallback((builder, options) => |
| | | 24 | | /// { |
| | | 25 | | /// // Configure the HostApplicationBuilder |
| | | 26 | | /// builder.Configuration.AddJsonFile("custom-settings.json", optional: true); |
| | | 27 | | /// builder.Services.AddSingleton<IMyCustomService, MyCustomService>(); |
| | | 28 | | /// }) |
| | | 29 | | /// .BuildHost(); |
| | | 30 | | /// |
| | | 31 | | /// await host.RunAsync(); |
| | | 32 | | /// </code> |
| | | 33 | | /// </example> |
| | | 34 | | [DoNotAutoRegister] |
| | | 35 | | public sealed record HostSyringe |
| | | 36 | | { |
| | 109 | 37 | | internal ConfiguredSyringe BaseSyringe { get; init; } |
| | 17 | 38 | | internal Func<CreateHostOptions>? OptionsFactory { get; init; } |
| | 11 | 39 | | internal Func<IServiceProviderBuilder, IServiceCollectionPopulator, IHostFactory>? HostFactoryCreator { get; init; } |
| | 14 | 40 | | internal Action<HostApplicationBuilder, CreateHostOptions>? ConfigureCallback { get; init; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Initializes a new instance of the <see cref="HostSyringe"/> class. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="baseSyringe">The configured syringe to wrap.</param> |
| | | 46 | | /// <example> |
| | | 47 | | /// <code> |
| | | 48 | | /// var configuredSyringe = new Syringe().UsingReflection(); |
| | | 49 | | /// |
| | | 50 | | /// var hostSyringe = new HostSyringe(configuredSyringe); |
| | | 51 | | /// </code> |
| | | 52 | | /// </example> |
| | 22 | 53 | | public HostSyringe(ConfiguredSyringe baseSyringe) |
| | | 54 | | { |
| | 22 | 55 | | ArgumentNullException.ThrowIfNull(baseSyringe); |
| | 21 | 56 | | BaseSyringe = baseSyringe; |
| | 21 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Builds a host with the configured settings. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <returns>The configured <see cref="IHost"/>.</returns> |
| | | 63 | | /// <example> |
| | | 64 | | /// <code> |
| | | 65 | | /// var host = new Syringe() |
| | | 66 | | /// .UsingReflection() |
| | | 67 | | /// .UsingScrutorTypeRegistrar() |
| | | 68 | | /// .ForHost() |
| | | 69 | | /// .UsingOptions(() => CreateHostOptions.Default |
| | | 70 | | /// .UsingArgs(args) |
| | | 71 | | /// .UsingApplicationName("My Worker Service")) |
| | | 72 | | /// .UsingConfigurationCallback((builder, options) => |
| | | 73 | | /// { |
| | | 74 | | /// // Add custom configuration sources |
| | | 75 | | /// builder.Configuration.AddJsonFile("appsettings.local.json", optional: true); |
| | | 76 | | /// |
| | | 77 | | /// // Register additional services |
| | | 78 | | /// builder.Services.AddSingleton<ICustomService, CustomService>(); |
| | | 79 | | /// }) |
| | | 80 | | /// .BuildHost(); |
| | | 81 | | /// |
| | | 82 | | /// await host.RunAsync(); |
| | | 83 | | /// </code> |
| | | 84 | | /// </example> |
| | | 85 | | public IHost BuildHost() |
| | | 86 | | { |
| | 11 | 87 | | var typeRegistrar = BaseSyringe.GetOrCreateTypeRegistrar(); |
| | 11 | 88 | | var typeFilterer = BaseSyringe.GetOrCreateTypeFilterer(); |
| | 11 | 89 | | var pluginFactory = BaseSyringe.GetOrCreatePluginFactory(); |
| | 11 | 90 | | var serviceCollectionPopulator = BaseSyringe.GetOrCreateServiceCollectionPopulator(typeRegistrar, typeFilterer, |
| | 11 | 91 | | var assemblyProvider = BaseSyringe.GetOrCreateAssemblyProvider(); |
| | 11 | 92 | | var additionalAssemblies = BaseSyringe.GetAdditionalAssemblies(); |
| | 11 | 93 | | var callbacks = BaseSyringe.GetPostPluginRegistrationCallbacks(); |
| | | 94 | | |
| | 11 | 95 | | var serviceProviderBuilder = BaseSyringe.GetOrCreateServiceProviderBuilder( |
| | 11 | 96 | | serviceCollectionPopulator, |
| | 11 | 97 | | assemblyProvider, |
| | 11 | 98 | | additionalAssemblies); |
| | | 99 | | |
| | 11 | 100 | | var hostFactory = GetOrCreateHostFactory(serviceProviderBuilder, serviceCollectionPopulator, pluginFactory); |
| | 11 | 101 | | var options = GetOrCreateOptions(); |
| | 11 | 102 | | if (callbacks.Count > 0) |
| | | 103 | | { |
| | 0 | 104 | | options = options.UsingPostPluginRegistrationCallbacks(callbacks); |
| | | 105 | | } |
| | | 106 | | |
| | 11 | 107 | | return hostFactory.Create( |
| | 11 | 108 | | options, |
| | 11 | 109 | | ConfigureCallback); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Builds a service provider with the configured settings. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="config">The configuration to use for building the service provider.</param> |
| | | 116 | | /// <returns>The configured <see cref="IServiceProvider"/>.</returns> |
| | | 117 | | /// <example> |
| | | 118 | | /// <code> |
| | | 119 | | /// var config = new ConfigurationBuilder() |
| | | 120 | | /// .AddJsonFile("appsettings.json") |
| | | 121 | | /// .Build(); |
| | | 122 | | /// |
| | | 123 | | /// var serviceProvider = new Syringe() |
| | | 124 | | /// .UsingReflection() |
| | | 125 | | /// .UsingScrutorTypeRegistrar() |
| | | 126 | | /// .ForHost() |
| | | 127 | | /// .BuildServiceProvider(config); |
| | | 128 | | /// |
| | | 129 | | /// var myService = serviceProvider.GetRequiredService<IMyService>(); |
| | | 130 | | /// </code> |
| | | 131 | | /// </example> |
| | | 132 | | public IServiceProvider BuildServiceProvider(IConfiguration config) |
| | | 133 | | { |
| | 0 | 134 | | return BaseSyringe.BuildServiceProvider(config); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | private IHostFactory GetOrCreateHostFactory( |
| | | 138 | | IServiceProviderBuilder serviceProviderBuilder, |
| | | 139 | | IServiceCollectionPopulator serviceCollectionPopulator, |
| | | 140 | | IPluginFactory pluginFactory) |
| | | 141 | | { |
| | 11 | 142 | | return HostFactoryCreator?.Invoke(serviceProviderBuilder, serviceCollectionPopulator) |
| | 11 | 143 | | ?? new HostFactory(serviceProviderBuilder, serviceCollectionPopulator, pluginFactory); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | private CreateHostOptions GetOrCreateOptions() |
| | | 147 | | { |
| | 11 | 148 | | return OptionsFactory?.Invoke() ?? CreateHostOptions.Default; |
| | | 149 | | } |
| | | 150 | | } |