/*
* 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.Services
{
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ThreadPilot.Services.Abstractions;
using ThreadPilot.ViewModels;
///
/// Centralized service configuration for dependency injection.
///
public static class ServiceConfiguration
{
///
/// Configure all application services.
///
public static IServiceCollection ConfigureApplicationServices(this IServiceCollection services)
{
// Configure service infrastructure
services.ConfigureServiceInfrastructure();
// Configure core system services
services.ConfigureCoreSystemServices();
// Configure process management services
services.ConfigureProcessManagementServices();
// Configure application services
services.ConfigureApplicationLevelServices();
// Configure presentation layer
services.ConfigurePresentationLayer();
return services;
}
///
/// Configure service infrastructure (logging, factories, etc.)
///
private static IServiceCollection ConfigureServiceInfrastructure(this IServiceCollection services)
{
// Logging infrastructure
services.AddLogging(builder =>
{
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Information);
});
// Enhanced logging service
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton(sp =>
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("ThreadPilot", "1.0"));
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
return httpClient;
});
services.AddSingleton();
services.AddSingleton();
// Memory caching for performance - PERFORMANCE IMPROVEMENT
services.AddMemoryCache();
// Service lifecycle management - PERFORMANCE IMPROVEMENT
services.AddSingleton();
services.AddSingleton();
// Error recovery and retry policies - RELIABILITY IMPROVEMENT
services.AddSingleton();
// Service factory for advanced service management
services.AddSingleton();
return services;
}
///
/// Configure core system services that interact directly with the OS.
///
private static IServiceCollection ConfigureCoreSystemServices(this IServiceCollection services)
{
// Core system interaction services
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// CoreMaskService needs IServiceProvider for checking profile references
services.AddSingleton(sp =>
{
var logger = sp.GetRequiredService>();
var cpuTopologyService = sp.GetRequiredService();
return new CoreMaskService(logger, cpuTopologyService, sp);
});
return services;
}
///
/// Configure process monitoring and management services.
///
private static IServiceCollection ConfigureProcessManagementServices(this IServiceCollection services)
{
// Process monitoring services
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// Performance monitoring services
services.AddSingleton();
return services;
}
///
/// Configure application-level services (settings, notifications, etc.)
///
private static IServiceCollection ConfigureApplicationLevelServices(this IServiceCollection services)
{
// Application configuration and settings
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// User interface services
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// System integration services
services.AddSingleton();
// System optimization services
services.AddSingleton();
// Security and elevation services
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// System tweaks service
services.AddSingleton();
// Keyboard shortcut service
services.AddSingleton();
return services;
}
///
/// Configure presentation layer (ViewModels and Views).
///
private static IServiceCollection ConfigurePresentationLayer(this IServiceCollection services)
{
// ViewModel factory for centralized ViewModel management
services.AddViewModelFactory();
// ViewModels - ProcessViewModel as Singleton to share state across views, others as Transient
services.AddSingleton();
services.AddSingleton();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
// Views - Transient for proper lifecycle management
services.AddTransient();
return services;
}
///
/// Validate service configuration.
///
public static void ValidateServiceConfiguration(IServiceProvider serviceProvider)
{
var loggerFactory = serviceProvider.GetRequiredService();
var logger = loggerFactory.CreateLogger("ServiceConfiguration");
try
{
// Validate core services can be resolved
var coreServices = new[]
{
typeof(IProcessService),
typeof(IPowerPlanService),
typeof(ICpuTopologyService),
typeof(IEnhancedLoggingService),
typeof(IApplicationSettingsService),
};
foreach (var serviceType in coreServices)
{
var service = serviceProvider.GetRequiredService(serviceType);
if (service == null)
{
throw new InvalidOperationException($"Failed to resolve required service: {serviceType.Name}");
}
}
logger.LogInformation("Service configuration validation completed successfully");
}
catch (Exception ex)
{
logger.LogError(ex, "Service configuration validation failed");
throw;
}
}
}
}