mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-13 11:44:57 +00:00
Adding methods for getting all registered services, and removing a service from all types
This commit is contained in:
@@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Release resources.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
m_ServicesSection.Enter();
|
||||
|
||||
foreach (IDisposable service in m_Services.Values.OfType<IDisposable>().Distinct().ToArray())
|
||||
service.Dispose();
|
||||
m_Services.Clear();
|
||||
}
|
||||
finally
|
||||
{
|
||||
m_ServicesSection.Leave();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release resources.
|
||||
/// </summary>
|
||||
public static void DisposeStatic()
|
||||
{
|
||||
if (s_Instance != null)
|
||||
s_Instance.Dispose();
|
||||
s_Instance = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
@@ -88,6 +56,15 @@ namespace ICD.Common.Services
|
||||
return Instance.GetServiceInstance(tService);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the registered services.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<object> GetServices()
|
||||
{
|
||||
return Instance.GetServicesInstance();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to remove the given service from every registered type.
|
||||
/// </summary>
|
||||
/// <param name="service"></param>
|
||||
[PublicAPI]
|
||||
public static void RemoveAllServices(object service)
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (service == null)
|
||||
throw new ArgumentNullException("service");
|
||||
|
||||
Instance.RemoveAllServicesInstance(service);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to remove the given service from the given type.
|
||||
/// </summary>
|
||||
@@ -260,6 +251,15 @@ namespace ICD.Common.Services
|
||||
throw new ServiceNotFoundException(tService);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the registered services.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private IEnumerable<object> GetServicesInstance()
|
||||
{
|
||||
return m_ServicesSection.Execute(() => m_Services.Values.ToList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given service under the given type.
|
||||
/// </summary>
|
||||
@@ -350,6 +350,18 @@ namespace ICD.Common.Services
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the given service from all registered types.
|
||||
/// </summary>
|
||||
/// <param name="service"></param>
|
||||
private void RemoveAllServicesInstance(object service)
|
||||
{
|
||||
if (service == null)
|
||||
throw new ArgumentNullException("service");
|
||||
|
||||
m_ServicesSection.Execute(() => m_Services.RemoveAllValues(service));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user