From 36fe23eba5e767f13bd8f479e498d668c8e53551 Mon Sep 17 00:00:00 2001 From: acidicMercury8 Date: Sat, 23 Nov 2024 23:52:00 +0300 Subject: [PATCH] Integrate dependency injection --- Directory.Packages.Props | 1 + .../App.axaml.cs | 36 ++++++++++++++----- ...Dex.AnomalyCustom.Launcher.Avalonia.csproj | 3 +- .../Extensions/ServiceCollectionExtensions.cs | 27 ++++++++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/Dex.AnomalyCustom.Launcher.Avalonia/Extensions/ServiceCollectionExtensions.cs diff --git a/Directory.Packages.Props b/Directory.Packages.Props index 36c62fb..a8202d2 100644 --- a/Directory.Packages.Props +++ b/Directory.Packages.Props @@ -10,5 +10,6 @@ + diff --git a/src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs b/src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs index f6654cb..962d395 100644 --- a/src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs +++ b/src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs @@ -2,13 +2,20 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using Dex.AnomalyCustom.Launcher.Avalonia.Extensions; using Dex.AnomalyCustom.Launcher.Avalonia.ViewModels; using Dex.AnomalyCustom.Launcher.Avalonia.Views; +using Microsoft.Extensions.DependencyInjection; + +using System; + namespace Dex.AnomalyCustom.Launcher.Avalonia; public partial class App : Application { + private IServiceProvider _serviceProvider = null!; + public override void Initialize() { AvaloniaXamlLoader.Load(this); @@ -18,17 +25,30 @@ public override void OnFrameworkInitializationCompleted() { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - desktop.MainWindow = new MainWindow - { - DataContext = new MainViewModel() - }; + _serviceProvider = new ServiceCollection() + .AddSingleton() + .AddViews() + .AddViewModels() + .BuildServiceProvider(); + + var mainViewModel = _serviceProvider.GetRequiredService(); + var mainWindow = _serviceProvider.GetRequiredService(); + mainWindow.DataContext = mainViewModel; + + desktop.MainWindow = mainWindow; } else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform) { - singleViewPlatform.MainView = new MainView - { - DataContext = new MainViewModel() - }; + _serviceProvider = new ServiceCollection() + .AddViews() + .AddViewModels() + .BuildServiceProvider(); + + var mainViewModel = _serviceProvider.GetRequiredService(); + var mainView = _serviceProvider.GetRequiredService(); + mainView.DataContext = mainViewModel; + + singleViewPlatform.MainView = mainView; } base.OnFrameworkInitializationCompleted(); diff --git a/src/Dex.AnomalyCustom.Launcher.Avalonia/Dex.AnomalyCustom.Launcher.Avalonia.csproj b/src/Dex.AnomalyCustom.Launcher.Avalonia/Dex.AnomalyCustom.Launcher.Avalonia.csproj index b59457c..219e1fa 100644 --- a/src/Dex.AnomalyCustom.Launcher.Avalonia/Dex.AnomalyCustom.Launcher.Avalonia.csproj +++ b/src/Dex.AnomalyCustom.Launcher.Avalonia/Dex.AnomalyCustom.Launcher.Avalonia.csproj @@ -12,9 +12,10 @@ - + + diff --git a/src/Dex.AnomalyCustom.Launcher.Avalonia/Extensions/ServiceCollectionExtensions.cs b/src/Dex.AnomalyCustom.Launcher.Avalonia/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..b5c53f8 --- /dev/null +++ b/src/Dex.AnomalyCustom.Launcher.Avalonia/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,27 @@ +using Dex.AnomalyCustom.Launcher.Avalonia.ViewModels; +using Dex.AnomalyCustom.Launcher.Avalonia.Views; + +using Microsoft.Extensions.DependencyInjection; + +namespace Dex.AnomalyCustom.Launcher.Avalonia.Extensions; + +internal static class ServiceCollectionExtensions +{ + public static IServiceCollection AddViews(this IServiceCollection serviceCollection) + { + serviceCollection.AddSingleton(); + + serviceCollection.AddTransient(); + + return serviceCollection; + } + + public static IServiceCollection AddViewModels(this IServiceCollection serviceCollection) + { + serviceCollection.AddSingleton(); + + serviceCollection.AddTransient(); + + return serviceCollection; + } +}