mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +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 System.Linq;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
using ICD.Common.Utils;
|
using ICD.Common.Utils;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
|
|
||||||
namespace ICD.Common.Services
|
namespace ICD.Common.Services
|
||||||
{
|
{
|
||||||
public sealed class ServiceProvider : IDisposable
|
public sealed class ServiceProvider
|
||||||
{
|
{
|
||||||
private static ServiceProvider s_Instance;
|
private static ServiceProvider s_Instance;
|
||||||
|
|
||||||
@@ -24,39 +25,6 @@ namespace ICD.Common.Services
|
|||||||
m_ServicesSection = new SafeCriticalSection();
|
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
|
#region Methods
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -88,6 +56,15 @@ namespace ICD.Common.Services
|
|||||||
return Instance.GetServiceInstance(tService);
|
return Instance.GetServiceInstance(tService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves the registered services.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IEnumerable<object> GetServices()
|
||||||
|
{
|
||||||
|
return Instance.GetServicesInstance();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves the registered service of the given type. Returns null if the service type was not found.
|
/// Retrieves the registered service of the given type. Returns null if the service type was not found.
|
||||||
/// Use this for optional dependencies.
|
/// Use this for optional dependencies.
|
||||||
@@ -181,6 +158,20 @@ namespace ICD.Common.Services
|
|||||||
return Instance.TryAddServiceInstance(tService, service);
|
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>
|
/// <summary>
|
||||||
/// Attempts to remove the given service from the given type.
|
/// Attempts to remove the given service from the given type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -260,6 +251,15 @@ namespace ICD.Common.Services
|
|||||||
throw new ServiceNotFoundException(tService);
|
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>
|
/// <summary>
|
||||||
/// Adds the given service under the given type.
|
/// Adds the given service under the given type.
|
||||||
/// </summary>
|
/// </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
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user