/* * ThreadPilot - Advanced Windows Process and Power Plan Manager * Copyright (C) 2025 Prime Build * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3 only. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace ThreadPilot.ViewModels { using System; using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using ThreadPilot.Services; /// /// Factory for creating and managing ViewModel instances. /// public interface IViewModelFactory { /// /// Create a ViewModel instance of the specified type. /// T CreateViewModel() where T : BaseViewModel; /// /// Create a ViewModel instance with initialization. /// Task CreateAndInitializeViewModelAsync() where T : BaseViewModel; /// /// Dispose all managed ViewModels. /// void DisposeAllViewModels(); } /// /// Implementation of ViewModel factory with dependency injection support. /// public class ViewModelFactory : IViewModelFactory, IDisposable { private readonly IServiceProvider serviceProvider; private readonly ILogger logger; private readonly List managedViewModels = new(); private bool disposed; public ViewModelFactory(IServiceProvider serviceProvider, ILogger logger) { this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public T CreateViewModel() where T : BaseViewModel { try { var viewModel = this.serviceProvider.GetRequiredService(); this.managedViewModels.Add(viewModel); this.logger.LogDebug("Created ViewModel of type {ViewModelType}", typeof(T).Name); return viewModel; } catch (Exception ex) { this.logger.LogError(ex, "Failed to create ViewModel of type {ViewModelType}", typeof(T).Name); throw; } } public async Task CreateAndInitializeViewModelAsync() where T : BaseViewModel { var viewModel = this.CreateViewModel(); try { await viewModel.InitializeAsync(); this.logger.LogDebug("Initialized ViewModel of type {ViewModelType}", typeof(T).Name); return viewModel; } catch (Exception ex) { this.logger.LogError(ex, "Failed to initialize ViewModel of type {ViewModelType}", typeof(T).Name); // Dispose the failed ViewModel viewModel.Dispose(); this.managedViewModels.Remove(viewModel); throw; } } public void DisposeAllViewModels() { this.logger.LogInformation("Disposing all managed ViewModels"); foreach (var viewModel in this.managedViewModels) { try { viewModel.Dispose(); } catch (Exception ex) { this.logger.LogError(ex, "Error disposing ViewModel {ViewModelType}", viewModel.GetType().Name); } } this.managedViewModels.Clear(); this.logger.LogInformation("All managed ViewModels disposed"); } public void Dispose() { if (!this.disposed) { this.DisposeAllViewModels(); this.disposed = true; } } } /// /// Extension methods for ViewModel factory registration. /// public static class ViewModelFactoryExtensions { /// /// Register ViewModel factory in dependency injection container. /// public static IServiceCollection AddViewModelFactory(this IServiceCollection services) { services.AddSingleton(); return services; } } }