GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor: change `ServiceLocator.Provider` getter to private; expose methods for getting services
Lars Bergera06feae288b6
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.csindex 0a87902f..289199cd 100644--- a/GlazeWM.Bar/BarViewModel.cs+++ b/GlazeWM.Bar/BarViewModel.cs@@ -6,7 +6,6 @@ using GlazeWM.Bar.Components;using GlazeWM.Domain.Monitors;using GlazeWM.Domain.UserConfigs;using GlazeWM.Infrastructure;-using Microsoft.Extensions.DependencyInjection;namespace GlazeWM.Bar{@@ -16,7 +15,7 @@ namespace GlazeWM.Barpublic Monitor Monitor { get; set; }private readonly UserConfigService _userConfigService =- ServiceLocator.Provider.GetRequiredService<UserConfigService>();+ ServiceLocator.GetRequiredService<UserConfigService>();private BarConfig _barConfig => _userConfigService.BarConfig;public BarPosition Position => _barConfig.Position;diff --git a/GlazeWM.Bar/Components/TextComponentViewModel.cs b/GlazeWM.Bar/Components/TextComponentViewModel.csindex 8c1be571..975d1d86 100644--- a/GlazeWM.Bar/Components/TextComponentViewModel.cs+++ b/GlazeWM.Bar/Components/TextComponentViewModel.cs@@ -4,17 +4,16 @@ using GlazeWM.Domain.Containers;using GlazeWM.Domain.UserConfigs;using GlazeWM.Infrastructure;using GlazeWM.Infrastructure.Bussing;-using Microsoft.Extensions.DependencyInjection;namespace GlazeWM.Bar.Components{public class TextComponentViewModel : ComponentViewModel{- private readonly Bus _bus = ServiceLocator.Provider.GetRequiredService<Bus>();+ private readonly Bus _bus = ServiceLocator.GetRequiredService<Bus>();private readonly ContainerService _containerService =- ServiceLocator.Provider.GetRequiredService<ContainerService>();+ ServiceLocator.GetRequiredService<ContainerService>();private readonly CommandParsingService _commandParsingService =- ServiceLocator.Provider.GetRequiredService<CommandParsingService>();+ ServiceLocator.GetRequiredService<CommandParsingService>();private TextComponentConfig _config => _componentConfig as TextComponentConfig;public string Text => _config.Text;diff --git a/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs b/GlazeWM.Bar/Components/WorkspacesComponentViewModel.csindex 8eccb589..5ad29b40 100644--- a/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs+++ b/GlazeWM.Bar/Components/WorkspacesComponentViewModel.cs@@ -14,7 +14,6 @@ using GlazeWM.Domain.Workspaces.Commands;using GlazeWM.Domain.Workspaces.Events;using GlazeWM.Infrastructure;using GlazeWM.Infrastructure.Bussing;-using Microsoft.Extensions.DependencyInjection;namespace GlazeWM.Bar.Components{@@ -23,9 +22,9 @@ namespace GlazeWM.Bar.Componentsprivate Dispatcher _dispatcher => _parentViewModel.Dispatcher;private Monitor _monitor => _parentViewModel.Monitor;private WorkspacesComponentConfig _config => _componentConfig as WorkspacesComponentConfig;- private readonly Bus _bus = ServiceLocator.Provider.GetRequiredService<Bus>();+ private readonly Bus _bus = ServiceLocator.GetRequiredService<Bus>();private readonly UserConfigService _userConfigService =- ServiceLocator.Provider.GetRequiredService<UserConfigService>();+ ServiceLocator.GetRequiredService<UserConfigService>();public ObservableCollection<Workspace> Workspaces => new(_orderedWorkspaces);diff --git a/GlazeWM.Bar/MainWindow.xaml.cs b/GlazeWM.Bar/MainWindow.xaml.csindex 62f3424d..b5fa7fea 100644--- a/GlazeWM.Bar/MainWindow.xaml.cs+++ b/GlazeWM.Bar/MainWindow.xaml.cs@@ -8,7 +8,6 @@ using GlazeWM.Domain.Monitors.Events;using GlazeWM.Domain.UserConfigs;using GlazeWM.Infrastructure;using GlazeWM.Infrastructure.Bussing;-using Microsoft.Extensions.DependencyInjection;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Bar@@ -18,9 +17,9 @@ namespace GlazeWM.Bar/// </summary>public partial class MainWindow : Window{- private readonly Bus _bus = ServiceLocator.Provider.GetRequiredService<Bus>();+ private readonly Bus _bus = ServiceLocator.GetRequiredService<Bus>();private readonly UserConfigService _userConfigService =- ServiceLocator.Provider.GetRequiredService<UserConfigService>();+ ServiceLocator.GetRequiredService<UserConfigService>();private BarViewModel _barViewModel { get; }private Dispatcher _dispatcher => _barViewModel.Dispatcher;diff --git a/GlazeWM.Bootstrapper/Program.cs b/GlazeWM.Bootstrapper/Program.csindex fbaaa64e..1fcd507d 100644--- a/GlazeWM.Bootstrapper/Program.cs+++ b/GlazeWM.Bootstrapper/Program.cs@@ -44,7 +44,7 @@ namespace GlazeWM.Bootstrappervar host = CreateHost(args);ServiceLocator.Provider = host.Services;- var startup = ServiceLocator.Provider.GetRequiredService<Startup>();+ var startup = ServiceLocator.GetRequiredService<Startup>();startup.Run();}diff --git a/GlazeWM.Domain/Containers/SplitContainer.cs b/GlazeWM.Domain/Containers/SplitContainer.csindex 5c4958f2..a601952d 100644--- a/GlazeWM.Domain/Containers/SplitContainer.cs+++ b/GlazeWM.Domain/Containers/SplitContainer.cs@@ -1,6 +1,5 @@using GlazeWM.Domain.Common.Enums;using GlazeWM.Infrastructure;-using Microsoft.Extensions.DependencyInjection;namespace GlazeWM.Domain.Containers{@@ -11,7 +10,7 @@ namespace GlazeWM.Domain.Containerspublic double SizePercentage { get; set; } = 1;private readonly ContainerService _containerService =- ServiceLocator.Provider.GetRequiredService<ContainerService>();+ ServiceLocator.GetRequiredService<ContainerService>();public override int Width => _containerService.GetWidthOfResizableContainer(this);public override int Height => _containerService.GetHeightOfResizableContainer(this);diff --git a/GlazeWM.Domain/Windows/TilingWindow.cs b/GlazeWM.Domain/Windows/TilingWindow.csindex a5d67c9f..0f6b164c 100644--- a/GlazeWM.Domain/Windows/TilingWindow.cs+++ b/GlazeWM.Domain/Windows/TilingWindow.cs@@ -2,7 +2,6 @@using GlazeWM.Domain.Containers;using GlazeWM.Infrastructure;using GlazeWM.Infrastructure.WindowsApi;-using Microsoft.Extensions.DependencyInjection;namespace GlazeWM.Domain.Windows{@@ -11,7 +10,7 @@ namespace GlazeWM.Domain.Windowspublic double SizePercentage { get; set; } = 1;private readonly ContainerService _containerService =- ServiceLocator.Provider.GetRequiredService<ContainerService>();+ ServiceLocator.GetRequiredService<ContainerService>();public override int Width => _containerService.GetWidthOfResizableContainer(this);public override int Height => _containerService.GetHeightOfResizableContainer(this);diff --git a/GlazeWM.Domain/Workspaces/Workspace.cs b/GlazeWM.Domain/Workspaces/Workspace.csindex 8e98c79d..835a0a8d 100644--- a/GlazeWM.Domain/Workspaces/Workspace.cs+++ b/GlazeWM.Domain/Workspaces/Workspace.cs@@ -3,7 +3,6 @@ using GlazeWM.Domain.Containers;using GlazeWM.Domain.Monitors;using GlazeWM.Domain.UserConfigs;using GlazeWM.Infrastructure;-using Microsoft.Extensions.DependencyInjection;namespace GlazeWM.Domain.Workspaces{@@ -12,10 +11,10 @@ namespace GlazeWM.Domain.Workspacespublic string Name { get; set; }private readonly UserConfigService _userConfigService =- ServiceLocator.Provider.GetRequiredService<UserConfigService>();+ ServiceLocator.GetRequiredService<UserConfigService>();private readonly WorkspaceService _workspaceService =- ServiceLocator.Provider.GetRequiredService<WorkspaceService>();+ ServiceLocator.GetRequiredService<WorkspaceService>();public string DisplayName =>_userConfigService.GetWorkspaceConfigByName(Name).DisplayName ?? Name;diff --git a/GlazeWM.Infrastructure/Bussing/Bus.cs b/GlazeWM.Infrastructure/Bussing/Bus.csindex d44d9633..36e0db26 100644--- a/GlazeWM.Infrastructure/Bussing/Bus.cs+++ b/GlazeWM.Infrastructure/Bussing/Bus.cs@@ -1,6 +1,4 @@-using Microsoft.Extensions.DependencyInjection;-using Microsoft.Extensions.Logging;-using System;+using Microsoft.Extensions.Logging;using System.Collections.Generic;using System.Reactive.Subjects;@@ -30,7 +28,7 @@ namespace GlazeWM.Infrastructure.Bussing// Create a `Type` object representing the constructed `ICommandHandler` generic.var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());- var handlerInstance = ServiceLocator.Provider.GetRequiredService(handlerType)+ var handlerInstance = ServiceLocator.GetRequiredService(handlerType)as ICommandHandler<T>;lock (LockObj)@@ -47,7 +45,7 @@ namespace GlazeWM.Infrastructure.Bussing// Create a `Type` object representing the constructed `IEventHandler` generic.var handlerType = typeof(IEventHandler<>).MakeGenericType(@event.GetType());- var handlerInstances = ServiceLocator.Provider.GetServices(handlerType)+ var handlerInstances = ServiceLocator.GetServices(handlerType)as IEnumerable<IEventHandler<T>>;foreach (var handler in handlerInstances)diff --git a/GlazeWM.Infrastructure/ServiceLocator.cs b/GlazeWM.Infrastructure/ServiceLocator.csindex 49e1d0f8..77a34b56 100644--- a/GlazeWM.Infrastructure/ServiceLocator.cs+++ b/GlazeWM.Infrastructure/ServiceLocator.cs@@ -1,9 +1,26 @@using System;+using System.Collections.Generic;+using Microsoft.Extensions.DependencyInjection;namespace GlazeWM.Infrastructure{public static class ServiceLocator{- public static IServiceProvider Provider { get; set; }+ public static IServiceProvider Provider { private get; set; }++ public static T GetRequiredService<T>()+ {+ return Provider.GetRequiredService<T>();+ }++ public static object GetRequiredService(Type type)+ {+ return Provider.GetRequiredService(type);+ }++ public static IEnumerable<object> GetServices(Type type)+ {+ return Provider.GetServices(type);+ }}}