| | | 1 | | using Microsoft.AspNetCore.Components; |
| | | 2 | | using Microsoft.AspNetCore.Components.Web; |
| | | 3 | | using Microsoft.AspNetCore.Components.WebAssembly.Hosting; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Pomodoro.Web; |
| | | 6 | | |
| | | 7 | | namespace Pomodoro.Web.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Interface for WebAssemblyHostBuilder to enable testing |
| | | 11 | | /// </summary> |
| | | 12 | | public interface IHostBuilderWrapper |
| | | 13 | | { |
| | | 14 | | IServiceCollection Services { get; } |
| | | 15 | | IHostEnvironmentWrapper HostEnvironment { get; } |
| | | 16 | | void AddRootComponent<TComponent>(string selector) where TComponent : IComponent; |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Interface for WebAssemblyHostEnvironment to enable testing |
| | | 21 | | /// </summary> |
| | | 22 | | public interface IHostEnvironmentWrapper |
| | | 23 | | { |
| | | 24 | | string BaseAddress { get; } |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Wrapper implementation for WebAssemblyHostBuilder |
| | | 29 | | /// </summary> |
| | | 30 | | public class WebAssemblyHostBuilderWrapper : IHostBuilderWrapper |
| | | 31 | | { |
| | | 32 | | private readonly WebAssemblyHostBuilder? _builder; |
| | | 33 | | private readonly IServiceCollection? _testServices; |
| | | 34 | | private readonly IWebAssemblyHostEnvironment? _testEnvironment; |
| | | 35 | | private readonly List<(Type ComponentType, string Selector)> _addedRootComponents = new(); |
| | | 36 | | |
| | | 37 | | public WebAssemblyHostBuilderWrapper(WebAssemblyHostBuilder builder) |
| | | 38 | | { |
| | | 39 | | _builder = builder; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | internal WebAssemblyHostBuilderWrapper(IServiceCollection services, IWebAssemblyHostEnvironment environment) |
| | | 43 | | { |
| | | 44 | | _testServices = services; |
| | | 45 | | _testEnvironment = environment; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public IServiceCollection Services => _builder?.Services ?? _testServices!; |
| | | 49 | | public IHostEnvironmentWrapper HostEnvironment => new WebAssemblyHostEnvironmentWrapper(_builder?.HostEnvironment ?? |
| | | 50 | | |
| | | 51 | | public void AddRootComponent<TComponent>(string selector) where TComponent : IComponent |
| | | 52 | | { |
| | | 53 | | if (_builder != null) |
| | | 54 | | { |
| | | 55 | | _builder.RootComponents.Add<TComponent>(selector); |
| | | 56 | | } |
| | | 57 | | _addedRootComponents.Add((typeof(TComponent), selector)); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | internal IReadOnlyList<(Type ComponentType, string Selector)> AddedRootComponents => _addedRootComponents; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Wrapper implementation for WebAssemblyHostEnvironment |
| | | 65 | | /// </summary> |
| | | 66 | | public class WebAssemblyHostEnvironmentWrapper : IHostEnvironmentWrapper |
| | | 67 | | { |
| | | 68 | | private readonly IWebAssemblyHostEnvironment _environment; |
| | | 69 | | |
| | | 70 | | public WebAssemblyHostEnvironmentWrapper(IWebAssemblyHostEnvironment environment) |
| | | 71 | | { |
| | | 72 | | _environment = environment; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public string BaseAddress => _environment.BaseAddress; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Service responsible for handling application startup and host configuration |
| | | 80 | | /// </summary> |
| | | 81 | | public class ApplicationStartupService : IApplicationStartupService |
| | | 82 | | { |
| | | 83 | | private readonly ILogger<ApplicationStartupService>? _logger; |
| | | 84 | | private readonly ILogger<ServiceRegistrationService>? _serviceRegistrationLogger; |
| | | 85 | | |
| | 25 | 86 | | public ApplicationStartupService(ILogger<ApplicationStartupService>? logger = null, ILogger<ServiceRegistrationServi |
| | 25 | 87 | | { |
| | 25 | 88 | | _logger = logger; |
| | 25 | 89 | | _serviceRegistrationLogger = serviceRegistrationLogger; |
| | 25 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Configures the WebAssemblyHostBuilder with root components and services |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="builder">The WebAssemblyHostBuilder to configure</param> |
| | | 96 | | [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] |
| | | 97 | | public virtual void ConfigureHostBuilder(WebAssemblyHostBuilder builder) |
| | | 98 | | { |
| | | 99 | | ConfigureHostBuilder(new WebAssemblyHostBuilderWrapper(builder)); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Configures the WebAssemblyHostBuilder with root components and services (testable overload) |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="builder">The wrapped builder to configure</param> |
| | | 106 | | public virtual void ConfigureHostBuilder(IHostBuilderWrapper builder) |
| | 1 | 107 | | { |
| | 1 | 108 | | ConfigureHostBuilderInternal(builder); |
| | 1 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Internal implementation that uses the wrapper interface for testability |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="builder">The wrapped builder to configure</param> |
| | | 115 | | protected virtual void ConfigureHostBuilderInternal(IHostBuilderWrapper builder) |
| | 4 | 116 | | { |
| | | 117 | | // Add root components |
| | 4 | 118 | | builder.AddRootComponent<App>(Constants.Blazor.AppRootSelector); |
| | 4 | 119 | | builder.AddRootComponent<HeadOutlet>(Constants.Blazor.HeadOutletSelector); |
| | | 120 | | |
| | | 121 | | // Add HttpClient |
| | 4 | 122 | | ConfigureHttpClient(builder.Services, builder.HostEnvironment.BaseAddress); |
| | | 123 | | |
| | | 124 | | // Configure logging |
| | 4 | 125 | | ConfigureLogging(builder.Services); |
| | | 126 | | |
| | | 127 | | // Register infrastructure services for testability |
| | 4 | 128 | | RegisterInfrastructureServices(builder.Services); |
| | | 129 | | |
| | | 130 | | // Register all application services |
| | 4 | 131 | | RegisterApplicationServices(builder.Services); |
| | 4 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Configures services for application (extracted for testability) |
| | | 136 | | /// This method contains the core service configuration logic from ConfigureHostBuilder |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="services">The service collection to configure</param> |
| | | 139 | | /// <param name="baseAddress">The base address for HttpClient</param> |
| | | 140 | | public virtual void ConfigureServices(IServiceCollection services, string baseAddress) |
| | 2 | 141 | | { |
| | | 142 | | // Add HttpClient |
| | 2 | 143 | | ConfigureHttpClient(services, baseAddress); |
| | | 144 | | |
| | | 145 | | // Configure logging |
| | 2 | 146 | | ConfigureLogging(services); |
| | | 147 | | |
| | | 148 | | // Register infrastructure services for testability |
| | 2 | 149 | | RegisterInfrastructureServices(services); |
| | | 150 | | |
| | | 151 | | // Register all application services |
| | 2 | 152 | | RegisterApplicationServices(services); |
| | 2 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Configures the HTTP client with the base address |
| | | 157 | | /// </summary> |
| | | 158 | | protected virtual void ConfigureHttpClient(IServiceCollection services, string baseAddress) |
| | 7 | 159 | | { |
| | 9 | 160 | | services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseAddress) }); |
| | 7 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Initializes and runs the application host |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="builder">The configured WebAssemblyHostBuilder</param> |
| | | 167 | | /// <returns>A task representing the asynchronous operation</returns> |
| | | 168 | | [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] |
| | | 169 | | public virtual async Task InitializeAndRunHostAsync(WebAssemblyHostBuilder builder) |
| | | 170 | | { |
| | | 171 | | var host = builder.Build(); |
| | | 172 | | |
| | | 173 | | // Initialize services with error handling |
| | | 174 | | await InitializeServicesWithErrorHandlingAsync(host.Services); |
| | | 175 | | |
| | | 176 | | await host.RunAsync(); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Configures logging services |
| | | 181 | | /// </summary> |
| | | 182 | | /// <param name="services">The service collection to configure</param> |
| | | 183 | | protected virtual void ConfigureLogging(IServiceCollection services) |
| | 7 | 184 | | { |
| | 7 | 185 | | services.AddLogging(builder => |
| | 7 | 186 | | { |
| | 7 | 187 | | #if DEBUG |
| | 7 | 188 | | builder.SetMinimumLevel(LogLevel.Information); |
| | 7 | 189 | | #else |
| | 7 | 190 | | builder.SetMinimumLevel(LogLevel.Warning); |
| | 7 | 191 | | #endif |
| | 7 | 192 | | builder.AddFilter(Constants.Logging.MicrosoftCategory, LogLevel.Warning); |
| | 7 | 193 | | builder.AddFilter(Constants.Logging.SystemCategory, LogLevel.Warning); |
| | 14 | 194 | | }); |
| | 7 | 195 | | } |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Registers infrastructure services for testability |
| | | 199 | | /// </summary> |
| | | 200 | | /// <param name="services">The service collection to configure</param> |
| | | 201 | | protected virtual void RegisterInfrastructureServices(IServiceCollection services) |
| | 12 | 202 | | { |
| | | 203 | | // Register service registration service for testability |
| | 12 | 204 | | services.AddScoped<IServiceRegistrationService, ServiceRegistrationService>(); |
| | | 205 | | |
| | | 206 | | // Register service initialization service for testability |
| | 12 | 207 | | services.AddScoped<IServiceInitializationService, ServiceInitializationService>(); |
| | | 208 | | |
| | | 209 | | // Register event wiring service for testability |
| | 12 | 210 | | services.AddScoped<IEventWiringService, EventWiringService>(); |
| | | 211 | | |
| | | 212 | | // Register layout presenter service for testability |
| | 12 | 213 | | services.AddScoped<LayoutPresenterService, LayoutPresenterService>(); |
| | | 214 | | |
| | | 215 | | // Register this service for testability |
| | 12 | 216 | | services.AddScoped<IApplicationStartupService, ApplicationStartupService>(); |
| | 12 | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Registers all application services |
| | | 221 | | /// </summary> |
| | | 222 | | /// <param name="services">The service collection to configure</param> |
| | | 223 | | protected virtual void RegisterApplicationServices(IServiceCollection services) |
| | 3 | 224 | | { |
| | | 225 | | // Register all application services through ServiceRegistrationService |
| | 3 | 226 | | var serviceRegistration = new ServiceRegistrationService(_serviceRegistrationLogger); |
| | 3 | 227 | | serviceRegistration.RegisterServices(services); |
| | 3 | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Initializes services with error handling |
| | | 232 | | /// </summary> |
| | | 233 | | /// <param name="services">The service provider to retrieve services from</param> |
| | | 234 | | /// <returns>A task representing the asynchronous operation</returns> |
| | | 235 | | protected virtual async Task InitializeServicesWithErrorHandlingAsync(IServiceProvider services) |
| | 4 | 236 | | { |
| | | 237 | | try |
| | 4 | 238 | | { |
| | | 239 | | // Initialize services through ServiceInitializationService |
| | 4 | 240 | | var serviceInitialization = services.GetRequiredService<IServiceInitializationService>(); |
| | 4 | 241 | | await serviceInitialization.InitializeServicesAsync(services); |
| | | 242 | | |
| | | 243 | | // Wire up event subscribers through EventWiringService |
| | 2 | 244 | | var eventWiring = services.GetRequiredService<IEventWiringService>(); |
| | 2 | 245 | | eventWiring.WireEventSubscribers(services); |
| | 1 | 246 | | } |
| | 3 | 247 | | catch (Exception ex) |
| | 3 | 248 | | { |
| | | 249 | | // Log initialization error - app will still run but may have limited functionality |
| | 3 | 250 | | _logger?.LogWarning(ex, Constants.Messages.ServiceInitializationFailed); |
| | 3 | 251 | | } |
| | 4 | 252 | | } |
| | | 253 | | } |