diff --git a/ICD.Common.Utils/Services/ServiceProvider.cs b/ICD.Common.Utils/Services/ServiceProvider.cs index 734ace4..a249c39 100644 --- a/ICD.Common.Utils/Services/ServiceProvider.cs +++ b/ICD.Common.Utils/Services/ServiceProvider.cs @@ -3,10 +3,11 @@ using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; using ICD.Common.Utils; +using ICD.Common.Utils.Extensions; namespace ICD.Common.Services { - public sealed class ServiceProvider : IDisposable + public sealed class ServiceProvider { private static ServiceProvider s_Instance; @@ -24,39 +25,6 @@ namespace ICD.Common.Services m_ServicesSection = new SafeCriticalSection(); } - #region IDisposable - - /// - /// Release resources. - /// - public void Dispose() - { - try - { - m_ServicesSection.Enter(); - - foreach (IDisposable service in m_Services.Values.OfType().Distinct().ToArray()) - service.Dispose(); - m_Services.Clear(); - } - finally - { - m_ServicesSection.Leave(); - } - } - - /// - /// Release resources. - /// - public static void DisposeStatic() - { - if (s_Instance != null) - s_Instance.Dispose(); - s_Instance = null; - } - - #endregion - #region Methods /// @@ -88,6 +56,15 @@ namespace ICD.Common.Services return Instance.GetServiceInstance(tService); } + /// + /// Retrieves the registered services. + /// + /// + public static IEnumerable GetServices() + { + return Instance.GetServicesInstance(); + } + /// /// Retrieves the registered service of the given type. Returns null if the service type was not found. /// Use this for optional dependencies. @@ -181,6 +158,20 @@ namespace ICD.Common.Services return Instance.TryAddServiceInstance(tService, service); } + /// + /// Attempts to remove the given service from every registered type. + /// + /// + [PublicAPI] + public static void RemoveAllServices(object service) + { + // ReSharper disable once CompareNonConstrainedGenericWithNull + if (service == null) + throw new ArgumentNullException("service"); + + Instance.RemoveAllServicesInstance(service); + } + /// /// Attempts to remove the given service from the given type. /// @@ -260,6 +251,15 @@ namespace ICD.Common.Services throw new ServiceNotFoundException(tService); } + /// + /// Gets the registered services. + /// + /// + private IEnumerable GetServicesInstance() + { + return m_ServicesSection.Execute(() => m_Services.Values.ToList()); + } + /// /// Adds the given service under the given type. /// @@ -350,6 +350,18 @@ namespace ICD.Common.Services } } + /// + /// Removes the given service from all registered types. + /// + /// + private void RemoveAllServicesInstance(object service) + { + if (service == null) + throw new ArgumentNullException("service"); + + m_ServicesSection.Execute(() => m_Services.RemoveAllValues(service)); + } + #endregion } }