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
{
#region Static
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()); } }
/// <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>
/// Retrieves the registered service of the given type. Use this for required dependencies.
/// </summary>
@@ -37,6 +85,9 @@ namespace ICD.Common.Services
[NotNull]
public static object GetService(Type tService)
{
if (tService == null)
throw new ArgumentNullException("tService");
return Instance.GetServiceInstance(tService);
}
@@ -63,14 +114,10 @@ namespace ICD.Common.Services
[CanBeNull]
public static object TryGetService(Type tService)
{
try
{
return Instance.GetServiceInstance(tService);
}
catch (ServiceNotFoundException)
{
return null;
}
if (tService == null)
throw new ArgumentNullException("tService");
return Instance.TryGetServiceInstance(tService);
}
/// <summary>
@@ -81,6 +128,10 @@ namespace ICD.Common.Services
[PublicAPI]
public static void AddService<TService>(TService service)
{
// ReSharper disable once CompareNonConstrainedGenericWithNull
if (service == null)
throw new ArgumentNullException("service");
AddService(typeof(TService), service);
}
@@ -92,26 +143,38 @@ namespace ICD.Common.Services
[PublicAPI]
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);
}
#endregion
private readonly Dictionary<Type, object> m_Services = new Dictionary<Type, object>();
private readonly SafeCriticalSection m_ServicesSection = new SafeCriticalSection();
#region Private Methods
#region Methods
private object GetServiceInstance(Type tService)
/// <summary>
/// Retrieves the registered service of the given type. Returns null if the service type was not found.
/// 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
{
m_ServicesSection.Enter();
object service;
if (m_Services.TryGetValue(tService, out service) && service != null)
return service;
throw new ServiceNotFoundException(tService);
m_Services.TryGetValue(tService, out service);
return service;
}
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]
private void AddServiceInstance(Type tService, object service)
{
m_ServicesSection.Enter();
m_Services[tService] = service;
m_ServicesSection.Leave();
}
if (tService == null)
throw new ArgumentNullException("tService");
#endregion
if (service == null)
throw new ArgumentNullException("service");
#region IDisposable
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();
m_Services.Add(tService, service);
}
finally
{
@@ -150,13 +224,6 @@ namespace ICD.Common.Services
}
}
public static void DisposeStatic()
{
if (s_Instance != null)
s_Instance.Dispose();
s_Instance = null;
}
#endregion
}
}