// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Copyright (C) LibreHardwareMonitor and Contributors.
using LibreHardwareMonitor.Hardware;
using LibreHardwareMonitor.Windows.WinUI.Services;
using LibreHardwareMonitor.Windows.WinUI.Services.Tracing;
using LibreHardwareMonitor.Windows.WinUI.ViewModels;
using Microsoft.Extensions.DependencyInjection;
namespace LibreHardwareMonitor.Windows.WinUI.Composition;
///
/// Single composition root for the application's service graph. Registers configuration, startup
/// tracing, the domain-tier services, and the main view model. Runtime/window-tied services are
/// produced separately by IMainWindowRuntimeFactory once the window exists.
///
internal static class ServiceCollectionExtensions
{
public static IServiceCollection AddAppServices(this IServiceCollection services, IStartupTracer startupTracer)
{
// Configuration root and the (already-created) startup tracer instance.
services.AddSingleton(startupTracer);
services.AddSingleton(_ => AppSettings.LoadDefault());
// Domain tier.
services.AddSingleton();
services.AddSingleton(sp => sp.GetRequiredService().Computer);
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton(sp =>
{
AppSettings settings = sp.GetRequiredService();
IHardwareMonitorService hardwareMonitor = sp.GetRequiredService();
return new RemoteWebServer(
hardwareMonitor.Computer,
hardwareMonitor.SensorReadLock,
settings.GetValue("listenerIp", "?"),
settings.GetValue("listenerPort", 8085),
settings.GetValue("authenticationEnabled", false),
settings.GetValue("authenticationUserName", ""),
settings.GetValue("authenticationPassword", ""));
});
// View model (explicit factory so the injected internal constructor is used, never the
// transitional convenience constructor).
services.AddSingleton(sp => new MainWindowViewModel(
sp.GetRequiredService(),
sp.GetRequiredService(),
sp.GetRequiredService(),
sp.GetRequiredService(),
sp.GetRequiredService(),
sp.GetRequiredService(),
sp.GetRequiredService(),
sp.GetRequiredService()));
// Runtime/window-tied service factory. The window itself is constructed by App (kept out of the
// container so the XAML-tied Window is not a container-managed singleton).
services.AddSingleton();
return services;
}
}