Tidying, validation, prevent ServiceProvider from being instantiated externally

This commit is contained in:
Chris Cameron
2017-10-02 14:57:13 -04:00
parent 5640b663d6
commit c3d47f6b38

View File

@@ -8,12 +8,60 @@ namespace ICD.Common.Services
{ {
public sealed class ServiceProvider : IDisposable public sealed class ServiceProvider : IDisposable
{ {
#region Static
private static ServiceProvider s_Instance; private static ServiceProvider s_Instance;
private readonly Dictionary<Type, object> m_Services;
private readonly SafeCriticalSection m_ServicesSection;
private static ServiceProvider Instance { get { return s_Instance ?? (s_Instance = new ServiceProvider()); } } private static ServiceProvider Instance { get { return s_Instance ?? (s_Instance = new ServiceProvider()); } }
/// <summary>
/// Prevent external instantiation.
/// </summary>
private ServiceProvider()
{
m_Services = new Dictionary<Type, object>();
m_ServicesSection = new SafeCriticalSection();
}
#region IDisposable
/// <summary>
/// Release resources.
/// </summary>
public void Dispose()
{
try
{
m_ServicesSection.Enter();
foreach (object service in m_Services.Values.Distinct())
{
if (!(service is IDisposable))
continue;
((IDisposable)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> /// <summary>
/// Retrieves the registered service of the given type. Use this for required dependencies. /// Retrieves the registered service of the given type. Use this for required dependencies.
/// </summary> /// </summary>
@@ -37,6 +85,9 @@ namespace ICD.Common.Services
[NotNull] [NotNull]
public static object GetService(Type tService) public static object GetService(Type tService)
{ {
if (tService == null)
throw new ArgumentNullException("tService");
return Instance.GetServiceInstance(tService); return Instance.GetServiceInstance(tService);
} }
@@ -63,14 +114,10 @@ namespace ICD.Common.Services
[CanBeNull] [CanBeNull]
public static object TryGetService(Type tService) public static object TryGetService(Type tService)
{ {
try if (tService == null)
{ throw new ArgumentNullException("tService");
return Instance.GetServiceInstance(tService);
} return Instance.TryGetServiceInstance(tService);
catch (ServiceNotFoundException)
{
return null;
}
} }
/// <summary> /// <summary>
@@ -81,6 +128,10 @@ namespace ICD.Common.Services
[PublicAPI] [PublicAPI]
public static void AddService<TService>(TService service) public static void AddService<TService>(TService service)
{ {
// ReSharper disable once CompareNonConstrainedGenericWithNull
if (service == null)
throw new ArgumentNullException("service");
AddService(typeof(TService), service); AddService(typeof(TService), service);
} }
@@ -92,26 +143,38 @@ namespace ICD.Common.Services
[PublicAPI] [PublicAPI]
public static void AddService(Type tService, object service) public static void AddService(Type tService, object service)
{ {
if (tService == null)
throw new ArgumentNullException("tService");
if (service == null)
throw new ArgumentNullException("service");
Instance.AddServiceInstance(tService, service); Instance.AddServiceInstance(tService, service);
} }
#endregion #endregion
private readonly Dictionary<Type, object> m_Services = new Dictionary<Type, object>(); #region Private Methods
private readonly SafeCriticalSection m_ServicesSection = new SafeCriticalSection();
#region Methods /// <summary>
/// Retrieves the registered service of the given type. Returns null if the service type was not found.
private object GetServiceInstance(Type tService) /// Use this for optional dependencies.
/// </summary>
/// <param name="tService">service type to retrieve</param>
/// <returns>requested service or null if that service type is not registered</returns>
[CanBeNull]
private object TryGetServiceInstance(Type tService)
{ {
if (tService == null)
throw new ArgumentNullException("tService");
try try
{ {
m_ServicesSection.Enter(); m_ServicesSection.Enter();
object service; object service;
if (m_Services.TryGetValue(tService, out service) && service != null) m_Services.TryGetValue(tService, out service);
return service; return service;
throw new ServiceNotFoundException(tService);
} }
finally finally
{ {
@@ -119,30 +182,41 @@ namespace ICD.Common.Services
} }
} }
/// <summary>
/// Gets the service of the given type.
/// </summary>
/// <param name="tService"></param>
/// <returns></returns>
private object GetServiceInstance(Type tService)
{
if (tService == null)
throw new ArgumentNullException("tService");
object service = TryGetService(tService);
if (service != null)
return service;
throw new ServiceNotFoundException(tService);
}
/// <summary>
/// Adds the given service under the given type.
/// </summary>
/// <param name="tService"></param>
/// <param name="service"></param>
[PublicAPI] [PublicAPI]
private void AddServiceInstance(Type tService, object service) private void AddServiceInstance(Type tService, object service)
{ {
m_ServicesSection.Enter(); if (tService == null)
m_Services[tService] = service; throw new ArgumentNullException("tService");
m_ServicesSection.Leave();
}
#endregion if (service == null)
throw new ArgumentNullException("service");
#region IDisposable
public void Dispose()
{
try try
{ {
m_ServicesSection.Enter(); m_ServicesSection.Enter();
foreach (object service in m_Services.Values.Distinct()) m_Services.Add(tService, service);
{
if (!(service is IDisposable))
continue;
((IDisposable)service).Dispose();
}
m_Services.Clear();
} }
finally finally
{ {
@@ -150,13 +224,6 @@ namespace ICD.Common.Services
} }
} }
public static void DisposeStatic()
{
if (s_Instance != null)
s_Instance.Dispose();
s_Instance = null;
}
#endregion #endregion
} }
} }