using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ThreadPilot.ViewModels;
namespace ThreadPilot.Services
{
///
/// 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();
// 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();
// Data access services
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();
return services;
}
///
/// Configure process monitoring and management services
///
private static IServiceCollection ConfigureProcessManagementServices(this IServiceCollection services)
{
// Process monitoring services
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// Game boost and optimization services
services.AddSingleton();
// Game detection and performance monitoring services
services.AddSingleton();
services.AddSingleton();
return services;
}
///
/// Configure application-level services (settings, notifications, etc.)
///
private static IServiceCollection ConfigureApplicationLevelServices(this IServiceCollection services)
{
// Application configuration and settings
services.AddSingleton();
// User interface services
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// System integration services
services.AddSingleton();
// Security and elevation services
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.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;
}
}
}
}