Files
ThreadPilot/ViewModels/ViewModelFactory.cs
T

151 lines
4.9 KiB
C#

/*
* 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 <https://www.gnu.org/licenses/>.
*/
namespace ThreadPilot.ViewModels
{
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ThreadPilot.Services;
/// <summary>
/// Factory for creating and managing ViewModel instances.
/// </summary>
public interface IViewModelFactory
{
/// <summary>
/// Create a ViewModel instance of the specified type.
/// </summary>
T CreateViewModel<T>()
where T : BaseViewModel;
/// <summary>
/// Create a ViewModel instance with initialization.
/// </summary>
Task<T> CreateAndInitializeViewModelAsync<T>()
where T : BaseViewModel;
/// <summary>
/// Dispose all managed ViewModels.
/// </summary>
void DisposeAllViewModels();
}
/// <summary>
/// Implementation of ViewModel factory with dependency injection support.
/// </summary>
public class ViewModelFactory : IViewModelFactory, IDisposable
{
private readonly IServiceProvider serviceProvider;
private readonly ILogger<ViewModelFactory> logger;
private readonly List<BaseViewModel> managedViewModels = new();
private bool disposed;
public ViewModelFactory(IServiceProvider serviceProvider, ILogger<ViewModelFactory> logger)
{
this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public T CreateViewModel<T>()
where T : BaseViewModel
{
try
{
var viewModel = this.serviceProvider.GetRequiredService<T>();
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<T> CreateAndInitializeViewModelAsync<T>()
where T : BaseViewModel
{
var viewModel = this.CreateViewModel<T>();
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;
}
}
}
/// <summary>
/// Extension methods for ViewModel factory registration.
/// </summary>
public static class ViewModelFactoryExtensions
{
/// <summary>
/// Register ViewModel factory in dependency injection container.
/// </summary>
public static IServiceCollection AddViewModelFactory(this IServiceCollection services)
{
services.AddSingleton<IViewModelFactory, ViewModelFactory>();
return services;
}
}
}