mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
Added "TryAddService" methods to fail gracefully if service already exists
This commit is contained in:
@@ -132,6 +132,21 @@ namespace ICD.Common.Services
|
|||||||
AddService(typeof(TService), service);
|
AddService(typeof(TService), service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers a service instance to the type given.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TService"></typeparam>
|
||||||
|
/// <param name="service"></param>
|
||||||
|
[PublicAPI]
|
||||||
|
public static bool TryAddService<TService>(TService service)
|
||||||
|
{
|
||||||
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
|
if (service == null)
|
||||||
|
throw new ArgumentNullException("service");
|
||||||
|
|
||||||
|
return TryAddService(typeof(TService), service);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a service instance to the type given.
|
/// Registers a service instance to the type given.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -149,6 +164,23 @@ namespace ICD.Common.Services
|
|||||||
Instance.AddServiceInstance(tService, service);
|
Instance.AddServiceInstance(tService, service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers a service instance to the type given.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tService"></param>
|
||||||
|
/// <param name="service"></param>
|
||||||
|
[PublicAPI]
|
||||||
|
public static bool TryAddService(Type tService, object service)
|
||||||
|
{
|
||||||
|
if (tService == null)
|
||||||
|
throw new ArgumentNullException("tService");
|
||||||
|
|
||||||
|
if (service == null)
|
||||||
|
throw new ArgumentNullException("service");
|
||||||
|
|
||||||
|
return Instance.TryAddServiceInstance(tService, 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>
|
||||||
@@ -259,6 +291,37 @@ namespace ICD.Common.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the given service under the given type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tService"></param>
|
||||||
|
/// <param name="service"></param>
|
||||||
|
private bool TryAddServiceInstance(Type tService, object service)
|
||||||
|
{
|
||||||
|
if (tService == null)
|
||||||
|
throw new ArgumentNullException("tService");
|
||||||
|
|
||||||
|
if (service == null)
|
||||||
|
throw new ArgumentNullException("service");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m_ServicesSection.Enter();
|
||||||
|
|
||||||
|
if (m_Services.ContainsKey(tService))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_Services.Add(tService, service);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
m_ServicesSection.Leave();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the given service from the given type.
|
/// Removes the given service from the given type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user