diff --git a/src/Greenshot.Base/Core/SimpleServiceProvider.cs b/src/Greenshot.Base/Core/SimpleServiceProvider.cs index 084f5b1fd..ed64022bb 100644 --- a/src/Greenshot.Base/Core/SimpleServiceProvider.cs +++ b/src/Greenshot.Base/Core/SimpleServiceProvider.cs @@ -12,8 +12,12 @@ namespace Greenshot.Base.Core { private readonly Dictionary> _services = new(); + /// + /// Gets the current instance of the service locator. + /// public static IServiceLocator Current { get; } = new SimpleServiceProvider(); + /// public IReadOnlyList GetAllInstances() { var typeOfService = typeof(TService); @@ -25,11 +29,13 @@ namespace Greenshot.Base.Core return results.Cast().ToArray(); } + /// public TService GetInstance() { return GetAllInstances().SingleOrDefault(); } + /// public void AddService(IEnumerable services) { var serviceType = typeof(TService); @@ -50,9 +56,40 @@ namespace Greenshot.Base.Core } } + /// public void AddService(params TService[] services) { AddService(services.AsEnumerable()); } + + /// + public void RemoveService(IEnumerable services) + { + var serviceType = typeof(TService); + if (!_services.TryGetValue(serviceType, out var currentServices)) + { + return; + } + + foreach (var service in services) + { + if (service == null) + { + continue; + } + currentServices.Remove(service); + } + + if (currentServices.Count == 0) + { + _services.Remove(serviceType); + } + } + + /// + public void RemoveService(params TService[] services) + { + RemoveService(services.AsEnumerable()); + } } } \ No newline at end of file diff --git a/src/Greenshot.Base/Interfaces/IServiceLocator.cs b/src/Greenshot.Base/Interfaces/IServiceLocator.cs index 5cf1fb529..9a0081fee 100644 --- a/src/Greenshot.Base/Interfaces/IServiceLocator.cs +++ b/src/Greenshot.Base/Interfaces/IServiceLocator.cs @@ -54,5 +54,19 @@ namespace Greenshot.Base.Interfaces /// Type of the service /// IEnumerable{TService} with services to add void AddService(IEnumerable services); + + /// + /// Remove one or more services from the registry + /// + /// Type of the service + /// One or more services which need to be removed + void RemoveService(params TService[] services); + + /// + /// Remove multiple services from the registry + /// + /// Type of the service + /// IEnumerable{TService} with services to remove + void RemoveService(IEnumerable services); } } \ No newline at end of file